lib/micromachine.rb in soveran-micromachine-0.0.5 vs lib/micromachine.rb in soveran-micromachine-0.0.6

- old
+ new

@@ -1,30 +1,42 @@ # Finite State Machine # # Usage: # -# fsm = MicroMachine.new(:new) # Initial state. +# machine = MicroMachine.new(:new) # Initial state. # -# fsm.transitions_for[:confirm] = { :new => :confirmed } -# fsm.transitions_for[:ignore] = { :new => :ignored } -# fsm.transitions_for[:reset] = { :confirmed => :new, :ignored => :new } +# machine.transitions_for[:confirm] = { :new => :confirmed } +# machine.transitions_for[:ignore] = { :new => :ignored } +# machine.transitions_for[:reset] = { :confirmed => :new, :ignored => :new } # -# fsm.trigger(:confirm) #=> true -# fsm.trigger(:ignore) #=> false -# fsm.trigger(:reset) #=> true -# fsm.trigger(:ignore) #=> true +# machine.trigger(:confirm) #=> true +# machine.trigger(:ignore) #=> false +# machine.trigger(:reset) #=> true +# machine.trigger(:ignore) #=> true # +# It also handles callbacks that are executed when entering a different state. +# +# machine.on(:confirmed) do +# puts "Confirmed" +# end class MicroMachine attr :transitions_for attr :state def initialize initial_state @state = initial_state @transitions_for = Hash.new + @callbacks = Hash.new { |hash, key| hash[key] = [] } end + def on key, &block + @callbacks[key] << block + end + def trigger event if transitions_for[event][@state] @state = transitions_for[event][@state] + @callbacks[@state].each { |callback| callback.call } + true end end end