Module: StateGate::Engine::Sequencer

Included in:
StateGate::Engine
Defined in:
lib/state_gate/engine/sequencer.rb

Overview

Description

Provides state sequence helper methods for StateGate::Engine.

All methods raise an error if sequential? is FALSE

Instance Method Summary collapse

Instance Method Details

#add_next_sequential_stateObject

Add the next sequential state



79
80
81
82
83
84
85
86
87
88
# File 'lib/state_gate/engine/sequencer.rb', line 79

def add_next_sequential_state
  next_state = nil
  @states.keys.reverse.each do |state|
    if next_state
      @states[state][:next_state] =      next_state
      @states[state][:transitions_to] << next_state
    end
    next_state = state
  end
end

#add_previous_sequential_stateObject

Add the previous sequential state



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/state_gate/engine/sequencer.rb', line 61

def add_previous_sequential_state
  return if @sequential_one_way

  previous_state = nil
  @states.keys.each do |state|
    if previous_state
      @states[state][:previous_state] =  previous_state
      @states[state][:transitions_to] << previous_state
    end
    previous_state = state
  end
end

#generate_sequencesObject

Add sequence hooks if sequential requested.



48
49
50
51
52
53
54
# File 'lib/state_gate/engine/sequencer.rb', line 48

def generate_sequences
  return unless sequential?

  add_previous_sequential_state
  add_next_sequential_state
  loop_sequence
end

#loop_sequenceObject

Add the first and last transitions to complete the sequential loop.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/state_gate/engine/sequencer.rb', line 95

def loop_sequence
  return unless @sequential_loop

  first_state = @states.keys.first
  last_state  = @states.keys.last

  @states[last_state][:next_state] =      first_state
  @states[last_state][:transitions_to] << first_state

  return if @sequential_one_way

  @states[first_state][:previous_state] =  last_state
  @states[first_state][:transitions_to] << last_state
end

#make_sequential(*args) ⇒ Object

Automatically add transitions from each state to the preceeding and following states.

make_sequential

Examples:

make_sequential :one_way
make_sequential :loop
make_sequential :one_way, :loop

Parameters:

  • args (Array[Symbol])

    the optional configuration arguments

Options Hash (*args):

  • :one_way (Symbol)

    Only adds tranitions from each state to the follow state. (optional)

  • :loop (Symbol)

    Adds transitions from the last state to the first and from the first to the last (unless also :one_way) (optional)



37
38
39
40
41
# File 'lib/state_gate/engine/sequencer.rb', line 37

def make_sequential(*args)
  @sequential         = true
  @sequential_loop    = true if args.include?(:loop)
  @sequential_one_way = true if args.include?(:one_way)
end

#sequential?Boolean

Returns true if the state_gate is sequential, otherwise false.

Examples:

.sequential?  #=> TRUE

Returns:

  • (Boolean)

    true if the state_gate is sequential, otherwise false.



123
124
125
# File 'lib/state_gate/engine/sequencer.rb', line 123

def sequential?
  !!@sequential
end