module StateGate::Engine::Transitioner

Description

Provides transition helper methods for StateGate::Engine.

Public Instance Methods

assert_valid_transition!(current_state = nil, new_state = nil) click to toggle source

Returns TRUE if a transition is allowed, otherwise raises an exception.

.assert_valid_transition!(:pending, :active) # => true
.assert_valid_transition!(:active, :pending) # => ArgumentError
# File lib/state_gate/engine/transitioner.rb, line 60
def assert_valid_transition!(current_state = nil, new_state = nil)
  from_state = assert_valid_state!(current_state)
  to_state   = assert_valid_state!(new_state)

  return true if to_state == from_state
  return true if to_state.to_s.start_with?('force_')
  return true if @states[from_state][:transitions_to].include?(to_state)

  aerr(:invalid_state_transition_err, from: from_state, to: to_state, kattr: true)
end
transitionless?() click to toggle source

Returns TRUE if every state can transition to every other state, rendering transitions pointless.

.transitionless?  # => true
# File lib/state_gate/engine/transitioner.rb, line 18
def transitionless?
  !!@transitionless
end
transitions() click to toggle source

Returns a Hash of states and allowed transitions.

.transitions  # => { pending:   [:active],
              #      ativive:   [:suspended, :archived],
              #      suspended: [:active, :archived],
              #      archived:  [] }
# File lib/state_gate/engine/transitioner.rb, line 32
def transitions
  @transitions ||= begin
    transitions = {}
    @states.each { |k, v| transitions[k] = v[:transitions_to] } # rubocop:disable Layout/ExtraSpacing
    transitions
  end
end
transitions_for_state(state) click to toggle source

Return an Array of allowed transitions for the given state

.transitions_for_state(:active) # => [:suspended, :archived]
# File lib/state_gate/engine/transitioner.rb, line 47
def transitions_for_state(state)
  state_name = assert_valid_state!(state)
  transitions[state_name]
end