wonkavision 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/CHANGELOG.rdoc +3 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.rdoc +0 -0
  4. data/Rakefile +48 -0
  5. data/lib/wonkavision.rb +68 -0
  6. data/lib/wonkavision/acts_as_oompa_loompa.rb +22 -0
  7. data/lib/wonkavision/event.rb +33 -0
  8. data/lib/wonkavision/event_binding.rb +21 -0
  9. data/lib/wonkavision/event_coordinator.rb +75 -0
  10. data/lib/wonkavision/event_handler.rb +14 -0
  11. data/lib/wonkavision/event_namespace.rb +72 -0
  12. data/lib/wonkavision/event_path_segment.rb +37 -0
  13. data/lib/wonkavision/message_mapper.rb +30 -0
  14. data/lib/wonkavision/message_mapper/indifferent_access.rb +26 -0
  15. data/lib/wonkavision/message_mapper/map.rb +126 -0
  16. data/lib/wonkavision/persistence/mongo_mapper_adapter.rb +33 -0
  17. data/lib/wonkavision/plugins.rb +30 -0
  18. data/lib/wonkavision/plugins/business_activity.rb +79 -0
  19. data/lib/wonkavision/plugins/event_handling.rb +78 -0
  20. data/lib/wonkavision/plugins/timeline.rb +80 -0
  21. data/lib/wonkavision/support.rb +0 -0
  22. data/lib/wonkavision/version.rb +3 -0
  23. data/test/business_activity_test.rb +24 -0
  24. data/test/config/database.yml +15 -0
  25. data/test/event_coordinator_test.rb +72 -0
  26. data/test/event_handler_test.rb +47 -0
  27. data/test/event_namespace_test.rb +109 -0
  28. data/test/event_path_segment_test.rb +56 -0
  29. data/test/event_test.rb +54 -0
  30. data/test/log/test.log +7853 -0
  31. data/test/map_test.rb +187 -0
  32. data/test/message_mapper_test.rb +21 -0
  33. data/test/test_activity_models.rb +56 -0
  34. data/test/test_helper.rb +63 -0
  35. data/test/timeline_test.rb +61 -0
  36. data/test/wonkavision_test.rb +10 -0
  37. data/wonkavision.gemspec +94 -0
  38. metadata +145 -0
