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,26 +1,30 @@
1
- module Wonkavision
2
- module MessageMapper
3
- module IndifferentAccess
4
- def [](key)
5
- key = key.to_s
6
- super(key)
7
- end
8
-
9
- def []=(key,val)
10
- super(key.to_s,val)
11
- end
12
-
13
- def include?(key)
14
- super(key.to_s)
15
- end
16
-
17
- def method_missing(sym,*args,&block)
18
- return self[sym.to_s[0..-2]] = args[0] if sym.to_s =~ /.*=$/
19
- return self[sym] if self.keys.include?(sym.to_s)
20
- nil
21
- end
22
- end
23
- end
24
-
25
- end
26
-
1
+ module Wonkavision
2
+ module MessageMapper
3
+ module IndifferentAccess
4
+ def [](key)
5
+ key = key.to_s
6
+ super(key)
7
+ end
8
+
9
+ def []=(key,val)
10
+ super(key.to_s,val)
11
+ end
12
+
13
+ def include?(key)
14
+ super(key.to_s)
15
+ end
16
+
17
+ def delete(key)
18
+ super(key.to_s)
19
+ end
20
+
21
+ def method_missing(sym,*args,&block)
22
+ return self[sym.to_s[0..-2]] = args[0] if sym.to_s =~ /.*=$/
23
+ return self[sym] if self.keys.include?(sym.to_s)
24
+ nil
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+
@@ -1,153 +1,241 @@
1
- module Wonkavision
2
-
3
- module MessageMapper
4
- class Map < Hash
5
- include IndifferentAccess
6
-
7
- def initialize(context)
8
- @context_stack = []
9
- @context_stack.push(context)
10
- end
11
-
12
- def context
13
- @context_stack[-1]
14
- end
15
-
16
- def from (context,&block)
17
- raise "No block ws provided to 'from'" unless block
18
- @context_stack.push(context)
19
- instance_eval(&block)
20
- @context_stack.pop
21
- end
22
-
23
- def exec(map_name)
24
- mapped = MessageMapper.execute(map_name,context)
25
- self.merge!(mapped) if mapped
26
- end
27
- alias import exec
28
-
29
- def map(source,options={},&block)
30
- raise "Neither a block nor a map_name were provided to 'map'" unless (block || options.keys.include?(:map_name))
31
- if (source.is_a?(Hash))
32
- field_name = source.keys[0]
33
- ctx = source[field_name]
34
- else
35
- field_name = source
36
- ctx = context.instance_eval("self.#{field_name}")
37
- end
38
- if ctx
39
- if (map_name = options.delete(:map_name))
40
- child = MessageMapper.execute(map_name,ctx)
41
- else
42
- child = Map.new(ctx)
43
- child.instance_eval(&block)
44
- end
45
- else
46
- child = {}
47
- end
48
- self[field_name] = child
49
- end
50
-
51
- def array(source,options={},&block)
52
- if (source.is_a?(Hash))
53
- field_name = source.keys[0]
54
- ctx = source.keys[1]
55
- else
56
- field_name = source
57
- ctx = extract_value_from_context(context,field_name)
58
- end
59
- result = []
60
- map_name = options.delete(:map_name)
61
- ctx.each do |item|
62
- if (map_name)
63
- child = MessageMapper.execute(map_name,item)
64
- else
65
- child = Map.new(item)
66
- child.instance_eval(&block)
67
- end
68
- result << child
69
- end
70
- self[field_name] = result
71
- end
72
-
73
- def string(*args)
74
- value(*args) {to_s}
75
- end
76
-
77
- def float(*args)
78
- value(*args){respond_to?(:to_f) ? to_f : self}
79
- end
80
-
81
- def iso8601(*args)
82
- value(*args) do
83
- if respond_to?(:strftime)
84
- strftime("%Y-%m-%dT%H:%M:%S")
85
- elsif respond_to?(:ToString)
86
- ToString("yyyy-MM-ddTHH:mm:ss")
87
- else
88
- self
89
- end
90
- end
91
- end
92
-
93
- def date(*args)
94
- value(*args) do
95
- if kind_of?(Time) || kind_of?(Date)
96
- self
97
- elsif respond_to?(:to_time)
98
- to_time
99
- elsif respond_to?(:to_date)
100
- to_date
101
- elsif (date_str=to_s) && date_str.length > 0
102
- begin
103
- Time.parse(date_str)
104
- rescue
105
- self
106
- end
107
- else
108
- self
109
- end
110
- end
111
- end
112
-
113
- def boolean(*args)
114
- value(*args) do
115
- %w(true yes).include?(to_s.downcase) ? true : false
116
- end
117
- end
118
-
119
- def int(*args)
120
- value(*args){respond_to?(:to_i) ? to_i : self}
121
- end
122
-
123
- def value(*args,&block)
124
- if (args.length == 1 && args[0].is_a?(Hash))
125
- args[0].each do |key,value|
126
- value = context.instance_eval(&value) if value.is_a?(Proc)
127
- value = value.instance_eval(&block) if block
128
- self[key] = value
129
- end
130
- else
131
- args.each do |field_name|
132
- value = extract_value_from_context(context,field_name,block)
133
- self[field_name] = value
134
- end
135
- end
136
- end
137
- alias integer int
138
-
139
- private
140
- def extract_value_from_context(context,field_name,block=nil)
141
- if context.respond_to?(field_name.to_sym)
142
- value = context.instance_eval("self.#{field_name}")
143
- elsif context.respond_to?(:[])
144
- value = context[field_name]
145
- else
146
- value = nil
147
- end
148
- value = value.instance_eval(&block) if block
149
- value
150
- end
151
- end
152
- end
153
- end
1
+ module Wonkavision
2
+
3
+ module MessageMapper
4
+ class Map < Hash
5
+ include IndifferentAccess
6
+
7
+ def initialize(context)
8
+ @context_stack = []
9
+ @context_stack.push(context)
10
+ @formats = default_formats
11
+ end
12
+
13
+ def formats
14
+ @formats
15
+ end
16
+
17
+ def context
18
+ @context_stack[-1]
19
+ end
20
+
21
+ def from (context,&block)
22
+ raise "No block ws provided to 'from'" unless block
23
+ return if context.nil?
24
+
25
+ @context_stack.push(context)
26
+ instance_eval(&block)
27
+ @context_stack.pop
28
+ end
29
+
30
+ def exec(map_name,exec_context=self.context)
31
+ mapped = MessageMapper.execute(map_name,exec_context)
32
+ self.merge!(mapped) if mapped
33
+ end
34
+ alias import exec
35
+ alias apply exec
36
+
37
+ def child(source,options={},&block)
38
+ raise "Neither a block nor a map_name were provided to 'map'" unless (block || options.keys.include?(:map_name))
39
+ if (source.is_a?(Hash))
40
+ field_name = source.keys[0]
41
+ ctx = source[field_name]
42
+ else
43
+ field_name = source
44
+ ctx = extract_value_from_context(context,field_name)
45
+ end
46
+ if ctx
47
+ if (map_name = options.delete(:map_name))
48
+ child = MessageMapper.execute(map_name,ctx)
49
+ else
50
+ child = Map.new(ctx)
51
+ child.instance_eval(&block)
52
+ end
53
+ else
54
+ child = {}
55
+ end
56
+ set_value(field_name,child,options)
57
+ end
58
+
59
+ def array(source,options={},&block)
60
+ if (source.is_a?(Hash))
61
+ field_name = source.keys[0]
62
+ ctx = source[field_name]
63
+ else
64
+ field_name = source
65
+ ctx = extract_value_from_context(context,field_name)
66
+ end
67
+ result = []
68
+ ctx = [ctx].compact.flatten
69
+ map_name = options.delete(:map_name)
70
+ ctx.each do |item|
71
+ if (map_name)
72
+ child = MessageMapper.execute(map_name,item)
73
+ else
74
+ child = Map.new(item)
75
+ child.instance_eval(&block)
76
+ end
77
+ result << child
78
+ end
79
+ set_value(field_name,result,options)
80
+ end
81
+
82
+ def string(*args)
83
+ value(*args) {to_s}
84
+ end
85
+
86
+ def float(*args)
87
+ value(*args){respond_to?(:to_f) ? to_f : self}
88
+ end
89
+
90
+ def dollars(*args)
91
+ args = add_options(args,:format=>:dollars)
92
+ float(*args)
93
+ end
94
+ alias dollar dollars
95
+
96
+ def percent(*args)
97
+ args = add_options(args,:format=>:percent)
98
+ float(*args)
99
+ end
100
+ alias percentage percent
101
+
102
+ def yes_no(*args)
103
+ args = add_options(args,:format=>:yes_no)
104
+ value(*args)
105
+ end
106
+
107
+ def iso8601(*args)
108
+ value(*args) do
109
+ if respond_to?(:strftime)
110
+ strftime("%Y-%m-%dT%H:%M:%S")
111
+ elsif respond_to?(:ToString)
112
+ ToString("yyyy-MM-ddTHH:mm:ss")
113
+ else
114
+ self
115
+ end
116
+ end
117
+ end
118
+
119
+ def time(*args)
120
+ value(*args) do
121
+ if kind_of?(Time)
122
+ self
123
+ elsif respond_to?(:to_time)
124
+ to_time
125
+ elsif (time_str=to_s) && time_str.length > 0
126
+ begin
127
+ Time.parse(time_str)
128
+ rescue
129
+ self
130
+ end
131
+ else
132
+ self
133
+ end
134
+ end
135
+ end
136
+
137
+ def date(*args)
138
+ value(*args) do
139
+ if kind_of?(Date)
140
+ self
141
+ elsif respond_to?(:to_date)
142
+ to_time
143
+ elsif (date_str=to_s) && date_str.length > 0
144
+ begin
145
+ Date.parse(date_str)
146
+ rescue
147
+ self
148
+ end
149
+ else
150
+ self
151
+ end
152
+ end
153
+ end
154
+
155
+ def boolean(*args)
156
+ value(*args) do
157
+ %w(true yes).include?(to_s.downcase) ? true : false
158
+ end
159
+ end
160
+
161
+ def int(*args)
162
+ value(*args){respond_to?(:to_i) ? to_i : self}
163
+ end
164
+ alias integer int
165
+
166
+ def value(*args,&block)
167
+ opts = args.length > 1 ? args.extract_options! : {}
168
+
169
+ if (args.length == 1 && args[0].is_a?(Hash))
170
+ args[0].each do |key,value|
171
+ value = context.instance_eval(&value) if value.is_a?(Proc)
172
+ value = value.instance_eval(&block) if block
173
+ set_value(key,value,opts)
174
+ end
175
+ else
176
+ args.each do |field_name|
177
+ value = extract_value_from_context(context,field_name,block)
178
+ set_value(field_name,value,opts)
179
+ end
180
+ end
181
+ end
182
+
183
+ private
184
+ def format_value(val,opts={})
185
+ val = opts[:default] || opts[:default_value] if val.nil?
186
+ return val if val.nil?
187
+
188
+ format = opts[:format]
189
+ format ||= :float if opts[:precision]
190
+ return val unless format
191
+ formatter = formats[format] || format
192
+ default_formatter = formats[:default]
193
+
194
+ formatter.respond_to?(:call) ? formatter.call(val,format,opts) : default_formatter.call(val,formatter,opts)
195
+ end
196
+
197
+ def extract_value_from_context(context,field_name,block=nil)
198
+ if context.respond_to?(field_name.to_sym)
199
+ value = context.instance_eval("self.#{field_name}")
200
+ elsif context.respond_to?(:[])
201
+ value = context[field_name]
202
+ if value.nil? && field_name
203
+ value = context[field_name.to_sym] || context[field_name.to_s]
204
+ end
205
+ else
206
+ value = nil
207
+ end
208
+ value = value.instance_eval(&block) if block
209
+ value
210
+ end
211
+
212
+ def set_value(field_name,val,opts={})
213
+ if prefix = opts[:prefix]; field_name = "#{prefix}#{field_name}"; end
214
+ if suffix = opts[:suffix]; field_name = "#{field_name}#{suffix}"; end
215
+ self[field_name] = format_value(val,opts)
216
+ end
217
+
218
+ def default_formats
219
+ HashWithIndifferentAccess.new(
220
+ :default=>lambda {|v,f,opts| v.respond_to?(:strftime) ? v.strftime(f) : f % v } ,
221
+ :float =>lambda {|v,f,opts| precision_format(opts) % v },
222
+ :dollars=>lambda {|v,f,opts| "$#{precision_format(opts,2)}" % v},
223
+ :percent=>lambda {|v,f,opts| "#{precision_format(opts,1)}%%" % v},
224
+ :yes_no=>lambda {|v,f,opts| v ? "Yes" : "No"}
225
+ )
226
+ end
227
+
228
+ def precision_format(opts,default_precision=nil)
229
+ precision = opts[:precision] || default_precision
230
+ "%#{precision ? "." + precision.to_s : default_precision}f"
231
+ end
232
+
233
+ def add_options(args,new_options)
234
+ opts = args.length > 1 ? args.extract_options! : {}
235
+ opts.merge!(new_options)
236
+ args << opts
237
+ args
238
+ end
239
+ end
240
+ end
241
+ end
@@ -1,33 +1,33 @@
1
- module Wonkavision
2
- module Persistence
3
- module MongoMapperAdapter
4
-
5
- def self.included(model)
6
- model.plugin Wonkavision::Persistence::MongoMapperAdapter
7
- end
8
-
9
- module ClassMethods
10
- include Wonkavision::ActsAsOompaLoompa
11
-
12
- def define_document_key(key_name,key_type,options={})
13
- key(key_name, key_type, options) unless keys[key_name]
14
- end
15
-
16
- def update_activity(activity,event_data)
17
- activity.assign(event_data)
18
- :updated
19
- end
20
-
21
- def find_activity_instance(correlation_field_name,correlation_id)
22
- self.send("find_or_create_by_#{correlation_field_name}",correlation_id)
23
- end
24
- end
25
-
26
- end
27
-
28
- end
29
- end
30
-
31
- if defined?(::MongoMapper::Document)
32
- MongoMapper::Document.append_inclusions(Wonkavision::Persistence::MongoMapperAdapter)
1
+ module Wonkavision
2
+ module Persistence
3
+ module MongoMapperAdapter
4
+
5
+ def self.included(model)
6
+ model.plugin Wonkavision::Persistence::MongoMapperAdapter
7
+ end
8
+
9
+ module ClassMethods
10
+ include Wonkavision::ActsAsOompaLoompa
11
+
12
+ def define_document_key(key_name,key_type,options={})
13
+ key(key_name, key_type, options) unless keys[key_name]
14
+ end
15
+
16
+ def update_activity(activity,event_data)
17
+ activity.assign(event_data)
18
+ :updated
19
+ end
20
+
21
+ def find_activity_instance(correlation_field_name,correlation_id)
22
+ self.send("find_or_create_by_#{correlation_field_name}",correlation_id)
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ if defined?(::MongoMapper::Document)
32
+ MongoMapper::Document.append_inclusions(Wonkavision::Persistence::MongoMapperAdapter)
33
33
  end