rtype 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -0
- data/lib/rtype/core_ext.rb +13 -1
- data/lib/rtype/version.rb +1 -1
- data/lib/rtype.rb +8 -0
- data/spec/rtype_spec.rb +29 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05918bfadfd474f37a811f57b5504868a353ca88
|
4
|
+
data.tar.gz: 1be969833dce3048884e47bed71f6602b44cab3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57da419c19fae640ca9f9cfc480ac54e7706d3c6be975778de885d423896f4af6f911f8c43b81197d487621f007c67c0814ddc5be7c2e993b55d709ff5bd6812
|
7
|
+
data.tar.gz: 07e3998a9f936988ac17bc140eee6eb5f9301fb8621892a257aa6ffcf4790a2f6621856bb0268bc4958eb59d87449f019a77fec35fd6f26439d97d572c9acd9a
|
data/README.md
CHANGED
@@ -189,6 +189,31 @@ func [1]
|
|
189
189
|
func [1, 2] # Your location is (1, 2). I will look for you. I will find you
|
190
190
|
```
|
191
191
|
|
192
|
+
#### rtype with attr_accessor
|
193
|
+
`rtype_accessor`
|
194
|
+
|
195
|
+
You can use `rtype_accessor_self` for static accessor.
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
require 'rtype'
|
199
|
+
|
200
|
+
class Example
|
201
|
+
rtype_accessor :value, String
|
202
|
+
attr_accessor :value
|
203
|
+
def initialize
|
204
|
+
@value = 456
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
Example.new.value = 123
|
209
|
+
# (Rtype::ArgumentTypeError) for 1st argument:
|
210
|
+
# Expected 123 to be a String
|
211
|
+
|
212
|
+
Example.new.value
|
213
|
+
# (Rtype::ReturnTypeError) for return:
|
214
|
+
# Expected 456 to be a String
|
215
|
+
```
|
216
|
+
|
192
217
|
#### Combined type
|
193
218
|
```ruby
|
194
219
|
### TEST 1 ###
|
data/lib/rtype/core_ext.rb
CHANGED
@@ -15,7 +15,7 @@ private
|
|
15
15
|
|
16
16
|
def rtype(method_name, type_sig_info)
|
17
17
|
if is_a?(Module)
|
18
|
-
::Rtype
|
18
|
+
::Rtype::define_typed_method(self, method_name, type_sig_info)
|
19
19
|
else
|
20
20
|
rtype_self(method_name, type_sig_info)
|
21
21
|
end
|
@@ -24,6 +24,18 @@ private
|
|
24
24
|
def rtype_self(method_name, type_sig_info)
|
25
25
|
::Rtype.define_typed_method(singleton_class, method_name, type_sig_info)
|
26
26
|
end
|
27
|
+
|
28
|
+
def rtype_accessor(accessor_name, type_behavior)
|
29
|
+
if is_a?(Module)
|
30
|
+
::Rtype::define_typed_accessor(self, accessor_name, type_behavior)
|
31
|
+
else
|
32
|
+
rtype_accessor_self(accessor_name, type_behavior)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def rtype_accessor_self(accessor_name, type_behavior)
|
37
|
+
::Rtype::define_typed_accessor(singleton_class, accessor_name, type_behavior)
|
38
|
+
end
|
27
39
|
end
|
28
40
|
|
29
41
|
class Method
|
data/lib/rtype/version.rb
CHANGED
data/lib/rtype.rb
CHANGED
@@ -59,6 +59,14 @@ module Rtype
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
def define_typed_accessor(owner, accessor_name, type_behavior)
|
63
|
+
getter = accessor_name.to_sym
|
64
|
+
setter = :"#{accessor_name}="
|
65
|
+
valid?(type_behavior, nil)
|
66
|
+
define_typed_method owner, getter, [] => type_behavior
|
67
|
+
define_typed_method owner, setter, [type_behavior] => Any
|
68
|
+
end
|
69
|
+
|
62
70
|
# validate argument type
|
63
71
|
def valid?(expected, value)
|
64
72
|
case expected
|
data/spec/rtype_spec.rb
CHANGED
@@ -101,6 +101,35 @@ describe Rtype do
|
|
101
101
|
expect {TestClass::static_test_return}.to raise_error Rtype::ReturnTypeError
|
102
102
|
end
|
103
103
|
|
104
|
+
it 'Kernel#rtype_accessor' do
|
105
|
+
class TestClass
|
106
|
+
rtype_accessor :value, String
|
107
|
+
attr_accessor :value
|
108
|
+
|
109
|
+
def initialize
|
110
|
+
@value = 123
|
111
|
+
end
|
112
|
+
end
|
113
|
+
expect {TestClass.new.value = 123}.to raise_error Rtype::ArgumentTypeError
|
114
|
+
expect {TestClass.new.value}.to raise_error Rtype::ReturnTypeError
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'Kernel#rtype_accessor_self' do
|
118
|
+
class TestClass
|
119
|
+
@@val = 123
|
120
|
+
|
121
|
+
rtype_accessor_self :value, String
|
122
|
+
def self.value=(val)
|
123
|
+
@@val = val
|
124
|
+
end
|
125
|
+
def self.value
|
126
|
+
@@val
|
127
|
+
end
|
128
|
+
end
|
129
|
+
expect {TestClass::value = 123}.to raise_error Rtype::ArgumentTypeError
|
130
|
+
expect {TestClass::value}.to raise_error Rtype::ReturnTypeError
|
131
|
+
end
|
132
|
+
|
104
133
|
describe 'Test type behaviors' do
|
105
134
|
describe 'Module' do
|
106
135
|
it "is right" do
|