public
Rubygem
Description: Adds support for creating state machines for attributes on any Ruby class
Homepage: http://www.pluginaweek.org
Clone URL: git://github.com/pluginaweek/state_machine.git
Add PluginAWeek::StateMachine::Machine#states
Add PluginAWeek::StateMachine::Event#transitions
obrie (author)
Thu Jul 03 15:10:02 -0700 2008
commit  26d60afa4705981762799dfc36d46794b2bff370
tree    82158d75a977b57f20ee1bf8dcf529cefba2bfbf
parent  b90d731c157acfc564e61fedecb4819732536ec2
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 == master
0
 
0
+* Add PluginAWeek::StateMachine::Machine#states
0
+* Add PluginAWeek::StateMachine::Event#transitions
0
 * Allow creating transitions with no from state (effectively allowing the transition for *any* from state)
0
 * Reduce the number of objects created for each transition
0
 
...
11
12
13
 
 
 
14
15
16
...
21
22
23
 
24
25
26
...
63
64
65
 
66
67
68
...
11
12
13
14
15
16
17
18
19
...
24
25
26
27
28
29
30
...
67
68
69
70
71
72
73
0
@@ -11,6 +11,9 @@ module PluginAWeek #:nodoc:
0
       # The name of the action that fires the event
0
       attr_reader :name
0
       
0
+      # The list of transitions that can be made for this event
0
+      attr_reader :transitions
0
+      
0
       delegate  :owner_class,
0
                   :to => :machine
0
       
0
@@ -21,6 +24,7 @@ module PluginAWeek #:nodoc:
0
         @machine = machine
0
         @name = name
0
         @options = options.stringify_keys
0
+        @transitions = []
0
         
0
         add_transition_actions
0
         add_transition_callbacks
0
@@ -63,6 +67,7 @@ module PluginAWeek #:nodoc:
0
         callback = Proc.new {|record, *args| try_transition(transition, true, record, *args)}
0
         owner_class.send("transition_bang_on_#{name}", callback, options)
0
         
0
+        transitions << transition
0
         transition
0
       end
0
       
...
15
16
17
 
 
 
18
19
20
...
44
45
46
 
47
48
49
...
110
111
112
 
 
 
 
 
 
113
114
115
...
15
16
17
18
19
20
21
22
23
...
47
48
49
50
51
52
53
...
114
115
116
117
118
119
120
121
122
123
124
125
0
@@ -15,6 +15,9 @@ module PluginAWeek #:nodoc:
0
       # The events that trigger transitions
0
       attr_reader :events
0
       
0
+      # A list of the states defined in the transitions of all of the events
0
+      attr_reader :states
0
+      
0
       # The attribute for which the state machine is being defined
0
       attr_accessor :attribute
0
       
0
@@ -44,6 +47,7 @@ module PluginAWeek #:nodoc:
0
         @attribute = attribute.to_s
0
         @initial_state = options[:initial]
0
         @events = {}
0
+        @states = []
0
         
0
         add_named_scopes
0
       end
0
@@ -110,6 +114,12 @@ module PluginAWeek #:nodoc:
0
         name = name.to_s
0
         event = events[name] = Event.new(self, name, options)
0
         event.instance_eval(&block)
0
+        
0
+        # Record the states
0
+        event.transitions.each do |transition|
0
+          @states |= ([transition.to_state] + transition.from_states)
0
+        end
0
+        
0
         event
0
       end
0
       
...
14
15
16
 
 
 
 
17
18
19
...
81
82
83
 
 
 
 
 
84
85
86
...
14
15
16
17
18
19
20
21
22
23
...
85
86
87
88
89
90
91
92
93
94
95
0
@@ -14,6 +14,10 @@ class EventTest < Test::Unit::TestCase
0
     assert_equal 'turn_on', @event.name
0
   end
0
   
0
+  def test_should_not_have_any_transitions
0
+    assert @event.transitions.empty?
0
+  end
0
+  
0
   def test_should_define_an_event_action_on_the_owner_class
0
     switch = new_switch
0
     assert switch.respond_to?(:turn_on)
0
@@ -81,6 +85,11 @@ class EventWithTransitionsTest < Test::Unit::TestCase
0
     assert @event.transition(:to => 'on', :from => %w(off on))
0
   end
0
   
0
+  def test_should_have_transitions
0
+    @event.transition(:to => 'on')
0
+    assert @event.transitions.any?
0
+  end
0
+  
0
   def teardown
0
     Switch.class_eval do
0
       @transition_on_turn_on_callbacks = nil
...
20
21
22
 
 
 
 
23
24
25
...
121
122
123
124
 
125
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
128
129
...
20
21
22
23
24
25
26
27
28
29
...
125
126
127
 
128
129
 
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
0
@@ -20,6 +20,10 @@ class MachineByDefaultTest < Test::Unit::TestCase
0
   def test_should_not_have_any_events
0
     assert @machine.events.empty?
0
   end
0
+  
0
+  def test_should_not_have_any_states
0
+    assert @machine.states.empty?
0
+  end
0
 end
0
 
0
 class MachineWithInvalidOptionsTest < Test::Unit::TestCase
0
@@ -121,9 +125,27 @@ class MachineWithEventsTest < Test::Unit::TestCase
0
     assert responded
0
   end
0
   
0
-  def test_should_store_the_event
0
+  def test_should_have_events
0
     @machine.event(:turn_on) {}
0
-    assert_equal 1, @machine.events.size
0
+    assert_equal %w(turn_on), @machine.events.keys
0
+  end
0
+end
0
+
0
+class MachineWithEventsAndTransitionsTest < Test::Unit::TestCase
0
+  def setup
0
+    @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
0
+    @machine.event(:turn_on) do
0
+      transition :to => 'on', :from => 'off'
0
+      transition :to => 'error', :from => 'unknown'
0
+    end
0
+  end
0
+  
0
+  def test_should_have_events
0
+    assert_equal %w(turn_on), @machine.events.keys
0
+  end
0
+  
0
+  def test_should_have_states
0
+    assert_equal %w(on off error unknown), @machine.states
0
   end
0
 end
0
 

Comments