wonkavision 0.5.4 → 0.5.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/CHANGELOG.rdoc +28 -16
  2. data/Gemfile +5 -0
  3. data/LICENSE.txt +21 -21
  4. data/Rakefile +47 -47
  5. data/lib/wonkavision.rb +75 -74
  6. data/lib/wonkavision/acts_as_oompa_loompa.rb +22 -22
  7. data/lib/wonkavision/event_binding.rb +21 -21
  8. data/lib/wonkavision/event_context.rb +9 -9
  9. data/lib/wonkavision/event_coordinator.rb +75 -75
  10. data/lib/wonkavision/event_handler.rb +15 -15
  11. data/lib/wonkavision/event_namespace.rb +79 -79
  12. data/lib/wonkavision/event_path_segment.rb +35 -35
  13. data/lib/wonkavision/message_mapper.rb +30 -30
  14. data/lib/wonkavision/message_mapper/indifferent_access.rb +30 -26
  15. data/lib/wonkavision/message_mapper/map.rb +241 -153
  16. data/lib/wonkavision/persistence/mongo_mapper_adapter.rb +32 -32
  17. data/lib/wonkavision/persistence/mongoid_adapter.rb +32 -0
  18. data/lib/wonkavision/plugins.rb +30 -30
  19. data/lib/wonkavision/plugins/business_activity.rb +92 -92
  20. data/lib/wonkavision/plugins/business_activity/event_binding.rb +15 -15
  21. data/lib/wonkavision/plugins/callbacks.rb +182 -182
  22. data/lib/wonkavision/plugins/event_handling.rb +111 -111
  23. data/lib/wonkavision/plugins/timeline.rb +79 -79
  24. data/lib/wonkavision/version.rb +3 -3
  25. data/test/business_activity_test.rb +31 -31
  26. data/test/event_handler_test.rb +68 -69
  27. data/test/event_namespace_test.rb +108 -108
  28. data/test/event_path_segment_test.rb +41 -41
  29. data/test/log/test.log +817 -18354
  30. data/test/map_test.rb +315 -201
  31. data/test/message_mapper_test.rb +20 -20
  32. data/test/test_activity_models.rb +72 -72
  33. data/test/test_helper.rb +70 -63
  34. data/test/timeline_test.rb +55 -61
  35. data/test/wonkavision_test.rb +9 -9
  36. metadata +72 -12
  37. data/wonkavision.gemspec +0 -97
@@ -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 MicroMapper::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
- should "allow method missing to index into hash" do
18
- l = Wonkavision::MessageMapper::Map.new({})
19
- l.hello = "goodbye"
20
- assert_equal "goodbye", l[:hello]
21
- assert_equal "goodbye", l.hello
22
- end
23
- end
24
- context "Map.from" do
25
- should "evaluate the provided block against the map using the provided object as the current context" do
26
- m = Wonkavision::MessageMapper::Map.new({})
27
- m.from(1) do
28
- self.take_it = context
29
- end
30
- assert_equal 1, m.take_it
31
- end
32
- should "return the previous context after the block" do
33
- m = Wonkavision::MessageMapper::Map.new(1)
34
- m.from(2) do
35
- self.a = context
36
- end
37
- assert_equal 1, m.context
38
- assert_equal 2, m.a
39
- end
40
- end
41
- context "Map.map" do
42
- should "raise an exception if called without a block or map name" do
43
- m = Wonkavision::MessageMapper::Map.new({})
44
- assert_raise RuntimeError do
45
- m.map(1)
46
- end
47
- end
48
- should "eval a provided block against a new map with the provided context" do
49
- m = Wonkavision::MessageMapper::Map.new({})
50
- ctx = {:hi=>true}
51
- m.map "this"=>ctx do
52
- self.ctx = context
53
- end
54
- assert_equal ctx, m.this.ctx
55
- end
56
- should "execute the provided map_name using the provided context" do
57
- Wonkavision::MessageMapper.register("map_test") do
58
- self.ctx = context
59
- end
60
- m = Wonkavision::MessageMapper::Map.new({})
61
- ctx = {:hi=>true}
62
- m.map({"this"=>ctx}, {:map_name=>"map_test"})
63
- assert_equal ctx, m.this.ctx
64
- end
65
- should "Get the context based on provided field name" do
66
- m = Wonkavision::MessageMapper::Map.new({:a=>:b})
67
- m.map "length" do
68
- self.l = context
69
- end
70
- assert_equal 1,m["length"].l
71
- end
72
- end
73
- context "Map.string" do
74
- should "convert the underlying value to a string" do
75
- m = Wonkavision::MessageMapper::Map.new({:a=>:b})
76
- m.string :a
77
- assert_equal "b", m.a
78
- end
79
- end
80
- context "Map.float" do
81
- should "convert the underlying value to a float" do
82
- m = Wonkavision::MessageMapper::Map.new({:a=>"1"})
83
- m.float :a
84
- assert_equal 1.0, m.a
85
- end
86
- end
87
- context "Map.iso8601" do
88
- should "convert underlying dates into an iso 8601 string" do
89
- m = Wonkavision::MessageMapper::Map.new({:a=>Time.parse("02/01/2001 01:00 PM")})
90
- m.iso8601 :a
91
- assert_equal "2001-02-01T13:00:00", m.a
92
- end
93
- end
94
- context "Map.date" do
95
- should "convert a string to a date" do
96
- m = Wonkavision::MessageMapper::Map.new({:a=>"01/02/2001"})
97
- m.date :a
98
- assert_equal "01/02/2001".to_time, m.a
99
- end
100
- should "accept a date unmolested" do
101
- m = Wonkavision::MessageMapper::Map.new(:a=>Date.today)
102
- m.date :a
103
- assert_equal Date.today, m.a
104
- end
105
- should "accept a time unmolested" do
106
- time = Time.now
107
- m = Wonkavision::MessageMapper::Map.new(:a=>time)
108
- m.date :a
109
- assert_equal time, m.a
110
- end
111
- end
112
- context "Map.boolean" do
113
- should "convert a 'true' string to a bool" do
114
- m = Wonkavision::MessageMapper::Map.new(:a=>'TruE')
115
- m.boolean :a
116
- assert m.a
117
- end
118
- should "convert a 'yes' string a bool" do
119
- m = Wonkavision::MessageMapper::Map.new(:a=>'YeS')
120
- m.boolean :a
121
- assert m.a
122
- end
123
- should "convert any other string to a false" do
124
- m = Wonkavision::MessageMapper::Map.new(:a=>"Whatever")
125
- m.boolean :a
126
- assert_equal false, m.a
127
- end
128
- should "accept a proper boolean at face value" do
129
- m = Wonkavision::MessageMapper::Map.new(:a=>true)
130
- m.boolean :a
131
- assert m.a
132
- end
133
- end
134
- context "Map.int" do
135
- should "convert a value to an int" do
136
- m = Wonkavision::MessageMapper::Map.new(:a=>"5.2")
137
- m.int :a
138
- assert_equal 5, m.a
139
- end
140
- end
141
- context "Map.value" do
142
- context "when the only argument is a hash" do
143
- should "iterate the hash, mapping each entry" do
144
- context = {:a=>1, :b=>2}
145
- m = Wonkavision::MessageMapper::Map.new(context)
146
- m.value :c=>context[:a], :d=>context[:b]
147
- assert_equal 1, m.c
148
- assert_equal 2, m.d
149
- end
150
- should "evaluate a proc in the context of a proc if provided" do
151
- m = Wonkavision::MessageMapper::Map.new(:a=>1)
152
- m.value :c=> proc {self[:a]}
153
- assert_equal 1, m.c
154
- end
155
- should "evaluate a block in the context of the provided value" do
156
- m = Wonkavision::MessageMapper::Map.new(:a=>1)
157
- m.value(:c=>m.context) do
158
- self[:a]
159
- end
160
- assert_equal 1, m.c
161
- end
162
- end
163
- context "when called with a list of names" do
164
- should "iterate the list, mapping each entry" do
165
- m = Wonkavision::MessageMapper::Map.new(:a=>1, :b=>2)
166
- m.value :a, :b
167
- assert_equal 1, m.a
168
- assert_equal 2, m.b
169
- end
170
- should "call a method by the same name on the context if present" do
171
- m = Wonkavision::MessageMapper::Map.new("01/01/2001")
172
- m.value :to_time
173
- assert_equal "01/01/2001".to_time, m.to_time
174
- end
175
- should "index into a hash if provided" do
176
- m = Wonkavision::MessageMapper::Map.new(:a=>1)
177
- m.value :a
178
- assert_equal 1, m.a
179
- end
180
- should "evaluate a block on the value if provided" do
181
- m = Wonkavision::MessageMapper::Map.new(:a=>1)
182
- m.value(:a) {to_s}
183
- assert_equal "1", m.a
184
- end
185
- end
186
- context "when mapping an array" do
187
- should "apply the supplied block to each item in the array" do
188
- m = Wonkavision::MessageMapper::Map.new(:collection=>[{:a=>1,:b=>2},{:a=>3,:b=>4}])
189
- m.array :collection do
190
- string :a
191
- integer :b
192
- end
193
- assert_equal 2, m.collection.length
194
- assert_equal "1", m.collection[0].a
195
- assert_equal 2, m.collection[0].b
196
- assert_equal "3", m.collection[1].a
197
- assert_equal 4, m.collection[1].b
198
- end
199
- end
200
- end
201
- end
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