wonkavision 0.5.4 → 0.5.9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +28 -16
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -21
- data/Rakefile +47 -47
- data/lib/wonkavision.rb +75 -74
- data/lib/wonkavision/acts_as_oompa_loompa.rb +22 -22
- data/lib/wonkavision/event_binding.rb +21 -21
- data/lib/wonkavision/event_context.rb +9 -9
- data/lib/wonkavision/event_coordinator.rb +75 -75
- data/lib/wonkavision/event_handler.rb +15 -15
- data/lib/wonkavision/event_namespace.rb +79 -79
- data/lib/wonkavision/event_path_segment.rb +35 -35
- data/lib/wonkavision/message_mapper.rb +30 -30
- data/lib/wonkavision/message_mapper/indifferent_access.rb +30 -26
- data/lib/wonkavision/message_mapper/map.rb +241 -153
- data/lib/wonkavision/persistence/mongo_mapper_adapter.rb +32 -32
- data/lib/wonkavision/persistence/mongoid_adapter.rb +32 -0
- data/lib/wonkavision/plugins.rb +30 -30
- data/lib/wonkavision/plugins/business_activity.rb +92 -92
- data/lib/wonkavision/plugins/business_activity/event_binding.rb +15 -15
- data/lib/wonkavision/plugins/callbacks.rb +182 -182
- data/lib/wonkavision/plugins/event_handling.rb +111 -111
- data/lib/wonkavision/plugins/timeline.rb +79 -79
- data/lib/wonkavision/version.rb +3 -3
- data/test/business_activity_test.rb +31 -31
- data/test/event_handler_test.rb +68 -69
- data/test/event_namespace_test.rb +108 -108
- data/test/event_path_segment_test.rb +41 -41
- data/test/log/test.log +817 -18354
- data/test/map_test.rb +315 -201
- data/test/message_mapper_test.rb +20 -20
- data/test/test_activity_models.rb +72 -72
- data/test/test_helper.rb +70 -63
- data/test/timeline_test.rb +55 -61
- data/test/wonkavision_test.rb +9 -9
- metadata +72 -12
- data/wonkavision.gemspec +0 -97
data/test/map_test.rb
CHANGED
@@ -1,201 +1,315 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class MapTest < ActiveSupport::TestCase
|
4
|
-
context "Map.initialize" do
|
5
|
-
should "make the provided object the current context" do
|
6
|
-
assert_equal 1, Wonkavision::MessageMapper::Map.new(1).context
|
7
|
-
end
|
8
|
-
end
|
9
|
-
context "Indexing into a
|
10
|
-
should "provide indifferent access" do
|
11
|
-
h = Wonkavision::MessageMapper::Map.new({})
|
12
|
-
h[:hello] = "hi"
|
13
|
-
assert_equal "hi", h["hello"]
|
14
|
-
h["hi"] = {:hello=>:sir}
|
15
|
-
assert_equal({:hello=>:sir}, h[:hi])
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
assert_equal "
|
22
|
-
end
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
should "
|
33
|
-
m = Wonkavision::MessageMapper::Map.new(
|
34
|
-
m.from(
|
35
|
-
self.
|
36
|
-
end
|
37
|
-
assert_equal 1, m.
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
ctx
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
m
|
68
|
-
|
69
|
-
|
70
|
-
assert_equal
|
71
|
-
end
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
assert_equal "
|
78
|
-
end
|
79
|
-
end
|
80
|
-
context "Map.
|
81
|
-
should "convert the underlying value to a
|
82
|
-
m = Wonkavision::MessageMapper::Map.new({:a
|
83
|
-
m.
|
84
|
-
assert_equal
|
85
|
-
end
|
86
|
-
end
|
87
|
-
context "Map.
|
88
|
-
should "convert underlying
|
89
|
-
m = Wonkavision::MessageMapper::Map.new({:a=>
|
90
|
-
m.
|
91
|
-
assert_equal
|
92
|
-
end
|
93
|
-
end
|
94
|
-
context "Map.
|
95
|
-
should "convert
|
96
|
-
m = Wonkavision::MessageMapper::Map.new({:a=>"01/
|
97
|
-
m.
|
98
|
-
assert_equal "
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
m.
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
m = Wonkavision::MessageMapper::Map.new(:a=>
|
115
|
-
m.
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
should "
|
136
|
-
m = Wonkavision::MessageMapper::Map.new(:a=>
|
137
|
-
m.
|
138
|
-
|
139
|
-
end
|
140
|
-
end
|
141
|
-
context "Map.
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
end
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
end
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
assert_equal
|
194
|
-
assert_equal
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class MapTest < ActiveSupport::TestCase
|
4
|
+
context "Map.initialize" do
|
5
|
+
should "make the provided object the current context" do
|
6
|
+
assert_equal 1, Wonkavision::MessageMapper::Map.new(1).context
|
7
|
+
end
|
8
|
+
end
|
9
|
+
context "Indexing into a MessageMapper::Map" do
|
10
|
+
should "provide indifferent access" do
|
11
|
+
h = Wonkavision::MessageMapper::Map.new({})
|
12
|
+
h[:hello] = "hi"
|
13
|
+
assert_equal "hi", h["hello"]
|
14
|
+
h["hi"] = {:hello=>:sir}
|
15
|
+
assert_equal({:hello=>:sir}, h[:hi])
|
16
|
+
end
|
17
|
+
|
18
|
+
should "provide indifferent deletes" do
|
19
|
+
h = Wonkavision::MessageMapper::Map.new({})
|
20
|
+
h["hello"] = "hi"
|
21
|
+
assert_equal "hi", h.delete(:hello)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "allow method missing to index into hash" do
|
25
|
+
l = Wonkavision::MessageMapper::Map.new({})
|
26
|
+
l.hello = "goodbye"
|
27
|
+
assert_equal "goodbye", l[:hello]
|
28
|
+
assert_equal "goodbye", l.hello
|
29
|
+
end
|
30
|
+
end
|
31
|
+
context "Map.from" do
|
32
|
+
should "evaluate the provided block against the map using the provided object as the current context" do
|
33
|
+
m = Wonkavision::MessageMapper::Map.new({})
|
34
|
+
m.from(1) do
|
35
|
+
self.take_it = context
|
36
|
+
end
|
37
|
+
assert_equal 1, m.take_it
|
38
|
+
end
|
39
|
+
should "return the previous context after the block" do
|
40
|
+
m = Wonkavision::MessageMapper::Map.new(1)
|
41
|
+
m.from(2) do
|
42
|
+
self.a = context
|
43
|
+
end
|
44
|
+
assert_equal 1, m.context
|
45
|
+
assert_equal 2, m.a
|
46
|
+
end
|
47
|
+
end
|
48
|
+
context "Map.map" do
|
49
|
+
should "raise an exception if called without a block or map name" do
|
50
|
+
m = Wonkavision::MessageMapper::Map.new({})
|
51
|
+
assert_raise RuntimeError do
|
52
|
+
m.child(1)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
should "eval a provided block against a new map with the provided context" do
|
56
|
+
m = Wonkavision::MessageMapper::Map.new({})
|
57
|
+
ctx = {:hi=>true}
|
58
|
+
m.child "this"=>ctx do
|
59
|
+
self.ctx = context
|
60
|
+
end
|
61
|
+
assert_equal ctx, m.this.ctx
|
62
|
+
end
|
63
|
+
should "execute the provided map_name using the provided context" do
|
64
|
+
Wonkavision::MessageMapper.register("map_test") do
|
65
|
+
self.ctx = context
|
66
|
+
end
|
67
|
+
m = Wonkavision::MessageMapper::Map.new({})
|
68
|
+
ctx = {:hi=>true}
|
69
|
+
m.child({"this"=>ctx}, {:map_name=>"map_test"})
|
70
|
+
assert_equal ctx, m.this.ctx
|
71
|
+
end
|
72
|
+
should "Get the context based on provided field name" do
|
73
|
+
m = Wonkavision::MessageMapper::Map.new({:a=>:b})
|
74
|
+
m.child "length" do
|
75
|
+
self.l = context
|
76
|
+
end
|
77
|
+
assert_equal 1,m["length"].l
|
78
|
+
end
|
79
|
+
end
|
80
|
+
context "Map.string" do
|
81
|
+
should "convert the underlying value to a string" do
|
82
|
+
m = Wonkavision::MessageMapper::Map.new({:a=>:b})
|
83
|
+
m.string :a
|
84
|
+
assert_equal "b", m.a
|
85
|
+
end
|
86
|
+
end
|
87
|
+
context "Map.float" do
|
88
|
+
should "convert the underlying value to a float" do
|
89
|
+
m = Wonkavision::MessageMapper::Map.new({:a=>"1"})
|
90
|
+
m.float :a
|
91
|
+
assert_equal 1.0, m.a
|
92
|
+
end
|
93
|
+
end
|
94
|
+
context "Map.iso8601" do
|
95
|
+
should "convert underlying dates into an iso 8601 string" do
|
96
|
+
m = Wonkavision::MessageMapper::Map.new({:a=>Time.parse("02/01/2001 01:00 PM")})
|
97
|
+
m.iso8601 :a
|
98
|
+
assert_equal "2001-02-01T13:00:00", m.a
|
99
|
+
end
|
100
|
+
end
|
101
|
+
context "Map.date" do
|
102
|
+
should "convert a string to a date" do
|
103
|
+
m = Wonkavision::MessageMapper::Map.new({:a=>"01/02/2001"})
|
104
|
+
m.date :a
|
105
|
+
assert_equal "01/02/2001".to_time, m.a
|
106
|
+
end
|
107
|
+
should "accept a date unmolested" do
|
108
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>Date.today)
|
109
|
+
m.date :a
|
110
|
+
assert_equal Date.today, m.a
|
111
|
+
end
|
112
|
+
should "accept a time unmolested" do
|
113
|
+
time = Time.now
|
114
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>time)
|
115
|
+
m.date :a
|
116
|
+
assert_equal time, m.a
|
117
|
+
end
|
118
|
+
end
|
119
|
+
context "Map.boolean" do
|
120
|
+
should "convert a 'true' string to a bool" do
|
121
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>'TruE')
|
122
|
+
m.boolean :a
|
123
|
+
assert m.a
|
124
|
+
end
|
125
|
+
should "convert a 'yes' string a bool" do
|
126
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>'YeS')
|
127
|
+
m.boolean :a
|
128
|
+
assert m.a
|
129
|
+
end
|
130
|
+
should "convert any other string to a false" do
|
131
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>"Whatever")
|
132
|
+
m.boolean :a
|
133
|
+
assert_equal false, m.a
|
134
|
+
end
|
135
|
+
should "accept a proper boolean at face value" do
|
136
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>true)
|
137
|
+
m.boolean :a
|
138
|
+
assert m.a
|
139
|
+
end
|
140
|
+
end
|
141
|
+
context "Map.int" do
|
142
|
+
should "convert a value to an int" do
|
143
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>"5.2")
|
144
|
+
m.int :a
|
145
|
+
assert_equal 5, m.a
|
146
|
+
end
|
147
|
+
end
|
148
|
+
context "Map.dollars" do
|
149
|
+
should "convert to a dollar string" do
|
150
|
+
m= Wonkavision::MessageMapper::Map.new(:a=>"5.2")
|
151
|
+
m.dollars :a
|
152
|
+
assert_equal "$5.20", m.a
|
153
|
+
end
|
154
|
+
end
|
155
|
+
context "Map.percent" do
|
156
|
+
should "convert to a percent string" do
|
157
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>"5")
|
158
|
+
m.percent :a
|
159
|
+
assert_equal "5.0%", m.a
|
160
|
+
end
|
161
|
+
end
|
162
|
+
context "Map.yes_no" do
|
163
|
+
should "convert a bool into a Yes or No" do
|
164
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>true, :b=>false)
|
165
|
+
m.yes_no :a,:b
|
166
|
+
assert_equal "Yes", m.a
|
167
|
+
assert_equal "No", m.b
|
168
|
+
end
|
169
|
+
end
|
170
|
+
context "Map.exec" do
|
171
|
+
setup do
|
172
|
+
Wonkavision::MessageMapper.register "exec_test" do
|
173
|
+
string :len => context.length
|
174
|
+
end
|
175
|
+
end
|
176
|
+
should "apply an external map to the current context" do
|
177
|
+
m = Wonkavision::MessageMapper::Map.new([1,2,3])
|
178
|
+
m.exec "exec_test"
|
179
|
+
assert_equal "3", m.len
|
180
|
+
end
|
181
|
+
should "apply an external map to a supplied context" do
|
182
|
+
m = Wonkavision::MessageMapper::Map.new([1,2,[1,2,3,4]])
|
183
|
+
m.exec "exec_test", m.context[-1]
|
184
|
+
assert_equal "4", m.len
|
185
|
+
end
|
186
|
+
end
|
187
|
+
context "Map.value" do
|
188
|
+
context "when the only argument is a hash" do
|
189
|
+
should "iterate the hash, mapping each entry" do
|
190
|
+
context = {:a=>1, :b=>2}
|
191
|
+
m = Wonkavision::MessageMapper::Map.new(context)
|
192
|
+
m.value :c=>context[:a], :d=>context[:b]
|
193
|
+
assert_equal 1, m.c
|
194
|
+
assert_equal 2, m.d
|
195
|
+
end
|
196
|
+
should "evaluate a proc in the context of a proc if provided" do
|
197
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>1)
|
198
|
+
m.value :c=> proc {self[:a]}
|
199
|
+
assert_equal 1, m.c
|
200
|
+
end
|
201
|
+
should "evaluate a block in the context of the provided value" do
|
202
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>1)
|
203
|
+
m.value(:c=>m.context) do
|
204
|
+
self[:a]
|
205
|
+
end
|
206
|
+
assert_equal 1, m.c
|
207
|
+
end
|
208
|
+
should "format numbers via a provided format string" do
|
209
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>"1")
|
210
|
+
m.value(:a, :format=>"%.1f")
|
211
|
+
assert_equal "1.0", m.a
|
212
|
+
end
|
213
|
+
should "format dates via a provided format string" do
|
214
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>Date.today)
|
215
|
+
m.value(:a, :format=>"%Y-%m-%d %H:%M:%S")
|
216
|
+
assert_equal Date.today.strftime("%Y-%m-%d %H:%M:%S"), m.a
|
217
|
+
end
|
218
|
+
should "return nil when a formatted date is requested on a nil value" do
|
219
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>nil)
|
220
|
+
m.date(:a, :format=>"%Y")
|
221
|
+
assert_nil m.a
|
222
|
+
end
|
223
|
+
should "use the provided default is the mapped value is nil" do
|
224
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>nil)
|
225
|
+
m.value(:a, :precision=>2, :default=>1)
|
226
|
+
assert_equal "1.00", m.a
|
227
|
+
end
|
228
|
+
should "format a value as a float if precision is specified" do
|
229
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>"3")
|
230
|
+
m.value(:a, :precision=>2)
|
231
|
+
assert_equal "3.00", m.a
|
232
|
+
end
|
233
|
+
should "format dollars as dollars" do
|
234
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>"3.1")
|
235
|
+
m.value(:a, :format=>:dollars)
|
236
|
+
assert_equal "$3.10", m.a
|
237
|
+
end
|
238
|
+
should "repsect precision option for dollars" do
|
239
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>"3.1")
|
240
|
+
m.value(:a, :format=>:dollars, :precision=>1)
|
241
|
+
assert_equal "$3.1", m.a
|
242
|
+
end
|
243
|
+
should "format percents" do
|
244
|
+
m= Wonkavision::MessageMapper::Map.new(:a=>"3.12")
|
245
|
+
m.value(:a, :format=>:percent)
|
246
|
+
assert_equal "3.1%", m.a
|
247
|
+
end
|
248
|
+
should "format yes_no = yes correctly" do
|
249
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>true)
|
250
|
+
m.value(:a, :format=>:yes_no)
|
251
|
+
assert_equal "Yes", m.a
|
252
|
+
end
|
253
|
+
should "format yes_no = no correctly" do
|
254
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>false)
|
255
|
+
m.value(:a, :format=>:yes_no)
|
256
|
+
assert_equal "No", m.a
|
257
|
+
end
|
258
|
+
should "append prefix and suffix to keys when provided" do
|
259
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>false)
|
260
|
+
m.value(:a, :format=>:yes_no, :prefix=>"pre_", :suffix=>"_post")
|
261
|
+
assert_equal "No", m.pre_a_post
|
262
|
+
assert_nil m.a
|
263
|
+
end
|
264
|
+
end
|
265
|
+
context "when called with a list of names" do
|
266
|
+
should "iterate the list, mapping each entry" do
|
267
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>1, :b=>2)
|
268
|
+
m.value :a, :b
|
269
|
+
assert_equal 1, m.a
|
270
|
+
assert_equal 2, m.b
|
271
|
+
end
|
272
|
+
should "call a method by the same name on the context if present" do
|
273
|
+
m = Wonkavision::MessageMapper::Map.new("01/01/2001")
|
274
|
+
m.value :to_time
|
275
|
+
assert_equal "01/01/2001".to_time, m.to_time
|
276
|
+
end
|
277
|
+
should "index into a hash if provided" do
|
278
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>1)
|
279
|
+
m.value :a
|
280
|
+
assert_equal 1, m.a
|
281
|
+
end
|
282
|
+
should "evaluate a block on the value if provided" do
|
283
|
+
m = Wonkavision::MessageMapper::Map.new(:a=>1)
|
284
|
+
m.value(:a) {to_s}
|
285
|
+
assert_equal "1", m.a
|
286
|
+
end
|
287
|
+
end
|
288
|
+
context "when mapping an array" do
|
289
|
+
should "apply the supplied block to each item in the array" do
|
290
|
+
m = Wonkavision::MessageMapper::Map.new(:collection=>[{:a=>1,:b=>2},{:a=>3,:b=>4}])
|
291
|
+
m.array :collection do
|
292
|
+
string :a
|
293
|
+
integer :b
|
294
|
+
end
|
295
|
+
assert_equal 2, m.collection.length
|
296
|
+
assert_equal "1", m.collection[0].a
|
297
|
+
assert_equal 2, m.collection[0].b
|
298
|
+
assert_equal "3", m.collection[1].a
|
299
|
+
assert_equal 4, m.collection[1].b
|
300
|
+
end
|
301
|
+
should "apply the supplied block to each item in the explicitly provided array" do
|
302
|
+
m = Wonkavision::MessageMapper::Map.new(:collection=>[{:a=>1,:b=>2},{:a=>3,:b=>4}])
|
303
|
+
m.array :new_collection=>m.context[:collection] do
|
304
|
+
string :a
|
305
|
+
integer :b
|
306
|
+
end
|
307
|
+
assert_equal 2, m.new_collection.length
|
308
|
+
assert_equal "1", m.new_collection[0].a
|
309
|
+
assert_equal 2, m.new_collection[0].b
|
310
|
+
assert_equal "3", m.new_collection[1].a
|
311
|
+
assert_equal 4, m.new_collection[1].b
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|