typerb 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e7010e0b1a8ba8cdb3fbbb061c5316f3138e82a5be4fe2d07cd7f5c15a36d4a
4
- data.tar.gz: 7f3485b10d7970a9417a983a5fa650a0a027e7b37e862206b1835ff92c2d64b2
3
+ metadata.gz: b684a861507b2c491d213388d490af7b7543146dc3938f6817a8c1c909a359b5
4
+ data.tar.gz: 53160641ff2410235cc25b73c37a57d81d14b4b322a6e7fabe68f424e076907c
5
5
  SHA512:
6
- metadata.gz: f459593a67c3e9a4a2c3f53a857142ae7fa038068bcbf6039fbb2130b01ba808f3b47e54345bf8d63852ec3e8133f7e00cf2a44aa7906bd538e84805b89e5346
7
- data.tar.gz: 96c0c44dcc7ebfd580a979f1b83f52c6c5051d7c3e8c2ba4e480e425466bfc520479d458b4009ce7a1945625d89d6348e0960271a7dc880792c60f26b0e7474b
6
+ metadata.gz: b2bde15804ce81dd7ddf94eefba526ca8ae24bb2880a628163d11068ea6c2fa8527dd4e6ef9be1ccba9689ed4b7a7cb6874be4dd7a9c7bd8eaebfc6f1457311b
7
+ data.tar.gz: 563e93c1cd6b3787d8876da94ac612af27be1accb3300012f5eb7bac9fdbb3ebdc7f68e6efe5b2d15126ca14cec5d55d1769990fe7e387d793e91321a12df9d7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- typerb (0.1.0)
4
+ typerb (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -35,12 +35,23 @@ gem 'typerb'
35
35
 
36
36
  And then execute:
37
37
 
38
- $ bundle
38
+ $ bundle install
39
39
 
40
40
  Or install it yourself as:
41
41
 
42
42
  $ gem install typerb
43
43
 
44
+ If this fails with error
45
+ ```
46
+ ERROR: Error installing typerb:
47
+ There are no versions of typerb (>= 0) compatible with your Ruby & RubyGems
48
+ typerb requires Ruby version >= 2.6.0.pre.preview3. The current ruby version is 2.6.0.
49
+ ```
50
+ even when you have Ruby 2.6.0-preview3 installed, then try installing it through Gemfile from git:
51
+ ```ruby
52
+ gem 'typerb', github: 'olegantonyan/typerb'
53
+ ```
54
+
44
55
  ## Usage
45
56
 
46
57
  1. Add `using Typerb` to a class where you want to have type check.
@@ -66,7 +77,7 @@ class A
66
77
 
67
78
  def call(some_arg)
68
79
  some_arg.
69
- type!(String) # this won't work. type!() call must be on the same line with the variable it's called on
80
+ type!(String) # this won't work. type!() call must be on the same line with the variable it's called on - raise error message without variable name
70
81
  # some_arg. type!(String) is ok though
71
82
  end
72
83
  end
@@ -84,6 +95,17 @@ end
84
95
  TypeError: expected Hash, got Integer # here we cannot get the source code for a line containing "a.type!(Hash)", so cannot see the variable name
85
96
  ```
86
97
 
98
+ 3. Multiple arguments on the same line:
99
+ ```ruby
100
+ class A
101
+ using Typerb
102
+
103
+ def initialize(arg1, arg2)
104
+ arg1.type!(Integer); arg2.type!(String) # no way to tell the variable - raise error message without variable name
105
+ end
106
+ end
107
+ ```
108
+
87
109
  These limitations shouldn't be a problem in any case. Please, file an issue if you know a scenario where one of these could be a real problem.
88
110
 
89
111
  ## Development
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Typerb
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
data/lib/typerb.rb CHANGED
@@ -10,16 +10,19 @@ module Typerb
10
10
 
11
11
  kls_text = klasses.size > 1 ? klasses.map(&:name).join(' or ') : klasses.first.name
12
12
 
13
+ # error message when multiple calls on the same line, or in a console - can't extract variable name
14
+ exception_text = "expected #{kls_text}, got #{self.class} (#{to_s})"
15
+
13
16
  where = caller_locations(1, 1)[0]
14
17
  file = where.path
15
18
  line = where.lineno
16
19
  if File.exist?(file)
17
20
  code = File.read(file).lines[line - 1].strip
18
21
  node = RubyVM::AST.parse(code)
19
- var_name = node.children.last.children.first.children.first
20
- exception_text = "`#{var_name}` should be #{kls_text}, not #{self.class}"
21
- else # probably in console
22
- exception_text = "expected #{kls_text}, got #{self.class}"
22
+ if node.children.last.children.size == 3 && node.children.last.children[1] == :type!
23
+ var_name = node.children.last.children.first.children.first
24
+ exception_text = "`#{var_name}` should be #{kls_text}, not #{self.class} (#{to_s})"
25
+ end
23
26
  end
24
27
  exception = TypeError.new(exception_text)
25
28
  exception.set_backtrace(caller)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typerb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Antonyan
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  requirements: []
124
124
  rubyforge_project:
125
- rubygems_version: 2.7.8
125
+ rubygems_version: 3.0.0.beta2
126
126
  signing_key:
127
127
  specification_version: 4
128
128
  summary: Typecheck sugar for Ruby.