class Mealy::Executer

@api private

An object on which {#run} behaves like Mealy{Mealy#execute}. The internal state is tracked by this instance, the user state is in {Mealy}.

Public Class Methods

new(mealy) click to toggle source

@param mealy [Mealy] mealy instance

# File lib/mealy/runner.rb, line 10
def initialize(mealy)
  @mealy = mealy
  @state = nil
end

Public Instance Methods

run(enum) click to toggle source

same as calling {Mealy#execute}

# File lib/mealy/runner.rb, line 16
def run(enum)
  start

  enum.each { |c| run_for_token(c) }

  finish
end

Private Instance Methods

finish() click to toggle source
# File lib/mealy/runner.rb, line 40
def finish
  user_action(finish_data)
end
lookup_transition_for(char) click to toggle source
# File lib/mealy/runner.rb, line 44
def lookup_transition_for(char)
  on_not_found = -> { raise UnexpectedTokenError.new(@state, char) }
  _, params = transitions[@state].find(on_not_found) do |key, _|
    key.match?(char)
  end
  params
end
run_for_token(token) click to toggle source
# File lib/mealy/runner.rb, line 31
def run_for_token(token)
  params = lookup_transition_for(token)
  block = params[:block]
  from = @state
  to = params[:to]
  @state = to
  user_action(block, token, from, to)
end
start() click to toggle source
# File lib/mealy/runner.rb, line 26
def start
  @state, block = start_data
  user_action(block)
end
user_action(user_action_block, *args) click to toggle source
# File lib/mealy/runner.rb, line 52
def user_action(user_action_block, *args)
  return if user_action_block.nil?

  @mealy.instance_exec(*args, &user_action_block)
end