class GenericStateMachine::StateMachineFactory
Factory class creating StateMachine
instances
Public Class Methods
create(start:, transitions:, hooks: [])
click to toggle source
Create a StateMachine
instance @param [Symbol] start The starting state @param [Array] transitions All available transitions @param [Array] hooks Optional collection of hooks @raise GenericStateMachine::Errors::GenericStateMachineError
# File state_machine/state_machine_factory.rb, line 17 def create(start:, transitions:, hooks: []) raise GenericStateMachine::Errors::GenericStateMachineError, "Can't create state machine w/o starting state" if start.nil? || !start.is_a?(Symbol) raise GenericStateMachine::Errors::GenericStateMachineError, "Can't create state machine w/o transitions" unless transitions.is_a?(Array) raise GenericStateMachine::Errors::GenericStateMachineError, "Can't create state machine w/o transitions" if transitions.empty? _validate_transitions transitions _validate_start_state start, transitions _validate_hooks(hooks) unless hooks.empty? GenericStateMachine::StateMachine.new start end
Private Class Methods
_validate_hooks(hooks)
click to toggle source
Helper method validating if all elements are hooks raise GenericStateMachine::Errors::GenericStateMachineError
if one of the elements is no Hook instance
# File state_machine/state_machine_factory.rb, line 67 def _validate_hooks(hooks) hooks.each do |t| raise GenericStateMachine::Errors::GenericStateMachineError, "Element '#{t}' isn't a hook" unless t.is_a?(GenericStateMachine::Hook) end end
_validate_start_state(start, transitions)
click to toggle source
Helper method validating if given start state has a transition raise GenericStateMachine::Errors::GenericStateMachineError
if no transition is found
# File state_machine/state_machine_factory.rb, line 38 def _validate_start_state(start, transitions) valid = false transitions.each do |t| if t.from == start valid = true break end end raise GenericStateMachine::Errors::GenericStateMachineError, "State '#{start}' isn't a valid start state" unless valid end
_validate_transitions(transitions)
click to toggle source
Helper method validating if all elements are transitions raise GenericStateMachine::Errors::GenericStateMachineError
if one of the elements is no Transition
instance
# File state_machine/state_machine_factory.rb, line 56 def _validate_transitions(transitions) transitions.each do |t| raise GenericStateMachine::Errors::GenericStateMachineError, "Element '#{t}' isn't a transition" unless t.is_a?(GenericStateMachine::Transition) end end