Module: DatashiftJourney::StateMachines::Planner

Defined in:
lib/datashift_journey/state_machines/planner.rb

Overview

Mixed into the State Machine class, so your JourneyPlan class has access to these methods and attributes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hash_klassObject



71
72
73
# File 'lib/datashift_journey/state_machines/planner.rb', line 71

def self.hash_klass
  ActiveSupport::HashWithIndifferentAccess
end

Instance Method Details

#branch_sequence(sequence_id, *list) ⇒ Object



67
68
69
# File 'lib/datashift_journey/state_machines/planner.rb', line 67

def branch_sequence(sequence_id, *list)
  branch_sequence_map.add_or_concat(sequence_id, list)
end

#branch_sequence_mapObject

Key - sequence ID



24
25
26
# File 'lib/datashift_journey/state_machines/planner.rb', line 24

def branch_sequence_map
  @branch_sequence_map ||= BranchSequenceMap.new
end

#init_planObject



13
14
15
16
# File 'lib/datashift_journey/state_machines/planner.rb', line 13

def init_plan
  sequence_list.clear                        # In development context where models get reloaded, this could duplicate
  branch_sequence_map.clear
end

#sequence(*list) ⇒ Object

Raises:



28
29
30
31
# File 'lib/datashift_journey/state_machines/planner.rb', line 28

def sequence(*list)
  raise PlannerApiError, 'Empty list passed to sequence - check your MachineBuilder syntax' if list.empty?
  sequence_list << Sequence.new(list.flatten)
end

#sequence_listObject

The complete, Ordered collection of sequences, used to generate the steps (states) of the plan



19
20
21
# File 'lib/datashift_journey/state_machines/planner.rb', line 19

def sequence_list
  @sequence_list ||= StateList.new
end

#split_on_equality(state, attr_reader, seq_to_target_value_map, _options = {}) ⇒ Object

Path splits down different branches, based on values stored on the main Journey model usually collected from from input e.g. radio, text or checkbox

Requires the starting or parent state, and the routing criteria to each target sequence

split_on_equality( :new_or_renew,
                   "what_branch?",             # Helper method on the journey Class
                   branch_1: 'branch_1',
                   branch_2: 'branch_2'
)

target_on_value_map is a hash mapping between the value collected from website and the associated named branch.

if value collected on parent state == stored target state, journey is routed down that branch


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datashift_journey/state_machines/planner.rb', line 48

def split_on_equality(state, attr_reader, seq_to_target_value_map, _options = {})
  unless seq_to_target_value_map.is_a? Hash
    raise 'BadDefinition - target_on_value_map must be hash map value => associated branch state'
  end

  sequence_list << Sequence.new(state, split: true)

  seq_to_target_value_map.each do |seq_id, trigger_value|
    if branch_sequence_map[seq_id]
      branch_sequence_map[seq_id].entry_state = state
      branch_sequence_map[seq_id].trigger_method = attr_reader
      branch_sequence_map[seq_id].trigger_value = trigger_value
    else
      seq = Sequence.new(nil, id: seq_id, entry_state: state, trigger_method: attr_reader, trigger_value: trigger_value)
      branch_sequence_map.add_branch(seq_id, seq)
    end
  end
end