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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64e55ed4eec9458ee6526ed63ec8ea39417a2102
4
- data.tar.gz: 8daa9c4b65bed25483f24c8a1dd029bbc80828c8
3
+ metadata.gz: 05918bfadfd474f37a811f57b5504868a353ca88
4
+ data.tar.gz: 1be969833dce3048884e47bed71f6602b44cab3e
5
5
  SHA512:
6
- metadata.gz: 678e6efe0c5dd85db8253f35388e83c907c026912a22cce499015a5d18c17cc914d314618017e9a34dd696d37c5e3e1786ad4aace7c1fc1ce513f96d9731be98
7
- data.tar.gz: 62cf0e908936c457153bf982aa5803a634efdefde4a878a454294ccf5b91c12fe8a36d870cf448eee31f1e7831e25c92d5bc084c0359feeef23cb4498ec5cd3e
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 ###
@@ -15,7 +15,7 @@ private
15
15
 
16
16
  def rtype(method_name, type_sig_info)
17
17
  if is_a?(Module)
18
- ::Rtype.define_typed_method(self, method_name, type_sig_info)
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
@@ -1,3 +1,3 @@
1
1
  module Rtype
2
- VERSION = "0.0.2".freeze
2
+ VERSION = "0.1.0".freeze
3
3
  end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rtype
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sputnik Gugja