Sha256: 99b12975168389d1d13939f11ee05990e63a672333d5f11c0a812af7af544d82

Contents?: true

Size: 461 Bytes

Versions: 3

Compression:

Stored size: 461 Bytes

Contents

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
soveran-micromachine-0.0.7 lib/micromachine.rb
micromachine-0.0.6 lib/micromachine.rb
micromachine-0.0.7 lib/micromachine.rb