Sha256: c67dde7ef2648e39c2c3492371fa59bf9b3ac36287c8407a653ce121e9576d32

Contents?: true

Size: 704 Bytes

Versions: 1

Compression:

Stored size: 704 Bytes

Contents

# Finite State Machine
#
# Usage:
#
#   fsm = 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 }
#
#   fsm.trigger(:confirm)  #=> true
#   fsm.trigger(:ignore)   #=> false
#   fsm.trigger(:reset)    #=> true
#   fsm.trigger(:ignore)   #=> true
#
class MicroMachine
  attr :transitions_for
  attr :state

  def initialize initial_state
    @state = initial_state
    @transitions_for = Hash.new
  end

  def trigger event
    if transitions_for[event][@state]
      @state = transitions_for[event][@state]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soveran-micromachine-0.0.5 lib/micromachine.rb