barebone-fsm 0.0.1.3 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -2
- data/lib/barebone-fsm.rb +12 -10
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -21,7 +21,6 @@ Apart from having support for states and events, this module offers the followin
|
|
21
21
|
3. Entry and exit events for each state
|
22
22
|
4. DSL like coding style
|
23
23
|
5. Access to state variables (including @state and @event) inside event blocks.
|
24
|
-
Currently the state variables can only be shared among event blocks of the same state.
|
25
24
|
|
26
25
|
== Usage
|
27
26
|
The FSM can be setup and triggered Succinctly using Domain Specific Language(DSL) like coding style.
|
@@ -78,8 +77,10 @@ The api is not stable yet, it may go trhough lots of changes before the first st
|
|
78
77
|
I am open to any suggestion or request to support custom features.
|
79
78
|
|
80
79
|
== Changes
|
80
|
+
* Version: 0.0.2
|
81
|
+
* State variables defined in other states can be accessed in the event block as well.
|
81
82
|
* Version: 0.0.1.3
|
82
|
-
* Minor fixes on the usage examples
|
83
|
+
* Minor fixes on the usage examples.
|
83
84
|
* Version: 0.0.1.2
|
84
85
|
* @state instance variable for FSM is dropped in favour of the #state instance method.
|
85
86
|
* #state method and index operator [] now accept nil arguement for state name, which returns the current state.
|
data/lib/barebone-fsm.rb
CHANGED
@@ -35,7 +35,8 @@ module FSM
|
|
35
35
|
# Though it can have any data type, usage of symbol or string is preferable.
|
36
36
|
attr_reader :state
|
37
37
|
|
38
|
-
def initialize(state_name)
|
38
|
+
def initialize(state_machine, state_name)
|
39
|
+
@fsm = state_machine
|
39
40
|
@state = state_name
|
40
41
|
@events = {}
|
41
42
|
end
|
@@ -54,11 +55,11 @@ module FSM
|
|
54
55
|
if event_name and block_given? then
|
55
56
|
@events[event_name] = event_block
|
56
57
|
elsif event_name and @events.has_key? event_name then
|
57
|
-
@event = event_name
|
58
|
-
|
58
|
+
@fsm.event = event_name
|
59
|
+
@fsm.instance_eval &@events[event_name]
|
59
60
|
elsif @events.has_key? :default then
|
60
|
-
@event = :default
|
61
|
-
|
61
|
+
@fsm.event = :default
|
62
|
+
@fsm.instance_eval &@events[:default]
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
@@ -90,12 +91,12 @@ module FSM
|
|
90
91
|
# end
|
91
92
|
#
|
92
93
|
class FSM
|
93
|
-
|
94
|
+
attr_writer :event
|
94
95
|
def initialize(default_state=nil)
|
95
96
|
@states = {}
|
96
97
|
if default_state then
|
97
98
|
@state = @default = default_state
|
98
|
-
@states[@state] = FSMState.new(@state)
|
99
|
+
@states[@state] = FSMState.new(self, @state)
|
99
100
|
end
|
100
101
|
end
|
101
102
|
|
@@ -113,7 +114,7 @@ module FSM
|
|
113
114
|
def [](state_name=nil)
|
114
115
|
state_name ||= @state
|
115
116
|
if state_name and not @states.has_key? state_name then
|
116
|
-
@states[state_name] = FSMState.new(state_name)
|
117
|
+
@states[state_name] = FSMState.new(self, state_name)
|
117
118
|
@state ||= state_name
|
118
119
|
end
|
119
120
|
@states[state_name]
|
@@ -133,11 +134,12 @@ module FSM
|
|
133
134
|
# It triggers the event_name event and changes the state of the FSM to its new state.
|
134
135
|
# The :entry and :exit events are called on the leaving state and the entering state.
|
135
136
|
# If the event does not mention the new state, then the state changes to the default state.
|
136
|
-
def event(event_name)
|
137
|
+
def event(event_name)
|
138
|
+
@event = event_name
|
137
139
|
@states[@state].event :exit
|
138
140
|
new_state = @states[@state].event event_name
|
139
141
|
new_state = nil if not @states.has_key? new_state
|
140
|
-
new_state ||= @
|
142
|
+
new_state ||= @default
|
141
143
|
new_state ||= @state
|
142
144
|
@state = new_state
|
143
145
|
@states[@state].event :enter
|