Module: Mealy::DSL

Defined in:
lib/mealy/dsl.rb

Overview

The class level DSL for defining machines.

Instance Method Summary collapse

Instance Method Details

#finish(&block) ⇒ Object

final FSM state

Parameters:

  • block

    fires on FSM shutdown



48
49
50
# File 'lib/mealy/dsl.rb', line 48

def finish(&block)
  @finish_data = block
end

#initial_state(sym, &block) ⇒ Object

Declares the initial state of the FSM.

Parameters:

  • sym (Symbol)

    the initial state

  • block

    user code executed in the instance of the FSM on start up



12
13
14
# File 'lib/mealy/dsl.rb', line 12

def initial_state(sym, &block)
  @start_data = [sym, block]
end

#read(state:, on: ANY, &block) ⇒ Object

An FSM loop

Parameters:

  • state (Array|Symbol)

    the state or states we loop on

  • on (Label)

    only allows this rule to trigger if the read token matches (HelperMethods.Label is automatically called on this)

  • block

    user code executed on each iteration of the loop



40
41
42
43
44
# File 'lib/mealy/dsl.rb', line 40

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) {|input, from, to| ... } ⇒ Object

An FSM transition.

Parameters:

  • from (Array|Symbol)

    the state or Array of states we transition away from

  • to (Symbol)

    the state we transition to

  • on (Label)

    only allows this rule to trigger if the read token matches (HelperMethods.Label is automatically called on this)

  • block

    user code executed when the rule fires

Yield Parameters:

  • input

    The read input, that matches the rules Label

  • from

    The state we are transitioning away from

  • to

    The state we are transitioning to



27
28
29
30
31
32
# File 'lib/mealy/dsl.rb', line 27

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