class FiniteMachine::StateDefinition

A class responsible for defining state query methods on state machine

Used by {TranstionBuilder} to add state query definition to the {StateMachine} instance.

@api private

Attributes

machine[R]

The current state machine

Public Class Methods

new(machine) click to toggle source

Initialize a StateDefinition

@param [StateMachine] machine

@api public

# File lib/finite_machine/state_definition.rb, line 16
def initialize(machine)
  @machine = machine
end

Public Instance Methods

apply(states) click to toggle source

Define query methods for states

@param [Hash] states

the states that require query helpers

@return [nil]

@api public

# File lib/finite_machine/state_definition.rb, line 28
def apply(states)
  define_state_query_methods(states)
end

Private Instance Methods

define_state_query_method(state) click to toggle source

Define state helper method

@param [Symbol] state

the state to define helper for

@api private

# File lib/finite_machine/state_definition.rb, line 57
def define_state_query_method(state)
  return if machine.respond_to?("#{state}?")
  machine.send(:define_singleton_method, "#{state}?") do
    machine.is?(state.to_sym)
  end
end
define_state_query_methods(states) click to toggle source

Define helper state mehods for the transition states

@param [Hash] states

the states to define helpers for

@return [nil]

@api private

# File lib/finite_machine/state_definition.rb, line 45
def define_state_query_methods(states)
  states.to_a.flatten.each do |state|
    define_state_query_method(state)
  end
end