class GenericStateMachine::DSL::StateMachineDSL

StateMachineDSL provides convenient methods for creating a state machine

Attributes

hooks[R]
starting[R]
transitions[R]

Public Class Methods

create(&block) click to toggle source

Factory method creating the DSL helper object

# File dsl/state_machine_dsl.rb, line 63
def create(&block)
  obj = new
  obj.instance_eval(&block)

  obj
end

Public Instance Methods

register(hook:, handler:, condition: nil) click to toggle source

Add hook @param [Symbol] hook The name of the hook @param [Symbol] handler Name of the handler to be executed @param [Object] condition Optional condition determining whether the hook should be executed @raise [GenericStateMachine::Errors::DSLError] on any error

# File dsl/state_machine_dsl.rb, line 34
def register(hook:, handler:, condition: nil)
  raise GenericStateMachine::Errors::HookError, "Hook '#{hook}' isn't a valid hook" unless
      GenericStateMachine::AVAILABLE_HOOKS.include?(hook)
  raise GenericStateMachine::Errors::HookError, "Can't use value '#{hook}' as hook" if
      hook.nil? || !hook.is_a?(Symbol)

  @hooks ||= []
  @hooks << Hook.new(hook, handler, condition)
rescue StandardError => e
  raise GenericStateMachine::Errors::DSLError, e
end
start(from:) click to toggle source

Set the starting state @param [Symbol] from @raise GenericStateMachine::Errors::DSLError on any error

# File dsl/state_machine_dsl.rb, line 51
def start(from:)
  raise GenericStateMachine::Errors::StateError, "Can't use value '#{from}' as state" if
      from.nil? || !from.is_a?(Symbol)

  @starting = from
rescue StandardError => e
  raise GenericStateMachine::Errors::DSLError, e
end
transition(from:, to:, condition: true) click to toggle source

Add transition @param [Symbol] from The current state @param [Symbol] to The state to transit to @param [Object] condition An optional condition determining whether the transition should be executed @raise [GenericStateMachine::Errors::DSLError] on any error

# File dsl/state_machine_dsl.rb, line 20
def transition(from:, to:, condition: true)
  @transitions ||= []
  @transitions << Transition.new(from, to, condition)
rescue StandardError => e
  raise GenericStateMachine::Errors::DSLError, e
end