attr_extras 2.2.1 → 2.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/lib/attr_extras.rb +6 -2
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_extras_spec.rb +1 -285
- data/spec/attr_id_query_spec.rb +20 -0
- data/spec/attr_initialize_spec.rb +53 -0
- data/spec/attr_private_spec.rb +18 -0
- data/spec/attr_query_spec.rb +19 -0
- data/spec/attr_value_spec.rb +105 -0
- data/spec/method_object_spec.rb +29 -0
- data/spec/pattr_initialize_spec.rb +21 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/vattr_initialize_spec.rb +26 -0
- metadata +28 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f439f8648baa182127cdc634072670fa5651ae4
|
4
|
+
data.tar.gz: e0b28afc47123a801a92154e1e5616be24962df8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 641e66f8a897f8a32585674cb30a16c6461e2c5c68d795b2bf79117285078e58656a7be04a77e8c5a1ab112ab57375351221ede2c73e16a50194881d88661894
|
7
|
+
data.tar.gz: bb9534b45b03c02e410ab8dd6ad3f4b4ac9d1dd7d7913b70ac883e587d2f1db22d46047aad358ebc838135dcee18052f208eec0144fa2663b582f6bdb2bf3a71
|
data/README.md
CHANGED
@@ -178,6 +178,25 @@ Or install it yourself as:
|
|
178
178
|
gem install attr_extras
|
179
179
|
|
180
180
|
|
181
|
+
## Running the tests
|
182
|
+
|
183
|
+
Run then with:
|
184
|
+
|
185
|
+
`rake`
|
186
|
+
|
187
|
+
Or to see warnings (try not to have any):
|
188
|
+
|
189
|
+
`RUBYOPT=-w rake`
|
190
|
+
|
191
|
+
|
192
|
+
## Contributors
|
193
|
+
|
194
|
+
* [Henrik Nyh](https://github.com/henrik)
|
195
|
+
* [Joakim Kolsjö](https://github.com/joakimk)
|
196
|
+
* [Victor Arias](https://github.com/victorarias)
|
197
|
+
* [Teo Ljungberg](https://github.com/teoljungberg)
|
198
|
+
|
199
|
+
|
181
200
|
## License
|
182
201
|
|
183
202
|
Copyright (c) 2012-2014 [Barsoom AB](http://barsoom.se)
|
data/lib/attr_extras.rb
CHANGED
@@ -10,6 +10,10 @@ module AttrExtras
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def attr_private(*names)
|
13
|
+
# Need this to avoid "private attribute?" warnings when running
|
14
|
+
# the full test suite; not sure why exactly.
|
15
|
+
public
|
16
|
+
|
13
17
|
attr_reader(*names)
|
14
18
|
private(*names)
|
15
19
|
end
|
@@ -34,12 +38,12 @@ module AttrExtras
|
|
34
38
|
|
35
39
|
def pattr_initialize(*names)
|
36
40
|
attr_initialize(*names)
|
37
|
-
attr_private
|
41
|
+
attr_private(*Utils.flat_names(names))
|
38
42
|
end
|
39
43
|
|
40
44
|
def vattr_initialize(*names)
|
41
45
|
attr_initialize(*names)
|
42
|
-
attr_value
|
46
|
+
attr_value(*Utils.flat_names(names))
|
43
47
|
end
|
44
48
|
|
45
49
|
def method_object(method_name, *names)
|
data/lib/attr_extras/version.rb
CHANGED
data/spec/attr_extras_spec.rb
CHANGED
@@ -1,288 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "minitest/pride"
|
5
|
-
require "attr_extras"
|
6
|
-
|
7
|
-
describe Object, ".method_object" do
|
8
|
-
it "creates a class method that instantiates and runs that instance method" do
|
9
|
-
klass = Class.new do
|
10
|
-
method_object :fooable?,
|
11
|
-
:foo
|
12
|
-
|
13
|
-
def fooable?
|
14
|
-
foo
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
assert klass.fooable?(true)
|
19
|
-
refute klass.fooable?(false)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "doesn't require attributes" do
|
23
|
-
klass = Class.new do
|
24
|
-
method_object :fooable?
|
25
|
-
|
26
|
-
def fooable?
|
27
|
-
true
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
assert klass.fooable?
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe Object, ".attr_initialize" do
|
36
|
-
let(:klass) do
|
37
|
-
Class.new do
|
38
|
-
attr_initialize :foo, :bar
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
it "creates an initializer setting those instance variables" do
|
43
|
-
example = klass.new("Foo", "Bar")
|
44
|
-
example.instance_variable_get("@foo").must_equal "Foo"
|
45
|
-
example.instance_variable_get("@bar").must_equal "Bar"
|
46
|
-
end
|
47
|
-
|
48
|
-
it "requires all arguments" do
|
49
|
-
lambda { klass.new("Foo") }.must_raise ArgumentError
|
50
|
-
end
|
51
|
-
|
52
|
-
it "can set ivars from a hash" do
|
53
|
-
klass = Class.new do
|
54
|
-
attr_initialize :foo, [:bar, :baz]
|
55
|
-
end
|
56
|
-
|
57
|
-
example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
58
|
-
example.instance_variable_get("@foo").must_equal "Foo"
|
59
|
-
example.instance_variable_get("@bar").must_equal "Bar"
|
60
|
-
example.instance_variable_get("@baz").must_equal "Baz"
|
61
|
-
end
|
62
|
-
|
63
|
-
it "treats hash values as optional" do
|
64
|
-
klass = Class.new do
|
65
|
-
attr_initialize :foo, [:bar, :baz]
|
66
|
-
end
|
67
|
-
|
68
|
-
example = klass.new("Foo", :bar => "Bar")
|
69
|
-
example.instance_variable_get("@baz").must_equal nil
|
70
|
-
|
71
|
-
example = klass.new("Foo")
|
72
|
-
example.instance_variable_get("@bar").must_equal nil
|
73
|
-
end
|
74
|
-
|
75
|
-
it "can require hash values" do
|
76
|
-
klass = Class.new do
|
77
|
-
attr_initialize [:optional, :required!]
|
78
|
-
end
|
79
|
-
|
80
|
-
example = klass.new(:required => "X")
|
81
|
-
example.instance_variable_get("@required").must_equal "X"
|
82
|
-
|
83
|
-
lambda { klass.new(:optional => "X") }.must_raise KeyError
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
describe Object, ".attr_private" do
|
88
|
-
let(:klass) do
|
89
|
-
Class.new do
|
90
|
-
attr_private :foo, :bar
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
it "creates private readers" do
|
95
|
-
example = klass.new
|
96
|
-
example.instance_variable_set("@foo", "Foo")
|
97
|
-
example.instance_variable_set("@bar", "Bar")
|
98
|
-
example.send(:foo).must_equal "Foo"
|
99
|
-
example.send(:bar).must_equal "Bar"
|
100
|
-
lambda { example.foo }.must_raise NoMethodError
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
describe Object, ".pattr_initialize" do
|
105
|
-
it "creates both initializer and private readers" do
|
106
|
-
klass = Class.new do
|
107
|
-
pattr_initialize :foo, :bar
|
108
|
-
end
|
109
|
-
|
110
|
-
example = klass.new("Foo", "Bar")
|
111
|
-
example.send(:foo).must_equal "Foo"
|
112
|
-
end
|
113
|
-
|
114
|
-
it "works with hash ivars" do
|
115
|
-
klass = Class.new do
|
116
|
-
pattr_initialize :foo, [:bar, :baz!]
|
117
|
-
end
|
118
|
-
|
119
|
-
example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
120
|
-
example.send(:baz).must_equal "Baz"
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
describe Object, ".vattr_initialize" do
|
125
|
-
it "creates initializer, value readers and value object identity" do
|
126
|
-
klass = Class.new do
|
127
|
-
vattr_initialize :foo, :bar
|
128
|
-
end
|
129
|
-
|
130
|
-
example1 = klass.new("Foo", "Bar")
|
131
|
-
example2 = klass.new("Foo", "Bar")
|
132
|
-
|
133
|
-
example1.foo.must_equal "Foo"
|
134
|
-
example1.must_equal example2
|
135
|
-
end
|
136
|
-
|
137
|
-
it "works with hash ivars" do
|
138
|
-
klass = Class.new do
|
139
|
-
vattr_initialize :foo, [:bar, :baz!]
|
140
|
-
end
|
141
|
-
|
142
|
-
example1 = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
143
|
-
example2 = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
144
|
-
example1.baz.must_equal "Baz"
|
145
|
-
example1.must_equal example2
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
describe Object, ".attr_value" do
|
150
|
-
it "creates public readers" do
|
151
|
-
klass = Class.new do
|
152
|
-
attr_value :foo, :bar
|
153
|
-
end
|
154
|
-
|
155
|
-
example = klass.new
|
156
|
-
example.instance_variable_set("@foo", "Foo")
|
157
|
-
example.foo.must_equal "Foo"
|
158
|
-
lambda { example.foo = "new value" }.must_raise NoMethodError
|
159
|
-
end
|
160
|
-
|
161
|
-
it "defines equality based on the attributes" do
|
162
|
-
klass = Class.new do
|
163
|
-
attr_initialize :foo, :bar
|
164
|
-
attr_value :foo, :bar
|
165
|
-
end
|
166
|
-
|
167
|
-
example1 = klass.new("Foo", "Bar")
|
168
|
-
example2 = klass.new("Foo", "Bar")
|
169
|
-
example3 = klass.new("Arroz", "Feijão")
|
170
|
-
|
171
|
-
assert example1 == example2, "Examples should be equal"
|
172
|
-
refute example1 != example2, "Examples should be equal"
|
173
|
-
|
174
|
-
refute example1 == example3, "Examples should not be equal"
|
175
|
-
assert example1 != example3, "Examples should not be equal"
|
176
|
-
end
|
177
|
-
|
178
|
-
it "defines equality based on the actual type" do
|
179
|
-
klass1 = Class.new do
|
180
|
-
attr_initialize :foo
|
181
|
-
attr_value :foo
|
182
|
-
end
|
183
|
-
klass2 = Class.new do
|
184
|
-
attr_initialize :foo
|
185
|
-
attr_value :foo
|
186
|
-
end
|
187
|
-
|
188
|
-
example1 = klass1.new("Foo")
|
189
|
-
example2 = klass2.new("Foo")
|
190
|
-
|
191
|
-
assert example1 != example2, "Examples should not be equal"
|
192
|
-
refute example1 == example2, "Examples should not be equal"
|
193
|
-
end
|
194
|
-
|
195
|
-
it "considers an instance equal to itself" do
|
196
|
-
klass = Class.new do
|
197
|
-
attr_initialize :foo
|
198
|
-
attr_value :foo
|
199
|
-
end
|
200
|
-
|
201
|
-
instance = klass.new("Foo")
|
202
|
-
|
203
|
-
assert instance == instance, "Instance should be equal to itself"
|
204
|
-
end
|
205
|
-
|
206
|
-
it "can compare value objects to other kinds of objects" do
|
207
|
-
klass = Class.new do
|
208
|
-
attr_initialize :foo
|
209
|
-
attr_value :foo
|
210
|
-
end
|
211
|
-
|
212
|
-
instance = klass.new("Foo")
|
213
|
-
|
214
|
-
assert instance != "a string"
|
215
|
-
end
|
216
|
-
|
217
|
-
it "hashes objects the same if they have the same attributes" do
|
218
|
-
klass = Class.new do
|
219
|
-
attr_initialize :foo
|
220
|
-
attr_value :foo
|
221
|
-
end
|
222
|
-
klass2 = Class.new do
|
223
|
-
attr_initialize :foo
|
224
|
-
attr_value :foo
|
225
|
-
end
|
226
|
-
|
227
|
-
example1 = klass.new("Foo")
|
228
|
-
example2 = klass.new("Foo")
|
229
|
-
example3 = klass.new("Bar")
|
230
|
-
example4 = klass2.new("Foo")
|
231
|
-
|
232
|
-
example1.hash.must_equal example2.hash
|
233
|
-
example1.hash.wont_equal example3.hash
|
234
|
-
example1.hash.wont_equal example4.hash
|
235
|
-
|
236
|
-
assert example1.eql?(example2), "Examples should be 'eql?'"
|
237
|
-
refute example1.eql?(example3), "Examples should not be 'eql?'"
|
238
|
-
refute example1.eql?(example4), "Examples should not be 'eql?'"
|
239
|
-
|
240
|
-
Set[example1, example2, example3, example4].length.must_equal 3
|
241
|
-
|
242
|
-
hash = {}
|
243
|
-
hash[example1] = :awyeah
|
244
|
-
hash[example3] = :wat
|
245
|
-
hash[example4] = :nooooo
|
246
|
-
hash[example2].must_equal :awyeah
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
describe Object, ".attr_id_query" do
|
251
|
-
it "creates id query methods" do
|
252
|
-
klass = Class.new do
|
253
|
-
attr_id_query :baz?, :boink?
|
254
|
-
attr_accessor :baz_id
|
255
|
-
end
|
256
|
-
|
257
|
-
example = klass.new
|
258
|
-
refute example.baz?
|
259
|
-
|
260
|
-
example.baz_id = 123
|
261
|
-
assert example.baz?
|
262
|
-
end
|
263
|
-
|
264
|
-
it "requires a trailing questionmark" do
|
265
|
-
lambda { Object.attr_id_query(:foo) }.must_raise ArgumentError
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
describe Object, ".attr_query" do
|
270
|
-
it "creates attribute query methods" do
|
271
|
-
klass = Class.new do
|
272
|
-
attr_query :flurp?
|
273
|
-
attr_accessor :flurp
|
274
|
-
end
|
275
|
-
|
276
|
-
example = klass.new
|
277
|
-
refute example.flurp?
|
278
|
-
example.flurp = "!"
|
279
|
-
assert example.flurp?
|
280
|
-
end
|
281
|
-
|
282
|
-
it "requires a trailing questionmark" do
|
283
|
-
lambda { Object.attr_query(:foo) }.must_raise ArgumentError
|
284
|
-
end
|
285
|
-
end
|
1
|
+
require_relative "spec_helper"
|
286
2
|
|
287
3
|
describe AttrExtras, "in modules" do
|
288
4
|
it "is supported" do
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".attr_id_query" do
|
4
|
+
it "creates id query methods" do
|
5
|
+
klass = Class.new do
|
6
|
+
attr_id_query :baz?, :boink?
|
7
|
+
attr_accessor :baz_id
|
8
|
+
end
|
9
|
+
|
10
|
+
example = klass.new
|
11
|
+
refute example.baz?
|
12
|
+
|
13
|
+
example.baz_id = 123
|
14
|
+
assert example.baz?
|
15
|
+
end
|
16
|
+
|
17
|
+
it "requires a trailing questionmark" do
|
18
|
+
lambda { Object.attr_id_query(:foo) }.must_raise ArgumentError
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".attr_initialize" do
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
attr_initialize :foo, :bar
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "creates an initializer setting those instance variables" do
|
11
|
+
example = klass.new("Foo", "Bar")
|
12
|
+
example.instance_variable_get("@foo").must_equal "Foo"
|
13
|
+
example.instance_variable_get("@bar").must_equal "Bar"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "requires all arguments" do
|
17
|
+
lambda { klass.new("Foo") }.must_raise ArgumentError
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can set ivars from a hash" do
|
21
|
+
klass = Class.new do
|
22
|
+
attr_initialize :foo, [:bar, :baz]
|
23
|
+
end
|
24
|
+
|
25
|
+
example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
26
|
+
example.instance_variable_get("@foo").must_equal "Foo"
|
27
|
+
example.instance_variable_get("@bar").must_equal "Bar"
|
28
|
+
example.instance_variable_get("@baz").must_equal "Baz"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "treats hash values as optional" do
|
32
|
+
klass = Class.new do
|
33
|
+
attr_initialize :foo, [:bar, :baz]
|
34
|
+
end
|
35
|
+
|
36
|
+
example = klass.new("Foo", :bar => "Bar")
|
37
|
+
example.instance_variable_get("@baz").must_equal nil
|
38
|
+
|
39
|
+
example = klass.new("Foo")
|
40
|
+
example.instance_variable_get("@bar").must_equal nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "can require hash values" do
|
44
|
+
klass = Class.new do
|
45
|
+
attr_initialize [:optional, :required!]
|
46
|
+
end
|
47
|
+
|
48
|
+
example = klass.new(:required => "X")
|
49
|
+
example.instance_variable_get("@required").must_equal "X"
|
50
|
+
|
51
|
+
lambda { klass.new(:optional => "X") }.must_raise KeyError
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".attr_private" do
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
attr_private :foo, :bar
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "creates private readers" do
|
11
|
+
example = klass.new
|
12
|
+
example.instance_variable_set("@foo", "Foo")
|
13
|
+
example.instance_variable_set("@bar", "Bar")
|
14
|
+
example.send(:foo).must_equal "Foo"
|
15
|
+
example.send(:bar).must_equal "Bar"
|
16
|
+
lambda { example.foo }.must_raise NoMethodError
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".attr_query" do
|
4
|
+
it "creates attribute query methods" do
|
5
|
+
klass = Class.new do
|
6
|
+
attr_query :flurp?
|
7
|
+
attr_accessor :flurp
|
8
|
+
end
|
9
|
+
|
10
|
+
example = klass.new
|
11
|
+
refute example.flurp?
|
12
|
+
example.flurp = "!"
|
13
|
+
assert example.flurp?
|
14
|
+
end
|
15
|
+
|
16
|
+
it "requires a trailing questionmark" do
|
17
|
+
lambda { Object.attr_query(:foo) }.must_raise ArgumentError
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "set"
|
4
|
+
require_relative "spec_helper"
|
5
|
+
|
6
|
+
describe Object, ".attr_value" do
|
7
|
+
it "creates public readers" do
|
8
|
+
klass = Class.new do
|
9
|
+
attr_value :foo, :bar
|
10
|
+
end
|
11
|
+
|
12
|
+
example = klass.new
|
13
|
+
example.instance_variable_set("@foo", "Foo")
|
14
|
+
example.foo.must_equal "Foo"
|
15
|
+
lambda { example.foo = "new value" }.must_raise NoMethodError
|
16
|
+
end
|
17
|
+
|
18
|
+
it "defines equality based on the attributes" do
|
19
|
+
klass = Class.new do
|
20
|
+
attr_initialize :foo, :bar
|
21
|
+
attr_value :foo, :bar
|
22
|
+
end
|
23
|
+
|
24
|
+
example1 = klass.new("Foo", "Bar")
|
25
|
+
example2 = klass.new("Foo", "Bar")
|
26
|
+
example3 = klass.new("Arroz", "Feijão")
|
27
|
+
|
28
|
+
assert example1 == example2, "Examples should be equal"
|
29
|
+
refute example1 != example2, "Examples should be equal"
|
30
|
+
|
31
|
+
refute example1 == example3, "Examples should not be equal"
|
32
|
+
assert example1 != example3, "Examples should not be equal"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "defines equality based on the actual type" do
|
36
|
+
klass1 = Class.new do
|
37
|
+
attr_initialize :foo
|
38
|
+
attr_value :foo
|
39
|
+
end
|
40
|
+
klass2 = Class.new do
|
41
|
+
attr_initialize :foo
|
42
|
+
attr_value :foo
|
43
|
+
end
|
44
|
+
|
45
|
+
example1 = klass1.new("Foo")
|
46
|
+
example2 = klass2.new("Foo")
|
47
|
+
|
48
|
+
assert example1 != example2, "Examples should not be equal"
|
49
|
+
refute example1 == example2, "Examples should not be equal"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "considers an instance equal to itself" do
|
53
|
+
klass = Class.new do
|
54
|
+
attr_initialize :foo
|
55
|
+
attr_value :foo
|
56
|
+
end
|
57
|
+
|
58
|
+
instance = klass.new("Foo")
|
59
|
+
|
60
|
+
assert instance == instance, "Instance should be equal to itself"
|
61
|
+
end
|
62
|
+
|
63
|
+
it "can compare value objects to other kinds of objects" do
|
64
|
+
klass = Class.new do
|
65
|
+
attr_initialize :foo
|
66
|
+
attr_value :foo
|
67
|
+
end
|
68
|
+
|
69
|
+
instance = klass.new("Foo")
|
70
|
+
|
71
|
+
assert instance != "a string"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "hashes objects the same if they have the same attributes" do
|
75
|
+
klass = Class.new do
|
76
|
+
attr_initialize :foo
|
77
|
+
attr_value :foo
|
78
|
+
end
|
79
|
+
klass2 = Class.new do
|
80
|
+
attr_initialize :foo
|
81
|
+
attr_value :foo
|
82
|
+
end
|
83
|
+
|
84
|
+
example1 = klass.new("Foo")
|
85
|
+
example2 = klass.new("Foo")
|
86
|
+
example3 = klass.new("Bar")
|
87
|
+
example4 = klass2.new("Foo")
|
88
|
+
|
89
|
+
example1.hash.must_equal example2.hash
|
90
|
+
example1.hash.wont_equal example3.hash
|
91
|
+
example1.hash.wont_equal example4.hash
|
92
|
+
|
93
|
+
assert example1.eql?(example2), "Examples should be 'eql?'"
|
94
|
+
refute example1.eql?(example3), "Examples should not be 'eql?'"
|
95
|
+
refute example1.eql?(example4), "Examples should not be 'eql?'"
|
96
|
+
|
97
|
+
Set[example1, example2, example3, example4].length.must_equal 3
|
98
|
+
|
99
|
+
hash = {}
|
100
|
+
hash[example1] = :awyeah
|
101
|
+
hash[example3] = :wat
|
102
|
+
hash[example4] = :nooooo
|
103
|
+
hash[example2].must_equal :awyeah
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".method_object" do
|
4
|
+
it "creates a class method that instantiates and runs that instance method" do
|
5
|
+
klass = Class.new do
|
6
|
+
method_object :fooable?,
|
7
|
+
:foo
|
8
|
+
|
9
|
+
def fooable?
|
10
|
+
foo
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
assert klass.fooable?(true)
|
15
|
+
refute klass.fooable?(false)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "doesn't require attributes" do
|
19
|
+
klass = Class.new do
|
20
|
+
method_object :fooable?
|
21
|
+
|
22
|
+
def fooable?
|
23
|
+
true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
assert klass.fooable?
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".pattr_initialize" do
|
4
|
+
it "creates both initializer and private readers" do
|
5
|
+
klass = Class.new do
|
6
|
+
pattr_initialize :foo, :bar
|
7
|
+
end
|
8
|
+
|
9
|
+
example = klass.new("Foo", "Bar")
|
10
|
+
example.send(:foo).must_equal "Foo"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works with hash ivars" do
|
14
|
+
klass = Class.new do
|
15
|
+
pattr_initialize :foo, [:bar, :baz!]
|
16
|
+
end
|
17
|
+
|
18
|
+
example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
19
|
+
example.send(:baz).must_equal "Baz"
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".vattr_initialize" do
|
4
|
+
it "creates initializer, value readers and value object identity" do
|
5
|
+
klass = Class.new do
|
6
|
+
vattr_initialize :foo, :bar
|
7
|
+
end
|
8
|
+
|
9
|
+
example1 = klass.new("Foo", "Bar")
|
10
|
+
example2 = klass.new("Foo", "Bar")
|
11
|
+
|
12
|
+
example1.foo.must_equal "Foo"
|
13
|
+
example1.must_equal example2
|
14
|
+
end
|
15
|
+
|
16
|
+
it "works with hash ivars" do
|
17
|
+
klass = Class.new do
|
18
|
+
vattr_initialize :foo, [:bar, :baz!]
|
19
|
+
end
|
20
|
+
|
21
|
+
example1 = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
22
|
+
example2 = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
23
|
+
example1.baz.must_equal "Baz"
|
24
|
+
example1.must_equal example2
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -10,20 +10,20 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-03-
|
13
|
+
date: 2014-03-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
|
-
- -
|
26
|
+
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
28
|
version: '0'
|
29
29
|
description:
|
@@ -33,9 +33,9 @@ executables: []
|
|
33
33
|
extensions: []
|
34
34
|
extra_rdoc_files: []
|
35
35
|
files:
|
36
|
-
- .gitignore
|
37
|
-
- .rvmrc
|
38
|
-
- .travis.yml
|
36
|
+
- ".gitignore"
|
37
|
+
- ".rvmrc"
|
38
|
+
- ".travis.yml"
|
39
39
|
- Gemfile
|
40
40
|
- README.md
|
41
41
|
- Rakefile
|
@@ -47,6 +47,15 @@ files:
|
|
47
47
|
- lib/attr_extras/version.rb
|
48
48
|
- script/test
|
49
49
|
- spec/attr_extras_spec.rb
|
50
|
+
- spec/attr_id_query_spec.rb
|
51
|
+
- spec/attr_initialize_spec.rb
|
52
|
+
- spec/attr_private_spec.rb
|
53
|
+
- spec/attr_query_spec.rb
|
54
|
+
- spec/attr_value_spec.rb
|
55
|
+
- spec/method_object_spec.rb
|
56
|
+
- spec/pattr_initialize_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
- spec/vattr_initialize_spec.rb
|
50
59
|
homepage: https://github.com/barsoom/attr_extras
|
51
60
|
licenses:
|
52
61
|
- MIT
|
@@ -57,19 +66,28 @@ require_paths:
|
|
57
66
|
- lib
|
58
67
|
required_ruby_version: !ruby/object:Gem::Requirement
|
59
68
|
requirements:
|
60
|
-
- -
|
69
|
+
- - ">="
|
61
70
|
- !ruby/object:Gem::Version
|
62
71
|
version: '0'
|
63
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
73
|
requirements:
|
65
|
-
- -
|
74
|
+
- - ">="
|
66
75
|
- !ruby/object:Gem::Version
|
67
76
|
version: '0'
|
68
77
|
requirements: []
|
69
78
|
rubyforge_project:
|
70
|
-
rubygems_version: 2.2.
|
79
|
+
rubygems_version: 2.2.2
|
71
80
|
signing_key:
|
72
81
|
specification_version: 4
|
73
82
|
summary: Takes some boilerplate out of Ruby with methods like attr_initialize.
|
74
83
|
test_files:
|
75
84
|
- spec/attr_extras_spec.rb
|
85
|
+
- spec/attr_id_query_spec.rb
|
86
|
+
- spec/attr_initialize_spec.rb
|
87
|
+
- spec/attr_private_spec.rb
|
88
|
+
- spec/attr_query_spec.rb
|
89
|
+
- spec/attr_value_spec.rb
|
90
|
+
- spec/method_object_spec.rb
|
91
|
+
- spec/pattr_initialize_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/vattr_initialize_spec.rb
|