class NMachine

Constants

InvalidEventError

Attributes

state[R]

Public Class Methods

new(initial_state) click to toggle source
# File lib/nmachine.rb, line 6
def initialize(initial_state)
  @state = initial_state
  @events = Hash.new
end

Public Instance Methods

when(event, transistions = {}, &block) click to toggle source
# File lib/nmachine.rb, line 11
def when(event, transistions = {}, &block)
  @events[event] = [transistions, block]
end

Private Instance Methods

ensure_allowed(event) click to toggle source
# File lib/nmachine.rb, line 24
def ensure_allowed(event)
  raise InvalidEventError.new("#{event} not valid from #{state}") unless @events[event][0].has_key?(state)
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/nmachine.rb, line 28
def method_missing(m, *args, &block)
  if @events.has_key?(m) then transition(m) else super end
end
transition(event) click to toggle source
# File lib/nmachine.rb, line 17
def transition(event)
  ensure_allowed(event)
  @state = @events[event][0][state]
  @events[event][1]&.call
  self
end