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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +24 -2
- data/lib/typerb/version.rb +1 -1
- data/lib/typerb.rb +7 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b684a861507b2c491d213388d490af7b7543146dc3938f6817a8c1c909a359b5
|
4
|
+
data.tar.gz: 53160641ff2410235cc25b73c37a57d81d14b4b322a6e7fabe68f424e076907c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2bde15804ce81dd7ddf94eefba526ca8ae24bb2880a628163d11068ea6c2fa8527dd4e6ef9be1ccba9689ed4b7a7cb6874be4dd7a9c7bd8eaebfc6f1457311b
|
7
|
+
data.tar.gz: 563e93c1cd6b3787d8876da94ac612af27be1accb3300012f5eb7bac9fdbb3ebdc7f68e6efe5b2d15126ca14cec5d55d1769990fe7e387d793e91321a12df9d7
|
data/Gemfile.lock
CHANGED
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
|
data/lib/typerb/version.rb
CHANGED
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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.
|
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:
|
125
|
+
rubygems_version: 3.0.0.beta2
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Typecheck sugar for Ruby.
|