Class: StateMachine::Machine
- Inherits:
-
Object
- Object
- StateMachine::Machine
- Defined in:
- lib/ruby-state-machine/state_machine.rb
Instance Attribute Summary collapse
-
#default_state ⇒ Object
Returns the value of attribute default_state.
-
#events ⇒ Object
Returns the value of attribute events.
-
#states ⇒ Object
Returns the value of attribute states.
-
#transitions ⇒ Object
Returns the value of attribute transitions.
Instance Method Summary collapse
- #add_transition(opts) ⇒ Object
-
#initialize(opts) ⇒ Machine
constructor
A new instance of Machine.
-
#next_state(state, event) ⇒ Object
Next state (symbol only) for given state and event.
-
#next_state_instruction(state, event) ⇒ Object
Next state instruction (as hash) for given state and event:.
- #strings_to_sym(hash) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Machine
Returns a new instance of Machine.
188 189 190 191 192 193 |
# File 'lib/ruby-state-machine/state_machine.rb', line 188 def initialize(opts) @states=opts[:states].collect{|state| state.to_sym} @events=opts[:events].collect{|event| event.to_sym} @default_state=opts[:default_state].to_sym if opts[:default_state] @transitions=[] end |
Instance Attribute Details
#default_state ⇒ Object
Returns the value of attribute default_state.
186 187 188 |
# File 'lib/ruby-state-machine/state_machine.rb', line 186 def default_state @default_state end |
#events ⇒ Object
Returns the value of attribute events.
184 185 186 |
# File 'lib/ruby-state-machine/state_machine.rb', line 184 def events @events end |
#states ⇒ Object
Returns the value of attribute states.
183 184 185 |
# File 'lib/ruby-state-machine/state_machine.rb', line 183 def states @states end |
#transitions ⇒ Object
Returns the value of attribute transitions.
185 186 187 |
# File 'lib/ruby-state-machine/state_machine.rb', line 185 def transitions @transitions end |
Instance Method Details
#add_transition(opts) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ruby-state-machine/state_machine.rb', line 195 def add_transition(opts) new_opts=strings_to_sym(opts) # Some validation for arrays of actions: if (new_opts[:next].is_a?Array) if new_opts[:next].length==1 new_opts[:next] = new_opts[:next].first elsif new_opts[:next].length>1 raise ArgumentError, "A decider must be present for multiple actions." if String(new_opts[:decider]).empty? end end @transitions << new_opts end |
#next_state(state, event) ⇒ Object
Next state (symbol only) for given state and event. (equivalent to next_state_instruction(state, event))
232 233 234 235 236 |
# File 'lib/ruby-state-machine/state_machine.rb', line 232 def next_state(state, event) transition = next_state_instruction(state, event) ns = transition ? transition[:next] : nil (ns.is_a?Symbol) ? ns : ns && ns[:state] end |
#next_state_instruction(state, event) ⇒ Object
Next state instruction (as hash) for given state and event:
239 240 241 |
# File 'lib/ruby-state-machine/state_machine.rb', line 239 def next_state_instruction(state, event) @transitions.detect{|t| t[:state]==state and t[:event]==event} end |
#strings_to_sym(hash) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/ruby-state-machine/state_machine.rb', line 210 def strings_to_sym(hash) new_hash=hash.dup hash.each do |k, v| unless (v.nil? or (v.respond_to?:empty? and v.empty? )) if (v.is_a?(Hash)) new_hash[k] = strings_to_sym(v) elsif (v.is_a?(Array)) new_array = v.dup v.each_with_index do |array_hash, index| new_array[index] = strings_to_sym(array_hash) if array_hash.is_a?(Hash) end new_hash[k]=new_array elsif (v.is_a?(String)) new_hash[k] = v.to_sym end end end new_hash end |