arrayfu 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/Rakefile +1 -0
- data/arrayfu.gemspec +26 -0
- data/bin/dwp_expand +4 -0
- data/lib/arrayfu.rb +10 -0
- data/lib/core/add_criterion.rb +16 -0
- data/lib/core/dsl.rb +36 -0
- data/lib/core/module_registry.rb +12 -0
- data/lib/core/mutator_detail.rb +10 -0
- data/lib/core/mutator_step.rb +18 -0
- data/lib/core/object_extensions.rb +27 -0
- data/lib/core/readable_step.rb +13 -0
- data/lib/core/version.rb +3 -0
- data/lib/core/visitor_detail.rb +10 -0
- data/lib/core/visitor_detail_step.rb +14 -0
- data/lib/core/writeable_step.rb +13 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/specs/add_criterion_spec.rb +42 -0
- data/spec/specs/dsl_spec.rb +284 -0
- data/spec/specs/mutator_step_spec.rb +45 -0
- data/spec/specs/object_extensions_spec.rb +55 -0
- data/spec/specs/readable_step_spec.rb +24 -0
- data/spec/specs/sample.rb +5 -0
- data/spec/specs/visitor_detail_step_spec.rb +76 -0
- data/spec/specs/writeable_step_spec.rb +28 -0
- metadata +147 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.3@arrayfu --create
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/arrayfu.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib",__FILE__)
|
3
|
+
require "core/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "arrayfu"
|
7
|
+
s.version = ArrayFu::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Develop With Passion®"]
|
10
|
+
s.email = ["open_source@developwithpassion.com"]
|
11
|
+
s.homepage = "http://www.developwithpassion.com"
|
12
|
+
s.summary = %q{Simple DSL For Declaritive Arrays}
|
13
|
+
s.description = %q{Simple DSL For Declaritive Arrays}
|
14
|
+
s.rubyforge_project = "arrayfu"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "guard"
|
24
|
+
s.add_development_dependency "guard-rspec"
|
25
|
+
s.add_development_dependency "fakes-rspec"
|
26
|
+
end
|
data/bin/dwp_expand
ADDED
data/lib/arrayfu.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'core/add_criterion'
|
2
|
+
require 'core/dsl'
|
3
|
+
require 'core/module_registry'
|
4
|
+
require 'core/mutator_detail'
|
5
|
+
require 'core/mutator_step'
|
6
|
+
require 'core/object_extensions'
|
7
|
+
require 'core/readable_step'
|
8
|
+
require 'core/visitor_detail'
|
9
|
+
require 'core/visitor_detail_step'
|
10
|
+
require 'core/writeable_step'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
class AddCriterion
|
3
|
+
attr_accessor :criteria,:failure_strategy
|
4
|
+
|
5
|
+
def initialize(criteria,failure_strategy)
|
6
|
+
@criteria = criteria
|
7
|
+
@failure_strategy = failure_strategy
|
8
|
+
end
|
9
|
+
|
10
|
+
def apply_to(value)
|
11
|
+
result = @criteria.is_satisfied_by(value)
|
12
|
+
@failure_strategy.run(@criteria.name,value) unless result
|
13
|
+
return result
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/core/dsl.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
class Dsl
|
3
|
+
attr_accessor :mutators,:visitors,:criteria,:name,:writable,:readable
|
4
|
+
|
5
|
+
def initialize(name)
|
6
|
+
@name = name
|
7
|
+
initialize_arrays :mutators,:visitors,:criteria
|
8
|
+
initialize_false :writable,:readable
|
9
|
+
end
|
10
|
+
|
11
|
+
def mutator(*names,&block)
|
12
|
+
names.each{|name| @mutators.push(MutatorDetail.new(name,block))}
|
13
|
+
end
|
14
|
+
|
15
|
+
def new_item_must(criteria,fail_option)
|
16
|
+
@criteria.push(AddCriterion.new(criteria,fail_option))
|
17
|
+
end
|
18
|
+
|
19
|
+
def process_using(name,visitor)
|
20
|
+
@visitors.push(VisitorDetail.new(name,visitor))
|
21
|
+
end
|
22
|
+
|
23
|
+
def read_and_write
|
24
|
+
writable
|
25
|
+
readable
|
26
|
+
end
|
27
|
+
|
28
|
+
def writable
|
29
|
+
@writable = true
|
30
|
+
end
|
31
|
+
|
32
|
+
def readable
|
33
|
+
@readable = true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
class MutatorStep
|
3
|
+
def run_using(builder)
|
4
|
+
Module.new do
|
5
|
+
builder.mutators.each do|mutator|
|
6
|
+
define_method(mutator.name) do|value|
|
7
|
+
array_var = instance_variable_get("@#{builder.name}")
|
8
|
+
continue_add = true
|
9
|
+
builder.criteria.each{|criteria| continue_add &= criteria.apply_to(value)}
|
10
|
+
return unless continue_add
|
11
|
+
array_var.push(value) unless mutator.block
|
12
|
+
mutator.block.call(value) if mutator.block
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Object
|
2
|
+
def initialize_defaults(factory,*items)
|
3
|
+
items.each do|item|
|
4
|
+
instance_variable_set("@#{item}",factory.call)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize_arrays(*items)
|
9
|
+
initialize_defaults(lambda{[]},*items)
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize_hashes(*items)
|
13
|
+
initialize_defaults(lambda{{}},*items)
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize_false(*items)
|
17
|
+
initialize_defaults(lambda{false},*items)
|
18
|
+
end
|
19
|
+
|
20
|
+
def array(name,&block)
|
21
|
+
initialize_arrays(name)
|
22
|
+
dsl = DevelopWithPassion::Arrays::Dsl.new(name)
|
23
|
+
yield dsl if block_given?
|
24
|
+
DevelopWithPassion::Arrays::ModuleRegistry.all_modules.each{|module_factory| self.extend(module_factory.run_using(dsl))}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/core/version.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
class VisitorDetailStep
|
3
|
+
def run_using(builder)
|
4
|
+
Module.new do
|
5
|
+
builder.visitors.each do|visitor|
|
6
|
+
define_method(visitor.name) do
|
7
|
+
array_var = instance_variable_get("@#{builder.name}")
|
8
|
+
array_var.each{|item| visitor.visitor.respond_to?(:run_using) ? visitor.visitor.run_using(item) : item.send(visitor.visitor)}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'fakes-rspec'
|
3
|
+
require 'singleton'
|
4
|
+
|
5
|
+
Dir.chdir(File.join(File.dirname(__FILE__),"..,lib".split(','))) do
|
6
|
+
require 'arrayfu.rb'
|
7
|
+
end
|
8
|
+
|
9
|
+
def catch_exception
|
10
|
+
begin
|
11
|
+
yield
|
12
|
+
rescue Exception => e
|
13
|
+
exception = e
|
14
|
+
end
|
15
|
+
exception
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ArrayFu
|
4
|
+
describe AddCriterion do
|
5
|
+
context "when applied to a value" do
|
6
|
+
let(:failure_strategy){fake}
|
7
|
+
let(:criteria){fake}
|
8
|
+
let(:value){42}
|
9
|
+
let(:name){"Name of criteria"}
|
10
|
+
let(:sut){AddCriterion.new(criteria,failure_strategy)}
|
11
|
+
|
12
|
+
context "and the value does not match the criteria" do
|
13
|
+
before (:each) do
|
14
|
+
criteria.stub(:is_satisfied_by).with(value).and_return(false)
|
15
|
+
criteria.stub(:name).and_return(name)
|
16
|
+
end
|
17
|
+
before (:each) do
|
18
|
+
sut.apply_to(value)
|
19
|
+
end
|
20
|
+
it "should run the failure strategy with the correct information" do
|
21
|
+
failure_strategy.should have_received(:run,name,value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "and the value matches the criteria" do
|
26
|
+
before (:each) do
|
27
|
+
criteria.stub(:is_satisfied_by).with(value).and_return(true)
|
28
|
+
criteria.stub(:name).and_return(name)
|
29
|
+
end
|
30
|
+
before (:each) do
|
31
|
+
@result = sut.apply_to(value)
|
32
|
+
end
|
33
|
+
it "should not use the failure strategy" do
|
34
|
+
failure_strategy.should_not have_received(:run)
|
35
|
+
end
|
36
|
+
it "should return true" do
|
37
|
+
@result.should be_true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,284 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ArrayFu
|
4
|
+
describe Dsl do
|
5
|
+
context "when a criteria is specified" do
|
6
|
+
let(:fail_option){fake}
|
7
|
+
let(:real_criteria){fake}
|
8
|
+
let(:criteria){fake}
|
9
|
+
let(:sut){Dsl.new(:name)}
|
10
|
+
before (:each) do
|
11
|
+
AddCriterion.stub(:new).with(real_criteria,fail_option).and_return(criteria)
|
12
|
+
end
|
13
|
+
before (:each) do
|
14
|
+
sut.new_item_must(real_criteria,fail_option)
|
15
|
+
end
|
16
|
+
it "should be added to the list of add specifications for the array" do
|
17
|
+
sut.criteria[0].should == criteria
|
18
|
+
end
|
19
|
+
end
|
20
|
+
context "when specifying mutators" do
|
21
|
+
context "and a singular mutator is specified" do
|
22
|
+
let(:mutator){fake}
|
23
|
+
let(:name){"sdfsdf"}
|
24
|
+
let(:sut){Dsl.new("sdf")}
|
25
|
+
before (:each) do
|
26
|
+
MutatorDetail.stub(:new).with(name,nil).and_return(mutator)
|
27
|
+
end
|
28
|
+
before (:each) do
|
29
|
+
sut.mutator(name)
|
30
|
+
end
|
31
|
+
it "should be added to the list of mutators for the list" do
|
32
|
+
sut.mutators[0].should == mutator
|
33
|
+
end
|
34
|
+
end
|
35
|
+
context "and a set of mutators are specified" do
|
36
|
+
let(:mutator){fake}
|
37
|
+
let(:sut){Dsl.new("sdf")}
|
38
|
+
before (:each) do
|
39
|
+
MutatorDetail.stub(:new).with(:sdf,nil).and_return(mutator)
|
40
|
+
MutatorDetail.stub(:new).with(:other,nil).and_return(mutator)
|
41
|
+
end
|
42
|
+
before (:each) do
|
43
|
+
sut.mutator(:sdf,:other)
|
44
|
+
end
|
45
|
+
it "should add a new mutator for each name specified" do
|
46
|
+
sut.mutators.count.should == 2
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when a visitor is specified" do
|
52
|
+
let(:visitor){fake}
|
53
|
+
let(:the_visitor){fake}
|
54
|
+
let(:name){"sdfsdf"}
|
55
|
+
let(:sut){Dsl.new("sdf")}
|
56
|
+
before (:each) do
|
57
|
+
VisitorDetail.stub(:new).with(name,the_visitor).and_return(visitor)
|
58
|
+
end
|
59
|
+
before (:each) do
|
60
|
+
sut.process_using(name,the_visitor)
|
61
|
+
end
|
62
|
+
it "should be added to the list of visitors for the list" do
|
63
|
+
sut.visitors[0].should == visitor
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "using the dsl" do
|
68
|
+
it "should be able to initialize all arrays specified on the instance and provide a method to expose addition" do
|
69
|
+
class Item
|
70
|
+
|
71
|
+
def initialize
|
72
|
+
array :kids do|a|
|
73
|
+
a.mutator :register_child
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
item = Item.new
|
79
|
+
item.methods.include?(:register_child).should be_true
|
80
|
+
item.register_child("hello")
|
81
|
+
item.kids.count.should == 1
|
82
|
+
end
|
83
|
+
it "should be able to expose a mutator with custom logic" do
|
84
|
+
class Item
|
85
|
+
attr_accessor :added,:item_added
|
86
|
+
|
87
|
+
def initialize
|
88
|
+
@added = 0
|
89
|
+
|
90
|
+
array :kids do|a|
|
91
|
+
a.mutator :register_child do|the_item|
|
92
|
+
@item_added = the_item
|
93
|
+
@added +=1
|
94
|
+
@kids.push(the_item)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
item = Item.new
|
101
|
+
item.register_child("hello")
|
102
|
+
item.kids.count.should == 1
|
103
|
+
item.item_added.should == "hello"
|
104
|
+
item.added.should == 1
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should be able to expose a processing visitor" do
|
108
|
+
class Item
|
109
|
+
attr_accessor :added
|
110
|
+
|
111
|
+
def initialize(visitor)
|
112
|
+
@added = 0
|
113
|
+
array :kids do|a|
|
114
|
+
a.mutator :register_child
|
115
|
+
a.process_using :register_kids,visitor
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class OurVisitor
|
121
|
+
attr_accessor :items
|
122
|
+
def initialize
|
123
|
+
@items = 0
|
124
|
+
end
|
125
|
+
def run_using(item)
|
126
|
+
@items +=1
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
our_visitor = OurVisitor.new
|
131
|
+
item = Item.new(our_visitor)
|
132
|
+
item.register_child("hello")
|
133
|
+
item.kids.count.should == 1
|
134
|
+
item.register_kids
|
135
|
+
our_visitor.items.should == 1
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should be able to expose a processing visitor by symbol" do
|
139
|
+
class Item
|
140
|
+
attr_accessor :added
|
141
|
+
|
142
|
+
def initialize
|
143
|
+
@added = 0
|
144
|
+
array :kids do|a|
|
145
|
+
a.mutator :register_child
|
146
|
+
a.process_using :register_kids,:speak
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class Kid
|
152
|
+
def initialize(array)
|
153
|
+
@array = array
|
154
|
+
end
|
155
|
+
def speak
|
156
|
+
@array.push("spoke")
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
items = []
|
161
|
+
item = Item.new
|
162
|
+
item.register_child(Kid.new(items))
|
163
|
+
item.kids.count.should == 1
|
164
|
+
item.register_kids
|
165
|
+
items.count.should == 1
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context "when specifying a mutator" do
|
170
|
+
context "and no block is provided" do
|
171
|
+
it "should expose the specified method to trigger addition" do
|
172
|
+
class Item
|
173
|
+
|
174
|
+
def initialize
|
175
|
+
array :kids do|a|
|
176
|
+
a.mutator :register_child
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
item = Item.new
|
182
|
+
item.methods.include?(:register_child).should be_true
|
183
|
+
item.register_child("hello")
|
184
|
+
item.kids.count.should == 1
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context "and a block is provided" do
|
189
|
+
it "should provide a method that delegates to the block when invoked" do
|
190
|
+
class Item
|
191
|
+
attr_accessor :added
|
192
|
+
|
193
|
+
def initialize
|
194
|
+
@added = 0
|
195
|
+
|
196
|
+
array :kids do|a|
|
197
|
+
a.mutator :register_child do|item|
|
198
|
+
@kids.push(item)
|
199
|
+
@added+=1
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
item = Item.new
|
206
|
+
item.register_child("hello")
|
207
|
+
item.kids.count.should == 1
|
208
|
+
item.added.should == 1
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
context "when criterias have been specified on the array" do
|
214
|
+
class BeGreaterThanZero
|
215
|
+
def name
|
216
|
+
return "Be greater than 0"
|
217
|
+
end
|
218
|
+
def is_satisfied_by(item)
|
219
|
+
return item > 0
|
220
|
+
end
|
221
|
+
end
|
222
|
+
context "and the criteria is not met" do
|
223
|
+
context "and the failure strategy is set to raise an error" do
|
224
|
+
class RaiseCriteriaFailure
|
225
|
+
def run(name,value)
|
226
|
+
raise "The value #{value} does not meet the criteria #{name}"
|
227
|
+
end
|
228
|
+
end
|
229
|
+
class OneClass
|
230
|
+
def initialize
|
231
|
+
array :items do|a|
|
232
|
+
a.mutator :add_item,:add_this,:add_that
|
233
|
+
a.new_item_must BeGreaterThanZero.new, RaiseCriteriaFailure.new
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
let(:target){OneClass.new}
|
238
|
+
before (:each) do
|
239
|
+
@exception = catch_exception do
|
240
|
+
target.add_item(0)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should raise an error when a failed add is attempted" do
|
245
|
+
(@exception.message =~ /does not meet/).should be_true
|
246
|
+
end
|
247
|
+
it "should not add the item to the underlying list" do
|
248
|
+
target.items.count.should == 0
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
context "and the failure strategy is not set to raise an error" do
|
253
|
+
class DisplayCriteriaFailure
|
254
|
+
include Singleton
|
255
|
+
attr_accessor :message
|
256
|
+
def run(name,value)
|
257
|
+
@message = "The value #{value} does not meet the criteria #{name}"
|
258
|
+
end
|
259
|
+
end
|
260
|
+
class AnotherClass
|
261
|
+
def initialize
|
262
|
+
array :items do|a|
|
263
|
+
a.mutator :add_item,:add_this,:add_that
|
264
|
+
a.new_item_must BeGreaterThanZero.new, DisplayCriteriaFailure.instance
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
let(:target){AnotherClass.new}
|
269
|
+
before (:each) do
|
270
|
+
target.add_item(0)
|
271
|
+
end
|
272
|
+
it "should have leveraged the failure strategy that does not throw an exception" do
|
273
|
+
DisplayCriteriaFailure.instance.message.should_not be_nil
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should not add the item to the underlying list" do
|
277
|
+
target.items.count.should == 0
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ArrayFu
|
4
|
+
describe MutatorStep do
|
5
|
+
context "when run" do
|
6
|
+
let(:numbers){[]}
|
7
|
+
let(:target){Sample.new(numbers)}
|
8
|
+
let(:mutators){[]}
|
9
|
+
let(:sut){MutatorStep.new}
|
10
|
+
let(:builder){Dsl.new(:numbers)}
|
11
|
+
|
12
|
+
context "using a dsl fragment that contains no block usage" do
|
13
|
+
before (:each) do
|
14
|
+
builder.mutator(:add_a_number)
|
15
|
+
end
|
16
|
+
before (:each) do
|
17
|
+
target.extend(sut.run_using(builder))
|
18
|
+
end
|
19
|
+
it "should create a member on the target that allows mutation of the original list" do
|
20
|
+
target.respond_to?(:add_a_number).should be_true
|
21
|
+
target.add_a_number(2)
|
22
|
+
numbers[0].should == 2
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context "using a dsl fragment that contains block usage" do
|
26
|
+
before (:each) do
|
27
|
+
builder.mutator(:add_a_number) do|value|
|
28
|
+
@value_attempted_to_be_added = value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
before (:each) do
|
32
|
+
target.extend(sut.run_using(builder))
|
33
|
+
end
|
34
|
+
it "should create a member on the target that intercepts mutation using the provided block" do
|
35
|
+
target.respond_to?(:add_a_number).should be_true
|
36
|
+
target.add_a_number(2)
|
37
|
+
numbers.count.should == 0
|
38
|
+
@value_attempted_to_be_added.should == 2
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Object do
|
4
|
+
context "when initializing fields with defaults" do
|
5
|
+
it "should initialize based on the factory provided" do
|
6
|
+
class Item
|
7
|
+
attr_accessor :kids,:cars,:name
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
initialize_defaults lambda{[]},:kids,:cars
|
11
|
+
initialize_defaults lambda{""},:name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
item = Item.new
|
16
|
+
[item.kids,item.cars].each{|value| value.class.should == Array}
|
17
|
+
item.name.class.should == String
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when initializing simple arrays" do
|
22
|
+
it "should initialize all arrays specified on the instance" do
|
23
|
+
class Item
|
24
|
+
attr_accessor :kids,:cars,:bikes
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
initialize_arrays :kids,:cars,:bikes
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
item = Item.new
|
32
|
+
[item.kids,item.cars,item.bikes].each do|value|
|
33
|
+
value.class.should == Array
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "when initializing hashes" do
|
39
|
+
it "should initialize all hashes specified" do
|
40
|
+
class Item
|
41
|
+
attr_accessor :kids,:cars,:bikes
|
42
|
+
|
43
|
+
def initialize
|
44
|
+
initialize_hashes :kids,:cars,:bikes
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
item = Item.new
|
49
|
+
[item.kids,item.cars,item.bikes].each do|value|
|
50
|
+
value.class.should == Hash
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ArrayFu
|
4
|
+
describe ReadableStep do
|
5
|
+
context "when run" do
|
6
|
+
let(:target){Sample.new}
|
7
|
+
let(:mutators){[]}
|
8
|
+
let(:sut){ReadableStep.new}
|
9
|
+
let(:builder){Dsl.new(:numbers)}
|
10
|
+
|
11
|
+
context "using a dsl fragment that contains no block usage" do
|
12
|
+
before (:each) do
|
13
|
+
builder.readable
|
14
|
+
end
|
15
|
+
before (:each) do
|
16
|
+
target.extend(sut.run_using(builder))
|
17
|
+
end
|
18
|
+
it "should create a member on the target that allows reading of the array" do
|
19
|
+
target.numbers.should be_a(Array)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ArrayFu
|
4
|
+
describe VisitorDetailStep do
|
5
|
+
class OurSet
|
6
|
+
attr_accessor :items
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@items = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(item)
|
13
|
+
@items << item
|
14
|
+
end
|
15
|
+
end
|
16
|
+
class Speak
|
17
|
+
def initialize(invocations)
|
18
|
+
@invocations = invocations
|
19
|
+
end
|
20
|
+
def hello
|
21
|
+
@invocations.push(1)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
context "when run" do
|
25
|
+
context "using a visitor that is symbol based" do
|
26
|
+
let(:target){OurSet.new}
|
27
|
+
let(:mutators){[]}
|
28
|
+
let(:invocations){[]}
|
29
|
+
let(:sut){VisitorDetailStep.new}
|
30
|
+
let(:builder){Dsl.new(:items)}
|
31
|
+
before (:each) do
|
32
|
+
(1..10).each{|item| target.add(Speak.new(invocations))}
|
33
|
+
builder.process_using(:run,:hello)
|
34
|
+
end
|
35
|
+
before (:each) do
|
36
|
+
target.extend(sut.run_using(builder))
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should create a method on the target that triggers each item in the list using its provided action" do
|
40
|
+
target.run
|
41
|
+
invocations.count.should == 10
|
42
|
+
end
|
43
|
+
end
|
44
|
+
context "using a visitor that is class based" do
|
45
|
+
class TheVisitor
|
46
|
+
attr_accessor :items
|
47
|
+
def initialize
|
48
|
+
@items = []
|
49
|
+
end
|
50
|
+
def run_using(item)
|
51
|
+
@items << item
|
52
|
+
end
|
53
|
+
end
|
54
|
+
let(:target){OurSet.new}
|
55
|
+
let(:visitor){TheVisitor.new}
|
56
|
+
let(:mutators){[]}
|
57
|
+
let(:invocations){[]}
|
58
|
+
let(:sut){VisitorDetailStep.new}
|
59
|
+
let(:builder){Dsl.new(:items)}
|
60
|
+
|
61
|
+
before (:each) do
|
62
|
+
(1..10).each{|item| target.add(item)}
|
63
|
+
builder.process_using(:run,visitor)
|
64
|
+
end
|
65
|
+
before (:each) do
|
66
|
+
target.extend(sut.run_using(builder))
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should create a method on the target that triggers the visitor once for each item in the list" do
|
70
|
+
target.run
|
71
|
+
(1..10).each{|item| visitor.items.include?(item).should be_true}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ArrayFu
|
4
|
+
describe WriteableStep do
|
5
|
+
context "when run" do
|
6
|
+
context "using a dsl fragment that contains no block usage" do
|
7
|
+
let(:target){Sample.new}
|
8
|
+
let(:mutators){[]}
|
9
|
+
let(:sut){WriteableStep.new}
|
10
|
+
let(:builder){Dsl.new(:numbers)}
|
11
|
+
before (:each) do
|
12
|
+
builder.writable
|
13
|
+
end
|
14
|
+
before (:each) do
|
15
|
+
target.extend(sut.run_using(builder))
|
16
|
+
end
|
17
|
+
it "should create a member on the target that allows assignment to the array" do
|
18
|
+
new_array = [1]
|
19
|
+
target.numbers = new_array
|
20
|
+
def target.numbers
|
21
|
+
return @numbers
|
22
|
+
end
|
23
|
+
target.numbers.should == new_array
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arrayfu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Develop With Passion®
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: guard-rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakes-rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Simple DSL For Declaritive Arrays
|
79
|
+
email:
|
80
|
+
- open_source@developwithpassion.com
|
81
|
+
executables:
|
82
|
+
- dwp_expand
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .rvmrc
|
88
|
+
- Gemfile
|
89
|
+
- Guardfile
|
90
|
+
- Rakefile
|
91
|
+
- arrayfu.gemspec
|
92
|
+
- bin/dwp_expand
|
93
|
+
- lib/arrayfu.rb
|
94
|
+
- lib/core/add_criterion.rb
|
95
|
+
- lib/core/dsl.rb
|
96
|
+
- lib/core/module_registry.rb
|
97
|
+
- lib/core/mutator_detail.rb
|
98
|
+
- lib/core/mutator_step.rb
|
99
|
+
- lib/core/object_extensions.rb
|
100
|
+
- lib/core/readable_step.rb
|
101
|
+
- lib/core/version.rb
|
102
|
+
- lib/core/visitor_detail.rb
|
103
|
+
- lib/core/visitor_detail_step.rb
|
104
|
+
- lib/core/writeable_step.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/specs/add_criterion_spec.rb
|
107
|
+
- spec/specs/dsl_spec.rb
|
108
|
+
- spec/specs/mutator_step_spec.rb
|
109
|
+
- spec/specs/object_extensions_spec.rb
|
110
|
+
- spec/specs/readable_step_spec.rb
|
111
|
+
- spec/specs/sample.rb
|
112
|
+
- spec/specs/visitor_detail_step_spec.rb
|
113
|
+
- spec/specs/writeable_step_spec.rb
|
114
|
+
homepage: http://www.developwithpassion.com
|
115
|
+
licenses: []
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project: arrayfu
|
134
|
+
rubygems_version: 1.8.21
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: Simple DSL For Declaritive Arrays
|
138
|
+
test_files:
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/specs/add_criterion_spec.rb
|
141
|
+
- spec/specs/dsl_spec.rb
|
142
|
+
- spec/specs/mutator_step_spec.rb
|
143
|
+
- spec/specs/object_extensions_spec.rb
|
144
|
+
- spec/specs/readable_step_spec.rb
|
145
|
+
- spec/specs/sample.rb
|
146
|
+
- spec/specs/visitor_detail_step_spec.rb
|
147
|
+
- spec/specs/writeable_step_spec.rb
|