data/test/map_test.rb ADDED
@@ -0,0 +1,187 @@
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
+ end
187
+ end
@@ -0,0 +1,21 @@
1
+ require "test_helper"
2
+
3
+ class Wonkavision::MessageMapperTest < ActiveSupport::TestCase
4
+ context "Wonkavision::MessageMapper.execute" do
5
+ setup do
6
+ Wonkavision::MessageMapper.register("test_map") do
7
+ context["i_was_here"] = true
8
+ end
9
+ end
10
+ should "evaluate the named block against a new map" do
11
+ data = {}
12
+ Wonkavision::MessageMapper.execute("test_map",data)
13
+ assert data["i_was_here"]
14
+ end
15
+ should "raise an error if the map is missing" do
16
+ assert_raise RuntimeError do
17
+ Wonkavision::MessageMapper.execute("I'm not really here", {})
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,56 @@
1
+ unless defined?(::TestEventHandler)
2
+ class TestEventHandler
3
+ include Wonkavision::EventHandler
4
+
5
+ def self.knids
6
+ @@knids ||= []
7
+ end
8
+
9
+ def self.reset
10
+ @@knids = []
11
+ end
12
+
13
+ event_namespace :vermicious
14
+
15
+ handle :knid do |data,path|
16
+ TestEventHandler.knids << [data,path]
17
+ end
18
+
19
+ #handle events in the vermicious namespace
20
+ handle "*" do |data,path|
21
+ TestEventHandler.knids << [data,path] if path !~ /.*knid/
22
+ end
23
+
24
+ #handle ALL events in any namespace
25
+ handle "/*" do |data,path|
26
+ puts "Handling #{path}. Before: #{TestEventHandler.knids.inspect}"
27
+ TestEventHandler.knids << [data,path] if path !~ /^vermicious.*/
28
+ puts "After: #{TestEventHandler.knids.inspect}"
29
+ end
30
+
31
+ end
32
+
33
+ class TestBusinessActivity
34
+ include MongoMapper::Document
35
+
36
+ acts_as_timeline
37
+
38
+ event_namespace :test
39
+ correlate_by :test_id
40
+
41
+ milestone :ms1, :evt1
42
+ milestone :ms2, :evt2
43
+ milestone :ms3, :evt3, '/not_test/evt4'
44
+
45
+ map /not_test\/.*/i do
46
+ import "evt4_test_map"
47
+ string 'modified_another_field'=> "'#{context["another_field"]}' WAS SERVED!! OH YEAH!! IN-YOUR-FACE!!"
48
+ end
49
+ end
50
+
51
+ Wonkavision::MessageMapper.register("evt4_test_map") do
52
+ string 'test_id'
53
+ date 'event_time'
54
+ end
55
+
56
+ end
@@ -0,0 +1,63 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ require "rubygems"
4
+ require "mongo_mapper"
5
+ require 'active_support/test_case'
6
+ require "shoulda"
7
+
8
+ dir = File.dirname(__FILE__)
9
+ require File.join(dir,"..","lib","wonkavision")
10
+
11
+ dir = File.expand_path(File.dirname(__FILE__))
12
+ logdir = File.join(dir,'log')
13
+ Dir.mkdir(logdir) unless File.directory?(logdir)
14
+ MongoMapper.setup(YAML.load_file(File.join(dir,'config', 'database.yml')), "test", {
15
+ :logger => Logger.new(File.join(logdir,'test.log')),
16
+ :passenger => false
17
+ })
18
+
19
+ module ActiveSupport
20
+ class TestCase
21
+ def setup
22
+
23
+ end
24
+ def teardown
25
+ MongoMapper.database.collections.each do |coll|
26
+ coll.drop unless coll.name =~ /(.*\.)?system\..*/
27
+ end
28
+ Time.reset #Return Time.now to its original state
29
+ end
30
+
31
+ # Make sure that each test case has a teardown
32
+ # method to clear the db after each test.
33
+ def inherited(base)
34
+ base.define_method teardown do
35
+ super
36
+ end
37
+ base.define_method setup do
38
+ super
39
+ end
40
+ end
41
+ end
42
+ end
43
+ #This seems like an odd approach - why not just alias now, you ask
44
+ #well, I did, and for whatever reason, got stack overflows
45
+ #whenever I'd try to run the test suite. So...um...this doesn't
46
+ #overflow the stack, so it will do.
47
+ class Time
48
+ @@old_now = method(:now)
49
+ class << self
50
+ def now=(new_now)
51
+ @@new_now = new_now.to_time
52
+ def Time.now
53
+ @@new_now
54
+ end
55
+ end
56
+
57
+ def reset
58
+ def Time.now
59
+ @@old_now.call
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,61 @@
1
+ require "test_helper"
2
+ require "test_activity_models"
3
+
4
+ class TimelineTest < ActiveSupport::TestCase
5
+ context "TestBusinessActivity" do
6
+ should "create the appropriate keys" do
7
+ assert TestBusinessActivity.keys.keys.include?("timeline")
8
+ assert TestBusinessActivity.keys.keys.include?("latest_milestone")
9
+ end
10
+ should "configure milestones from DSL" do
11
+ assert_equal 3, TestBusinessActivity.timeline_milestones.length
12
+
13
+ assert_equal :ms1, TestBusinessActivity.timeline_milestones[0].name
14
+ assert_equal "test/evt1", TestBusinessActivity.timeline_milestones[0].events[0]
15
+
16
+ assert_equal :ms2, TestBusinessActivity.timeline_milestones[1].name
17
+ assert_equal "test/evt2", TestBusinessActivity.timeline_milestones[1].events[0]
18
+
19
+ assert_equal :ms3, TestBusinessActivity.timeline_milestones[2].name
20
+ assert_equal "test/evt3", TestBusinessActivity.timeline_milestones[2].events[0]
21
+ assert_equal "/not_test/evt4", TestBusinessActivity.timeline_milestones[2].events[1]
22
+ end
23
+ should "subscribe to all milestone events" do
24
+ assert Wonkavision.event_coordinator.root_namespace.find_or_create("test/evt1").subscribers.length > 0
25
+ assert Wonkavision.event_coordinator.root_namespace.find_or_create("test/evt2").subscribers.length > 0
26
+ assert Wonkavision.event_coordinator.root_namespace.find_or_create("test/evt3").subscribers.length > 0
27
+ assert Wonkavision.event_coordinator.root_namespace.find_or_create("not_test/evt4").subscribers.length > 0
28
+ end
29
+ should "record appropriate milestone time and milestone upon relevant event being received" do
30
+ event = {"test_id"=>"123","event_time"=>"1/1/2001","another_field"=>"hi there"}
31
+ Wonkavision.event_coordinator.receive_event("test/evt1",event)
32
+ assert activity = TestBusinessActivity.find_by_test_id("123")
33
+ assert_equal "1/1/2001".to_time.utc,activity.timeline["ms1"]
34
+ assert_equal "hi there", activity.another_field
35
+ Wonkavision.event_coordinator.receive_event("test/evt2",event)
36
+ activity = activity.reload
37
+ assert_equal "1/1/2001".to_time.utc,activity.timeline["ms2"]
38
+ Wonkavision.event_coordinator.receive_event("test/evt3",event)
39
+ activity = activity.reload
40
+ assert_equal "1/1/2001".to_time.utc, activity.timeline["ms3"]
41
+ event["event_time"] = "1/1/2010"
42
+ Wonkavision.event_coordinator.receive_event("not_test/evt4",event)
43
+ activity = activity.reload
44
+ # timeline for milestone ms3 already set
45
+ assert_equal "1/1/2001".to_time.utc, activity.timeline["ms3"]
46
+ assert_equal "ms3", activity.latest_milestone
47
+ end
48
+ should "not overwrite latest_milestone when processing an event that occurred prior to the latest milestone event" do
49
+ event = {"test_id"=>"123","event_time"=>"1/1/2001 08:00"}
50
+ Wonkavision.event_coordinator.receive_event("test/evt2",event)
51
+ assert activity = TestBusinessActivity.find_by_test_id("123")
52
+ assert_equal "1/1/2001 08:00".to_time.utc,activity.timeline["ms2"]
53
+ assert_equal "ms2", activity.latest_milestone
54
+ event = {"test_id"=>"123","event_time"=>"1/1/2001 07:00"}
55
+ Wonkavision.event_coordinator.receive_event("test/evt1",event)
56
+ activity = activity.reload
57
+ assert_equal "1/1/2001 07:00".to_time.utc, activity.timeline["ms1"]
58
+ assert_equal "ms2", activity.latest_milestone
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,10 @@
1
+ require "test_helper"
2
+
3
+ class WonkavisionTest < ActiveSupport::TestCase
4
+
5
+ context "Jumping into an experimental teleportation device built by a madman" do
6
+ should "wreak some kind of humerous havoc" do
7
+ assert_equal true, true
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,94 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{wonkavision}
8
+ s.version = "0.5.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nathan Stults"]
12
+ s.date = %q{2010-07-19}
13
+ s.description = %q{Wonkavision is a small gem that allows you to publish}
14
+ s.email = %q{hereiam@sonic.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "CHANGELOG.rdoc",
21
+ "LICENSE.txt",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "lib/wonkavision.rb",
25
+ "lib/wonkavision/acts_as_oompa_loompa.rb",
26
+ "lib/wonkavision/event.rb",
27
+ "lib/wonkavision/event_binding.rb",
28
+ "lib/wonkavision/event_coordinator.rb",
29
+ "lib/wonkavision/event_handler.rb",
30
+ "lib/wonkavision/event_namespace.rb",
31
+ "lib/wonkavision/event_path_segment.rb",
32
+ "lib/wonkavision/message_mapper.rb",
33
+ "lib/wonkavision/message_mapper/indifferent_access.rb",
34
+ "lib/wonkavision/message_mapper/map.rb",
35
+ "lib/wonkavision/persistence/mongo_mapper_adapter.rb",
36
+ "lib/wonkavision/plugins.rb",
37
+ "lib/wonkavision/plugins/business_activity.rb",
38
+ "lib/wonkavision/plugins/event_handling.rb",
39
+ "lib/wonkavision/plugins/timeline.rb",
40
+ "lib/wonkavision/support.rb",
41
+ "lib/wonkavision/version.rb",
42
+ "test/business_activity_test.rb",
43
+ "test/config/database.yml",
44
+ "test/event_coordinator_test.rb",
45
+ "test/event_handler_test.rb",
46
+ "test/event_namespace_test.rb",
47
+ "test/event_path_segment_test.rb",
48
+ "test/event_test.rb",
49
+ "test/log/test.log",
50
+ "test/map_test.rb",
51
+ "test/message_mapper_test.rb",
52
+ "test/test_activity_models.rb",
53
+ "test/test_helper.rb",
54
+ "test/timeline_test.rb",
55
+ "test/wonkavision_test.rb",
56
+ "wonkavision.gemspec"
57
+ ]
58
+ s.homepage = %q{http://github.com/PlasticLizard/wonkavision}
59
+ s.rdoc_options = ["--charset=UTF-8"]
60
+ s.require_paths = ["lib"]
61
+ s.rubygems_version = %q{1.3.7}
62
+ s.summary = %q{Simple Business Activity Monitoring}
63
+ s.test_files = [
64
+ "test/business_activity_test.rb",
65
+ "test/event_coordinator_test.rb",
66
+ "test/event_handler_test.rb",
67
+ "test/event_namespace_test.rb",
68
+ "test/event_path_segment_test.rb",
69
+ "test/event_test.rb",
70
+ "test/map_test.rb",
71
+ "test/message_mapper_test.rb",
72
+ "test/test_activity_models.rb",
73
+ "test/test_helper.rb",
74
+ "test/timeline_test.rb",
75
+ "test/wonkavision_test.rb"
76
+ ]
77
+
78
+ if s.respond_to? :specification_version then
79
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
80
+ s.specification_version = 3
81
+
82
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
83
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
84
+ s.add_development_dependency(%q<shoulda>, ["= 2.10.3"])
85
+ else
86
+ s.add_dependency(%q<activesupport>, [">= 2.3"])
87
+ s.add_dependency(%q<shoulda>, ["= 2.10.3"])
88
+ end
89
+ else
90
+ s.add_dependency(%q<activesupport>, [">= 2.3"])
91
+ s.add_dependency(%q<shoulda>, ["= 2.10.3"])
92
+ end
93
+ end
94
+