Class: FiniteMachine::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/finite_machine/definition.rb

Overview

Responsible for defining a standalone state machine

Class Method Summary collapse

Class Method Details

.add_deferred(deferred) ⇒ Array<Proc>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add deferred

Parameters:

  • deferred (Proc)

    the deferred execution

Returns:

  • (Array<Proc>)


85
86
87
# File 'lib/finite_machine/definition.rb', line 85

def self.add_deferred(deferred)
  deferreds << deferred
end

.any_eventFiniteMachine::Const

The any event constant

Examples:

on_before(any_event) { ... }

Returns:



16
17
18
# File 'lib/finite_machine/definition.rb', line 16

def self.any_event
  ANY_EVENT
end

.any_stateFiniteMachine::Const

The any state constant

Examples:

event :go, any_state => :green
on_enter(any_state) { ... }

Returns:



31
32
33
# File 'lib/finite_machine/definition.rb', line 31

def self.any_state
  ANY_STATE
end

.deferredsArray<Proc>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The state machine deferreds

Returns:

  • (Array<Proc>)


73
74
75
# File 'lib/finite_machine/definition.rb', line 73

def self.deferreds
  @deferreds ||= []
end

.inherited(subclass) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Add deferred methods to the subclass

Parameters:

  • subclass (Class)

    the inheriting subclass



62
63
64
65
66
# File 'lib/finite_machine/definition.rb', line 62

def self.inherited(subclass)
  super

  deferreds.each { |d| subclass.add_deferred(d) }
end

.method_missing(method_name, *arguments, &block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Delay lookup of DSL method

Parameters:

  • method_name (Symbol)

    the method name

  • arguments (Array)

    the method arguments



99
100
101
102
103
104
105
# File 'lib/finite_machine/definition.rb', line 99

def self.method_missing(method_name, *arguments, &block)
  deferred = proc do |name, args, blok, object|
    object.send(name, *args, &blok)
  end
  deferred = deferred.curry(4)[method_name][arguments][block]
  add_deferred(deferred)
end

.new(*args) ⇒ FiniteMachine::StateMachine

Initialize a StateMachine

Examples:

class Engine < FiniteMachine::Definition
  ...
end

engine = Engine.new

Returns:



47
48
49
50
51
52
# File 'lib/finite_machine/definition.rb', line 47

def self.new(*args)
  context = self
  FiniteMachine.new(*args) do
    context.deferreds.each { |d| d.call(self) }
  end
end