Class: Journea::StateMachine

Inherits:
Object
  • Object
show all
Includes:
Statesman::Machine
Defined in:
lib/journea/state_machine.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.step(*args) ⇒ Object



5
6
7
8
# 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

Instance Method Details

#in_initial_state?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/journea/state_machine.rb', line 24

def in_initial_state?
  current_state
end

#next_stepObject



15
16
17
18
# 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_stepObject



10
11
12
13
# File 'lib/journea/state_machine.rb', line 10

def previous_step
  return nil unless last_transition.present?
  last_transition.["previous_state"]
end

#transition_to_next_stepObject



20
21
22
# 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 }) ⇒ Object

Monkey patch to prevent hard fail on transitioning between unsupported routes



29
30
31
32
33
34
35
36
37
# 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