Module: StateMachine::ClassMethods

Defined in:
lib/ruby-state-machine/state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#machineObject

Returns the value of attribute machine.



18
19
20
# File 'lib/ruby-state-machine/state_machine.rb', line 18

def machine
  @machine
end

Instance Method Details

#default_stateObject



57
# File 'lib/ruby-state-machine/state_machine.rb', line 57

def default_state; @machine.default_state; end

#eventsObject



59
# File 'lib/ruby-state-machine/state_machine.rb', line 59

def events; @machine.events; end

#next_state(*args) ⇒ Object



61
# File 'lib/ruby-state-machine/state_machine.rb', line 61

def next_state(*args); @machine.next_state(*args); end

#next_state_instruction(*args) ⇒ Object



62
# File 'lib/ruby-state-machine/state_machine.rb', line 62

def next_state_instruction(*args); @machine.next_state_instruction(*args); end

#state_machine(opts) ⇒ Object

Create state machine with the following options:

states Array[symbols], each symbol is name of state
events Array[symbols], each symbol is name of event
default_state symbol, symbol is one of states


24
25
26
27
# File 'lib/ruby-state-machine/state_machine.rb', line 24

def state_machine(opts)
  @machine = StateMachine::Machine.new(opts)
  self.send(:include, StateMachine::InstanceMethods)
end

#state_transition(opts) ⇒ Object

Add a transition to state machine with the following keys: :state=>state (symbol) for which transition is being declared :event=>event (symbol) which causes transition :decider=>name of instance method on state machine specialization that “decides”

next state in case of multiple actions for next state (see below).
Method in question should return name of next state, or name of action
if actions are named (not required).

:next=>action to take for this transition, one of:

1) state name(symbol), or special symbol (:stay), which stays at current state
2) hash{:state=>state name(symbol), :action=>code/lambda(string), :name=>id of action, for decider}
3) array of 1 and/or 2.  Decider required in this case.

Example 1:

state_transition :state=>:c_state, :event=>:w_event, :decider => :c_state_decider, 
  :next=>[
    {:state=>:a_state, :action=>lambda{|o, args| o.increment_value(7)} }, 
    {:state=>:b_state, :action=>lambda{|o, args| o.increment_value(-10)} }
  ]

Example 2 (stays at current state):

state_transition :state=>:c_state, :event=>:y_event, :next=>{:state=>:stay, :action=>lambda{|o, args| o.increment_value(-3)}}

Example 3:

state_transition :state=>:b_state, :event=>:z_event, :next=>:a_state


53
54
55
# File 'lib/ruby-state-machine/state_machine.rb', line 53

def state_transition(opts)
  @machine.add_transition(opts)
end

#statesObject



58
# File 'lib/ruby-state-machine/state_machine.rb', line 58

def states; @machine.states; end

#transitionsObject



60
# File 'lib/ruby-state-machine/state_machine.rb', line 60

def transitions; @machine.transitions; end