class Object

Public Instance Methods

create_back(from, to) { || ... } click to toggle source
# File lib/datashift_journey/state_machines/state_machine_core_ext.rb, line 21
def create_back(from, to)
  raise "Bad transitions supplied for Back - FROM #{from} - TO #{to}" if from.nil? || to.nil?
  if block_given?
    #puts "DEBUG: Creating BACK transition from #{from} to #{to} with Block"
    transition(from => to, on: :back, if: yield)
  else
    #puts "DEBUG: Creating BACK transition from #{from} to #{to}"
    transition(from => to, on: :back)
  end
end
create_back_transitions(journey, except = []) click to toggle source

BACK - Create a 'back' event for each step in list Automatically removes first state, as nothing to go back to from that state You can exclude any other steps with the except list

# File lib/datashift_journey/state_machines/state_machine_core_ext.rb, line 54
def create_back_transitions(journey, except = [])
  journey.drop(1).each_with_index do |t, i|
    next if except.include?(t)
    create_back(t, journey[i]) # n.b previous index is actually i not (i-1) due to the drop
  end
end
create_next(from, to) { || ... } click to toggle source

We use skip_fwd as the event type to avoid keyword next

This will add usual helpers like

vehicle.skip_fwd?                 # => true
vehicle.can_skip_fwd?             # => true
# File lib/datashift_journey/state_machines/state_machine_core_ext.rb, line 39
def create_next(from, to)
  raise "Bad transitions supplied for Next - FROM #{from} - TO #{to}" if from.nil? || to.nil?
  if block_given?
    #puts "DEBUG: Creating NEXT transition from #{from} to #{to} with Block "
    transition(from => to, on: :skip_fwd, if: yield)
  else
    #puts "DEBUG: Creating NEXT transition from #{from} to #{to}"
    transition(from => to, on: :skip_fwd)
  end
end
create_next_transitions(journey, except = []) click to toggle source

NEXT - Create a 'next' event for each step (apart from last) in journey You can exclude any other steps with the except list

# File lib/datashift_journey/state_machines/state_machine_core_ext.rb, line 64
def create_next_transitions(journey, except = [])

  #puts "DEBUG: Creating NEXT transitions for #{journey.inspect}"
  journey[0...-1].each_with_index do |t, i|
    next if except.include?(t)
    create_next(t, journey[i + 1])
  end
end
create_pair(lhs, rhs) click to toggle source

Create both a next link from lhs to rhs, and a back link from rhs to lhs

# File lib/datashift_journey/state_machines/state_machine_core_ext.rb, line 11
def create_pair(lhs, rhs)
  create_back(lhs, rhs)
  create_next(rhs, lhs)
end
create_pairs(sequence) click to toggle source
# File lib/datashift_journey/state_machines/state_machine_core_ext.rb, line 16
def create_pairs(sequence)
  create_back_transitions sequence
  create_next_transitions sequence
end