Module: Trailblazer::Activity::DSL::Linear::Helper::Path

Defined in:
lib/trailblazer/activity/dsl/linear/helper/path.rb

Overview

Normalizer logic for do … end.

TODO: it would be cool to be able to connect an (empty) path to specific termini,

this would work if we could add multiple magnetic_to.

Defined Under Namespace

Modules: Normalizer

Class Method Summary collapse

Class Method Details

.convert_path_to_track(track_color: "track_#{rand}", connect_to: nil, before: false, block: nil, terminus: nil, **options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/trailblazer/activity/dsl/linear/helper/path.rb', line 50

def convert_path_to_track(track_color: "track_#{rand}", connect_to: nil, before: false, block: nil, terminus: nil, **options)
  options =
    if end_task = options[:end_task] # TODO: remove in 2.0.
      Activity::Deprecate.warn Linear::Deprecate.dsl_caller_location,
        %(Using `:end_task` and `:end_id` in Path() is deprecated, use `:terminus` instead. Please refer to https://trailblazer.to/2.1/docs/activity.html#activity-wiring-api-path-end_task-end_id-deprecation)

      options.merge(
        end_task: Activity.End(end_task.to_h[:semantic]),
        end_id:   options[:end_id]
      )
    elsif connect_to
      {}
    elsif terminus
      options.merge(
        end_task: Activity.End(terminus),
        end_id:   "End.#{terminus}"
      )
    else # Path() with End() inside block.
      {}
    end

  # DISCUSS:  if anyone overrides `#step` in the "outer" activity, this won't be applied inside the branch.

  # DISCUSS: use Path::Sequencer::Builder here instead?
  path = Activity::Path(**options, track_name: track_color, &block)

  seq = path.to_h[:sequence]
  # Strip default ends `Start.default` and `End.success` (if present).
  seq = seq[1..-1].reject { |row| row[3][:stop_event] && row.id == "End.success" }

  if connect_to
    seq = connect_for_sequence(seq, connect_to: connect_to)
  end

  # Add the path elements before {End.success}.
  # Termini (or :stop_event) are to be placed after {End.success}.
  adds = seq.collect do |row|
    options = row[3]

    # the terminus of the path goes _after_ {End.success} into the "end group".
    insert_method = options[:stop_event] ? Activity::Adds::Insert.method(:Append) : Activity::Adds::Insert.method(:Prepend)

    insert_target = "End.success" # insert before/after
    insert_target = before if before && connect_to.instance_of?(Linear::Normalizer::OutputTuples::Track) # FIXME: this is a bit hacky, of course!

    {
      row:    row,
      insert: [insert_method, insert_target]
    }
  end

  # Connect the Output() => Track(path_track)
  Linear::Normalizer::OutputTuples::Track.new(track_color, adds, {})
end