Module: Mealy

Defined in:
lib/mealy.rb,
lib/mealy/dsl.rb,
lib/mealy/label.rb,
lib/mealy/runner.rb,
lib/mealy/helper_methods.rb

Overview

A Mealy finite state machine.

For usage information please read README.

Defined Under Namespace

Modules: DSL, HelperMethods Classes: AnyLabel, Executer, Label, Runner, UnexpectedTokenError

Constant Summary collapse

ANY =

Wildcard for machine input tokens that match anything.

AnyLabel.instance

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Module.included hook. Resets the state transitions for a class



55
56
57
58
# File 'lib/mealy/dsl.rb', line 55

def self.included(klass)
  klass.class_eval { @transitions = Hash.new { Hash.new({}) } }
  klass.extend(DSL)
end

Instance Method Details

#emit(token) ⇒ Object

emit tokens from the DSL blocks

Parameters:

  • token

    the emitted token



19
20
21
22
# File 'lib/mealy.rb', line 19

def emit(token)
  return unless @emit_runner
  @emit_runner.emit(token)
end

#execute(enum) ⇒ Object

Runs the Mealy machine on the given input.

Parameters:

  • enum (Enumerable)

    the input for the FSM

Returns:



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

def execute(enum)
  Executer.new(self).run(enum)
end

#run(enum) {|emit| ... } ⇒ Enumerator

Runs the Mealy machine on the given input. Outputs a stream of tokens by yielding each emitted token to the given block.

Parameters:

  • enum (Enumerable)

    the input for the FSM

Yield Parameters:

  • emit

    The user token emitted by #emit.

Returns:

  • (Enumerator)

    if no block is given



29
30
31
32
33
34
35
# File 'lib/mealy.rb', line 29

def run(enum, &block)
  return to_enum(:run, enum) unless block_given?

  @emit_runner = Runner.new(self)
  @emit_runner.run(enum, &block)
  @emit_runner = nil
end