dm-is-state_machine 0.9.8 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,10 +1,17 @@
1
+ === 0.9.9 / 2009-01-04
2
+
3
+ * 1 minor enhancement:
4
+
5
+ * Added transition! to allow before/after hooks to execute on state
6
+ transitions.
7
+
1
8
  === 0.9.8 / 2008-12-07
2
9
 
3
10
  * No changes this version
4
11
 
5
12
  === 0.9.7 / 2008-11-18
6
13
 
7
- 4 minor enhancements:
14
+ * 4 minor enhancements:
8
15
 
9
16
  * Added support to pass a symbol to a transition, which will call the
10
17
  corresponding method. This means you don't have to put a whole
@@ -42,11 +42,8 @@ module DataMapper
42
42
  push_state_machine_context(:event)
43
43
 
44
44
  # ===== Define methods =====
45
- column = machine.column
46
45
  define_method("#{name}!") do
47
- machine.current_state_name = send(:"#{column}")
48
- machine.fire_event(name, self)
49
- send(:"#{column}=", machine.current_state_name)
46
+ transition!(name)
50
47
  end
51
48
 
52
49
  # Possible alternative to the above:
@@ -103,6 +103,14 @@ module DataMapper
103
103
  end
104
104
  end
105
105
 
106
+ def transition!(event_name)
107
+ machine = self.class.instance_variable_get(:@is_state_machine)[:machine]
108
+ column = machine.column
109
+ machine.current_state_name = attribute_get(:"#{column}")
110
+ machine.fire_event(event_name, self)
111
+ attribute_set(:"#{column}", machine.current_state_name)
112
+ end
113
+
106
114
  end # InstanceMethods
107
115
 
108
116
  end # StateMachine
@@ -1,7 +1,7 @@
1
1
  module DataMapper
2
2
  module Is
3
3
  module StateMachine
4
- VERSION = '0.9.8'
4
+ VERSION = '0.9.9'
5
5
  end
6
6
  end
7
7
  end
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'pathname'
4
4
 
5
5
  # Add all external dependencies for the plugin here
6
- gem 'dm-core', '~>0.9.8'
6
+ gem 'dm-core', '~>0.9.9'
7
7
  require 'dm-core'
8
8
 
9
9
  # Require plugin-files
@@ -22,7 +22,20 @@ class TrafficLight
22
22
  end
23
23
  end
24
24
 
25
+ before :transition!, :before_hook
26
+ after :transition!, :after_hook
27
+
28
+ def before_hook
29
+ before_hook_log << attribute_get(:color)
30
+ end
31
+
32
+ def after_hook
33
+ after_hook_log << attribute_get(:color)
34
+ end
35
+
25
36
  def log; @log ||= [] end
37
+ def before_hook_log; @bh_log ||= [] end
38
+ def after_hook_log; @ah_log ||= [] end
26
39
 
27
40
  attr_reader :init
28
41
  def initialize(*args)
@@ -147,4 +147,40 @@ describe TrafficLight do
147
147
 
148
148
  end
149
149
 
150
+ describe "hooks" do
151
+
152
+ it "should log initial state before state is changed on a before hook" do
153
+ @t.forward!
154
+ @t.before_hook_log.should == %w(green)
155
+ @t.forward!
156
+ @t.before_hook_log.should == %w(green yellow)
157
+ @t.forward!
158
+ @t.before_hook_log.should == %w(green yellow red)
159
+ end
160
+
161
+ it "should log final state before state is changed on a before hook" do
162
+ @t.forward!
163
+ @t.after_hook_log.should == %w(yellow)
164
+ @t.forward!
165
+ @t.after_hook_log.should == %w(yellow red)
166
+ @t.forward!
167
+ @t.after_hook_log.should == %w(yellow red green)
168
+ end
169
+
170
+ end
171
+
172
+ describe "overwriting event methods" do
173
+
174
+ before(:all) do
175
+ TrafficLight.class_eval "def forward!(added_param); log << added_param; transition!(:forward); end"
176
+ end
177
+
178
+ it "should transition normally with added functionality" do
179
+ @t.color.should == "green"
180
+ @t.forward!("test")
181
+ @t.color.should == "yellow"
182
+ @t.log.should == %w(G test Y)
183
+ end
184
+
185
+ end
150
186
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-is-state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - David James
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-07 00:00:00 -08:00
12
+ date: 2009-01-04 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ~>
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.8
23
+ version: 0.9.9
24
24
  version:
25
25
  description: DataMapper plugin for creating state machines
26
26
  email: