module Mealy::DSL

The class level DSL for defining machines.

Public Instance Methods

finish(&block) click to toggle source

final FSM state @param block fires on FSM shutdown

# File lib/mealy/dsl.rb, line 49
def finish(&block)
  @finish_data = block
end
initial_state(sym, &block) click to toggle source

Declares the initial state of the FSM. @param sym [Symbol] the initial state @param block user code executed in the instance of the FSM instance on

start up
# File lib/mealy/dsl.rb, line 13
def initial_state(sym, &block)
  @start_data = [sym, block]
end
read(state:, on: ANY, &block) click to toggle source

An FSM loop @param state [Array|Symbol] the state or states we loop on @param on [Label] only allows this rule to trigger if the read

token matches ({HelperMethods.Label} is automatically called
on this)

@param block user code executed on each iteration of the loop

# File lib/mealy/dsl.rb, line 41
def read(state:, on: ANY, &block)
  [* state].each do |one_state|
    transition(from: one_state, to: one_state, on: on, &block)
  end
end
transition(from:, to:, on: ANY, &block) click to toggle source

An FSM transition. @param from [Array|Symbol] the state or Array of states we transition

away from

@param to [Symbol] the state we transition to @param on [Label] only allows this rule to trigger if the read

token matches ({HelperMethods.Label} is automatically called
on this)

@param block user code executed when the rule fires @yieldparam input The read input, that matches the rules {Label} @yieldparam from The state we are transitioning away from @yieldparam to The state we are transitioning to

# File lib/mealy/dsl.rb, line 28
def transition(from:, to:, on: ANY, &block)
  hash = { HelperMethods.Label(on) => { to: to, block: block } }
  [* from].each do |origin|
    @transitions[origin] = @transitions[origin].merge(hash)
  end
end