class Journea::StateMachine

Public Class Methods

step(*args) click to toggle source
# File lib/journea/state_machine.rb, line 5
def self.step(*args)
  # Instead of using Statesman 'state', this let's us use 'step'
  state(*args)
end

Public Instance Methods

in_initial_state?() click to toggle source
# File lib/journea/state_machine.rb, line 24
def in_initial_state?
  current_state
end
next_step() click to toggle source
# File lib/journea/state_machine.rb, line 15
def next_step
  raise "No next step defined" if allowed_transitions.empty?
  allowed_transitions.first.to_sym
end
previous_step() click to toggle source
# File lib/journea/state_machine.rb, line 10
def previous_step
  return nil unless last_transition.present?
  last_transition.metadata["previous_state"]
end
transition_to_next_step() click to toggle source
# File lib/journea/state_machine.rb, line 20
def transition_to_next_step
  transition_to!(next_step, previous_state: current_state)
end
validate_transition(options = { from: nil, to: nil, metadata: nil }) click to toggle source

Monkey patch to prevent hard fail on transitioning between unsupported routes

# File lib/journea/state_machine.rb, line 29
def validate_transition(options = { from: nil, to: nil, metadata: nil })
  from = to_s_or_nil(options[:from])
  to   = to_s_or_nil(options[:to])

  # Call all guards, they raise exceptions if they fail
  guards_for(from: from, to: to).each do |guard|
    guard.call(@object, last_transition, options[:metadata])
  end
end