class Lab42::StateMachine::State

Attributes

designation[R]
triggers[R]

Public Class Methods

new(designation) click to toggle source
# File lib/lab42/state_machine/state.rb, line 60
def initialize(designation)
  @designation = designation
  @triggers    = []
end

Public Instance Methods

_match(input, trigger) click to toggle source
# File lib/lab42/state_machine/state.rb, line 24
def _match(input, trigger)
  case trigger
  when Lab42::Match
    m =trigger.match(input)
    m.success? && m
  when TrueClass
    true
  when FalseClass
    raise StopIteration
  when Symbol
    input.send trigger
  end
end
add(trigger, action, new_state) click to toggle source
# File lib/lab42/state_machine/state.rb, line 7
def add(trigger, action, new_state)
  trigger = Lab42::Match.new(trigger) if Regexp === trigger
  new_state ||= designation
  @triggers << [trigger, action, new_state]
end
freeze() click to toggle source
Calls superclass method
# File lib/lab42/state_machine/state.rb, line 38
def freeze
  super
  @triggers.freeze
end
transition(accumulator, input) click to toggle source
# File lib/lab42/state_machine/state.rb, line 13
def transition(accumulator, input)
  triggers.each do |trigger, action, new_state|
    match = _match input, trigger
    next unless match
    output, new_acc, new_state1 = _apply(match, accumulator, input: input, to: action)
    output = output.string if Lab42::Match === output
    return [output, new_acc || accumulator, new_state1 || new_state || designation]
  end
  [input, accumulator, designation]
end

Private Instance Methods

_apply(match, accumulator, input:, to:) click to toggle source
# File lib/lab42/state_machine/state.rb, line 45
def _apply(match, accumulator, input:, to:)
  if to
    case to.arity
    when 1
      to.(match)
    when 2
      to.(accumulator, match)
    else
      raise ArgumentError, "#{to} needs to accept one or two parameters , as it is a State's action"
    end
  else
    input
  end
end