arrayfu 0.0.8 → 0.1.0
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/arrayfu.gemspec +0 -0
- data/lib/arrayfu/array_definition.rb +84 -0
- data/lib/arrayfu/arrayfu.rb +31 -0
- data/lib/arrayfu/generate_mutators.rb +28 -0
- data/lib/arrayfu/{readable_step.rb → generate_readers.rb} +6 -4
- data/lib/arrayfu/generate_visitors.rb +18 -0
- data/lib/arrayfu/{writeable_step.rb → generate_writers.rb} +6 -4
- data/lib/arrayfu/initializer.rb +21 -0
- data/lib/arrayfu/item_constraint.rb +17 -0
- data/lib/arrayfu/module_registry.rb +9 -7
- data/lib/arrayfu/mutator_definition.rb +15 -0
- data/lib/arrayfu/version.rb +1 -1
- data/lib/arrayfu/visitor_definition.rb +19 -0
- data/lib/arrayfu.rb +10 -9
- data/spec/examples/{instance_usage.rb → usage.rb} +169 -72
- data/spec/specs/{dsl_spec.rb → array_definition_spec.rb} +20 -22
- data/spec/specs/dsl_usage_spec.rb +86 -52
- data/spec/specs/{mutator_step_spec.rb → generate_mutators_spec.rb} +5 -5
- data/spec/specs/{readable_step_spec.rb → generate_readers_spec.rb} +3 -3
- data/spec/specs/{visitor_detail_step_spec.rb → generate_visitors_spec.rb} +5 -5
- data/spec/specs/{writeable_step_spec.rb → generate_writers_step_spec.rb} +3 -3
- data/spec/specs/{object_extensions_spec.rb → initializer_spec.rb} +4 -1
- data/spec/specs/{add_criterion_spec.rb → item_constraint_spec.rb} +11 -11
- data/spec/specs/sample.rb +2 -0
- metadata +28 -29
- data/lib/arrayfu/add_criterion.rb +0 -16
- data/lib/arrayfu/dsl.rb +0 -43
- data/lib/arrayfu/mutator_detail.rb +0 -10
- data/lib/arrayfu/mutator_step.rb +0 -18
- data/lib/arrayfu/object_extensions.rb +0 -27
- data/lib/arrayfu/visitor_detail.rb +0 -10
- data/lib/arrayfu/visitor_detail_step.rb +0 -14
- data/spec/examples/class_usage.rb +0 -210
@@ -2,31 +2,39 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
example "Basic" do
|
4
4
|
class SomeClass
|
5
|
-
|
6
|
-
array :names
|
7
|
-
end
|
8
|
-
end
|
5
|
+
include ArrayFu
|
9
6
|
|
10
|
-
|
7
|
+
array :names
|
8
|
+
|
9
|
+
end
|
11
10
|
end
|
12
11
|
|
13
12
|
example 'Allow the array to have a read accessor' do
|
14
13
|
class SomeClass
|
14
|
+
include ArrayFu
|
15
|
+
|
16
|
+
array :names do|a|
|
17
|
+
a.readable
|
18
|
+
end
|
19
|
+
|
15
20
|
def initialize
|
16
|
-
|
17
|
-
a.readable
|
18
|
-
end
|
21
|
+
initialize_arrayfu
|
19
22
|
end
|
23
|
+
|
20
24
|
end
|
21
25
|
SomeClass.new.names.should_not be_nil
|
22
26
|
end
|
23
27
|
|
24
28
|
example 'Allow the array to have a write accessor' do
|
25
29
|
class SomeClass
|
30
|
+
include ArrayFu
|
31
|
+
|
32
|
+
array :names do|a|
|
33
|
+
a.writable
|
34
|
+
end
|
35
|
+
|
26
36
|
def initialize
|
27
|
-
|
28
|
-
a.writable
|
29
|
-
end
|
37
|
+
initialize_arrayfu
|
30
38
|
end
|
31
39
|
end
|
32
40
|
SomeClass.new.names.should_not be_nil
|
@@ -34,10 +42,14 @@ end
|
|
34
42
|
|
35
43
|
example 'Allow the array to have a read and write accessor' do
|
36
44
|
class SomeClass
|
45
|
+
include ArrayFu
|
46
|
+
|
47
|
+
array :names do|a|
|
48
|
+
a.read_and_write
|
49
|
+
end
|
50
|
+
|
37
51
|
def initialize
|
38
|
-
|
39
|
-
a.read_and_write
|
40
|
-
end
|
52
|
+
initialize_arrayfu
|
41
53
|
end
|
42
54
|
end
|
43
55
|
SomeClass.new.names.should_not be_nil
|
@@ -45,10 +57,13 @@ end
|
|
45
57
|
|
46
58
|
example 'Add a mutator method to the class that stores the array' do
|
47
59
|
class SomeClass
|
60
|
+
include ArrayFu
|
61
|
+
|
62
|
+
array :names do|a|
|
63
|
+
a.mutator :add_item
|
64
|
+
end
|
48
65
|
def initialize
|
49
|
-
|
50
|
-
a.mutator :add_item
|
51
|
-
end
|
66
|
+
initialize_arrayfu
|
52
67
|
end
|
53
68
|
end
|
54
69
|
|
@@ -59,11 +74,15 @@ end
|
|
59
74
|
|
60
75
|
example 'Add multiple mutators to the class that stores the array' do
|
61
76
|
class SomeClass
|
77
|
+
include ArrayFu
|
78
|
+
|
79
|
+
array :names do|a|
|
80
|
+
a.mutator :add_item, :add_it, :push_it
|
81
|
+
end
|
62
82
|
def initialize
|
63
|
-
|
64
|
-
a.mutator :add_item,:add_it,:push_it
|
65
|
-
end
|
83
|
+
initialize_arrayfu
|
66
84
|
end
|
85
|
+
|
67
86
|
end
|
68
87
|
|
69
88
|
items = SomeClass.new
|
@@ -75,12 +94,16 @@ end
|
|
75
94
|
|
76
95
|
example 'Add a mutator that ignores addition' do
|
77
96
|
class SomeClass
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
97
|
+
include ArrayFu
|
98
|
+
|
99
|
+
array :names do|a|
|
100
|
+
a.mutator :add_item do|item|
|
82
101
|
end
|
83
102
|
end
|
103
|
+
def initialize
|
104
|
+
initialize_arrayfu
|
105
|
+
end
|
106
|
+
|
84
107
|
end
|
85
108
|
|
86
109
|
items = SomeClass.new
|
@@ -90,15 +113,23 @@ end
|
|
90
113
|
|
91
114
|
example 'Add a mutator that does other custom logic as well as addition' do
|
92
115
|
class SomeClass
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
116
|
+
include ArrayFu
|
117
|
+
|
118
|
+
array :secondary do |a|
|
119
|
+
a.readable
|
120
|
+
end
|
121
|
+
|
122
|
+
array :names do|a|
|
123
|
+
a.readable
|
124
|
+
a.mutator :add_item do|item|
|
125
|
+
@secondary.push item
|
126
|
+
@names.push item
|
100
127
|
end
|
101
128
|
end
|
129
|
+
|
130
|
+
def initialize
|
131
|
+
initialize_arrayfu
|
132
|
+
end
|
102
133
|
end
|
103
134
|
|
104
135
|
items = SomeClass.new
|
@@ -108,28 +139,36 @@ example 'Add a mutator that does other custom logic as well as addition' do
|
|
108
139
|
end
|
109
140
|
|
110
141
|
example 'Add a singular constraint and failure condition to each of the mutators' do
|
111
|
-
|
112
|
-
|
113
|
-
|
142
|
+
module NotBeJP
|
143
|
+
extend self
|
144
|
+
|
145
|
+
def matches?(item)
|
114
146
|
return item != "JP"
|
115
147
|
end
|
148
|
+
|
116
149
|
def name
|
117
150
|
return "The value should not be JP"
|
118
151
|
end
|
119
152
|
end
|
120
|
-
|
121
|
-
|
153
|
+
|
154
|
+
module CriteriaViolation
|
155
|
+
extend self
|
156
|
+
|
122
157
|
def run(description,value)
|
123
158
|
|
124
159
|
end
|
125
160
|
end
|
126
161
|
|
127
162
|
class SomeClass
|
163
|
+
include ArrayFu
|
164
|
+
|
165
|
+
array :names do|a|
|
166
|
+
a.mutator :add_item,:add_it
|
167
|
+
a.new_item_must NotBeJP, CriteriaViolation
|
168
|
+
end
|
169
|
+
|
128
170
|
def initialize
|
129
|
-
|
130
|
-
a.mutator :add_item,:add_it
|
131
|
-
a.new_item_must NotBeJP.instance,CriteriaViolation.instance
|
132
|
-
end
|
171
|
+
initialize_arrayfu
|
133
172
|
end
|
134
173
|
end
|
135
174
|
|
@@ -140,38 +179,49 @@ example 'Add a singular constraint and failure condition to each of the mutators
|
|
140
179
|
end
|
141
180
|
|
142
181
|
example 'Add multiple constraints and a failure condition to each of the mutators' do
|
143
|
-
|
144
|
-
|
145
|
-
|
182
|
+
module NotBeJP
|
183
|
+
extend self
|
184
|
+
|
185
|
+
def matches?(item)
|
146
186
|
return item != "JP"
|
147
187
|
end
|
188
|
+
|
148
189
|
def name
|
149
190
|
return "The value should not be JP"
|
150
191
|
end
|
151
192
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
193
|
+
|
194
|
+
module NotBeNil
|
195
|
+
extend self
|
196
|
+
|
197
|
+
def matches?(item)
|
155
198
|
return item != nil
|
156
199
|
end
|
200
|
+
|
157
201
|
def name
|
158
202
|
return "The value should not be nil"
|
159
203
|
end
|
160
204
|
end
|
161
|
-
class CriteriaViolation
|
162
|
-
include Singleton
|
163
|
-
def run(description,value)
|
164
205
|
|
206
|
+
module CriteriaViolation
|
207
|
+
extend self
|
208
|
+
|
209
|
+
def run(description,value)
|
210
|
+
# puts "Criteria violated - #{description} - #{value}"
|
165
211
|
end
|
166
212
|
end
|
167
213
|
|
168
214
|
class SomeClass
|
215
|
+
include ArrayFu
|
216
|
+
|
217
|
+
array :names do|a|
|
218
|
+
a.mutator :add_item,:add_it
|
219
|
+
a.addition_constraint NotBeJP
|
220
|
+
a.addition_constraint NotBeNil, CriteriaViolation
|
221
|
+
end
|
222
|
+
|
169
223
|
def initialize
|
170
|
-
|
171
|
-
a.mutator :add_item,:add_it
|
172
|
-
a.new_item_must NotBeJP.instance,CriteriaViolation.instance
|
173
|
-
a.new_item_must NotBeNil.instance,CriteriaViolation.instance
|
174
|
-
end
|
224
|
+
initialize_arrayfu
|
175
225
|
end
|
176
226
|
end
|
177
227
|
|
@@ -196,12 +246,16 @@ example 'Add an explicit processing visitor to the array' do
|
|
196
246
|
end
|
197
247
|
|
198
248
|
class SomeClass
|
249
|
+
include ArrayFu
|
250
|
+
|
251
|
+
array :names do|a|
|
252
|
+
a.mutator :add_item
|
253
|
+
a.process_using :display_all,DisplayItem
|
254
|
+
end
|
199
255
|
def initialize
|
200
|
-
|
201
|
-
a.mutator :add_item
|
202
|
-
a.process_using :display_all,DisplayItem
|
203
|
-
end
|
256
|
+
initialize_arrayfu
|
204
257
|
end
|
258
|
+
|
205
259
|
end
|
206
260
|
|
207
261
|
items = SomeClass.new
|
@@ -219,14 +273,14 @@ example 'Add an method based processing visitor to the array based on a method t
|
|
219
273
|
|
220
274
|
class SomeClass
|
221
275
|
@@items_visited = 0
|
276
|
+
include ArrayFu
|
222
277
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
a.process_using :display_all,:process #the second symbol is the name of a method on an element in the array
|
227
|
-
end
|
278
|
+
array :names do|a|
|
279
|
+
a.mutator :add_item
|
280
|
+
a.process_using :display_all, :process #the second symbol is the name of a method on an element in the array
|
228
281
|
end
|
229
282
|
|
283
|
+
|
230
284
|
#the process method of the Item class invokes this method (a little bit roundabout, but it hopefully demonstrates the capability
|
231
285
|
def self.increment
|
232
286
|
@@items_visited += 1
|
@@ -234,6 +288,9 @@ example 'Add an method based processing visitor to the array based on a method t
|
|
234
288
|
def self.number_of_items_visited
|
235
289
|
@@items_visited
|
236
290
|
end
|
291
|
+
def initialize
|
292
|
+
initialize_arrayfu
|
293
|
+
end
|
237
294
|
end
|
238
295
|
|
239
296
|
items = SomeClass.new
|
@@ -250,12 +307,15 @@ example 'Augment configuration using configuration block' do
|
|
250
307
|
end
|
251
308
|
|
252
309
|
class SomeClass
|
310
|
+
include ArrayFu
|
311
|
+
|
312
|
+
array :names do|a|
|
313
|
+
a.mutator :add_item
|
314
|
+
a.configure_using ArrayConfigs.add_another_mutator
|
315
|
+
end
|
253
316
|
|
254
317
|
def initialize
|
255
|
-
|
256
|
-
a.mutator :add_item
|
257
|
-
a.configure_using ArrayConfigs.add_another_mutator
|
258
|
-
end
|
318
|
+
initialize_arrayfu
|
259
319
|
end
|
260
320
|
end
|
261
321
|
|
@@ -265,23 +325,60 @@ example 'Augment configuration using configuration block' do
|
|
265
325
|
items.names.count.should == 2
|
266
326
|
end
|
267
327
|
|
268
|
-
example 'Augment configuration using configuration instance' do
|
269
|
-
|
328
|
+
example 'Augment configuration using configuration instance (anything that responds to configure with the array definition as the argument)' do
|
329
|
+
|
330
|
+
module ArrayConfiguration
|
331
|
+
extend self
|
332
|
+
|
270
333
|
def configure(item)
|
271
334
|
item.mutator :once_more
|
272
335
|
end
|
273
336
|
end
|
274
337
|
|
275
338
|
class SomeClass
|
339
|
+
include ArrayFu
|
340
|
+
|
341
|
+
array :names do|a|
|
342
|
+
a.mutator :add_item
|
343
|
+
a.configure_using ArrayConfiguration
|
344
|
+
end
|
276
345
|
|
277
346
|
def initialize
|
278
|
-
|
279
|
-
|
280
|
-
|
347
|
+
initialize_arrayfu
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
items = SomeClass.new
|
352
|
+
items.add_item("Yo")
|
353
|
+
items.once_more("Yo")
|
354
|
+
items.names.count.should == 2
|
355
|
+
end
|
356
|
+
|
357
|
+
example 'Augment configuration using configuration block' do
|
358
|
+
|
359
|
+
module ArrayConfiguration
|
360
|
+
extend self
|
361
|
+
|
362
|
+
def configuration_block
|
363
|
+
Proc.new do|array|
|
364
|
+
array.mutator :once_more
|
281
365
|
end
|
282
366
|
end
|
283
367
|
end
|
284
368
|
|
369
|
+
class SomeClass
|
370
|
+
include ArrayFu
|
371
|
+
|
372
|
+
array :names do|a|
|
373
|
+
a.mutator :add_item
|
374
|
+
a.configure_using ArrayConfiguration.configuration_block
|
375
|
+
end
|
376
|
+
def initialize
|
377
|
+
initialize_arrayfu
|
378
|
+
end
|
379
|
+
|
380
|
+
end
|
381
|
+
|
285
382
|
items = SomeClass.new
|
286
383
|
items.add_item("Yo")
|
287
384
|
items.once_more("Yo")
|
@@ -1,29 +1,29 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module ArrayFu
|
4
|
-
describe
|
5
|
-
context "when a
|
4
|
+
describe ArrayDefinition do
|
5
|
+
context "when a constraint is specified" do
|
6
6
|
let(:fail_option){fake}
|
7
|
-
let(:
|
8
|
-
let(:
|
9
|
-
let(:sut){
|
7
|
+
let(:real_constraint){fake}
|
8
|
+
let(:constraint){fake}
|
9
|
+
let(:sut){ArrayDefinition.new(:name)}
|
10
10
|
before (:each) do
|
11
|
-
|
11
|
+
ItemConstraint.stub(:new).with(real_constraint,fail_option).and_return(constraint)
|
12
12
|
end
|
13
13
|
before (:each) do
|
14
|
-
sut.new_item_must(
|
14
|
+
sut.new_item_must(real_constraint,fail_option)
|
15
15
|
end
|
16
16
|
it "should be added to the list of add specifications for the array" do
|
17
|
-
sut.
|
17
|
+
sut.constraints[0].should == constraint
|
18
18
|
end
|
19
19
|
end
|
20
20
|
context "when specifying mutators" do
|
21
21
|
context "and a singular mutator is specified" do
|
22
22
|
let(:mutator){fake}
|
23
23
|
let(:name){"sdfsdf"}
|
24
|
-
let(:sut){
|
24
|
+
let(:sut){ArrayDefinition.new("sdf")}
|
25
25
|
before (:each) do
|
26
|
-
|
26
|
+
MutatorDefinition.stub(:new).with(name,nil).and_return(mutator)
|
27
27
|
end
|
28
28
|
before (:each) do
|
29
29
|
sut.mutator(name)
|
@@ -34,10 +34,10 @@ module ArrayFu
|
|
34
34
|
end
|
35
35
|
context "and a set of mutators are specified" do
|
36
36
|
let(:mutator){fake}
|
37
|
-
let(:sut){
|
37
|
+
let(:sut){ArrayDefinition.new("sdf")}
|
38
38
|
before (:each) do
|
39
|
-
|
40
|
-
|
39
|
+
MutatorDefinition.stub(:new).with(:sdf,nil).and_return(mutator)
|
40
|
+
MutatorDefinition.stub(:new).with(:other,nil).and_return(mutator)
|
41
41
|
end
|
42
42
|
before (:each) do
|
43
43
|
sut.mutator(:sdf,:other)
|
@@ -52,9 +52,9 @@ module ArrayFu
|
|
52
52
|
let(:visitor){fake}
|
53
53
|
let(:the_visitor){fake}
|
54
54
|
let(:name){"sdfsdf"}
|
55
|
-
let(:sut){
|
55
|
+
let(:sut){ArrayDefinition.new("sdf")}
|
56
56
|
before (:each) do
|
57
|
-
|
57
|
+
VisitorDefinition.stub(:new).with(name,the_visitor).and_return(visitor)
|
58
58
|
end
|
59
59
|
before (:each) do
|
60
60
|
sut.process_using(name,the_visitor)
|
@@ -68,7 +68,7 @@ module ArrayFu
|
|
68
68
|
context "and they are explicit configurators" do
|
69
69
|
let(:configurator1){fake}
|
70
70
|
let(:configurator2){fake}
|
71
|
-
subject{
|
71
|
+
subject{ArrayDefinition.new('name')}
|
72
72
|
|
73
73
|
before (:each) do
|
74
74
|
def configurator1.respond_to?(name) true end
|
@@ -79,7 +79,7 @@ module ArrayFu
|
|
79
79
|
subject.configure_using configurator1,configurator2
|
80
80
|
end
|
81
81
|
|
82
|
-
it "should invoke the configurator with the
|
82
|
+
it "should invoke the configurator with the definition" do
|
83
83
|
configurator1.should have_received(:configure,subject)
|
84
84
|
configurator2.should have_received(:configure,subject)
|
85
85
|
end
|
@@ -88,7 +88,7 @@ module ArrayFu
|
|
88
88
|
context "and they are a set of blocks" do
|
89
89
|
let(:configurator1){fake}
|
90
90
|
let(:configurator2){fake}
|
91
|
-
subject{
|
91
|
+
subject{ArrayDefinition.new('name')}
|
92
92
|
before (:each) do
|
93
93
|
@first_ran = false
|
94
94
|
@second_ran = false
|
@@ -98,7 +98,7 @@ module ArrayFu
|
|
98
98
|
subject.configure_using lambda{|item| item.should == subject;@first_ran = true},lambda{|item| item.should == subject;@second_ran = true}
|
99
99
|
end
|
100
100
|
|
101
|
-
it "should invoke each block with the
|
101
|
+
it "should invoke each block with the definition" do
|
102
102
|
@first_ran.should be_true
|
103
103
|
@second_ran.should be_true
|
104
104
|
end
|
@@ -106,7 +106,7 @@ module ArrayFu
|
|
106
106
|
|
107
107
|
context "and they are a mixture of blocks and explicit configurators" do
|
108
108
|
let(:configurator1){fake}
|
109
|
-
subject{
|
109
|
+
subject{ArrayDefinition.new('name')}
|
110
110
|
before (:each) do
|
111
111
|
@first_ran = false
|
112
112
|
def configurator1.respond_to?(name) true end
|
@@ -121,8 +121,6 @@ module ArrayFu
|
|
121
121
|
configurator1.should have_received(:configure,subject)
|
122
122
|
end
|
123
123
|
end
|
124
|
-
|
125
124
|
end
|
126
|
-
|
127
125
|
end
|
128
126
|
end
|