Module: Trailblazer::Activity::DSL::Linear::Helper::ClassMethods

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

Overview

Shortcut functions for the DSL.

Defined Under Namespace

Modules: Patch

Instance Method Summary collapse

Instance Method Details

#End(semantic) ⇒ Object



30
31
32
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 30

def End(semantic)
  Activity.End(semantic)
end

#end_id(_end) ⇒ Object



34
35
36
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 34

def end_id(_end)
  "End.#{_end.to_h[:semantic]}" # TODO: use everywhere
end

#Id(id) ⇒ Object



42
43
44
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 42

def Id(id)
  Id.new(id).freeze
end

#normalize(options, local_keys) ⇒ Object

TODO: test me.



141
142
143
144
145
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 141

def normalize(options, local_keys) # TODO: test me.
  locals  = options.reject { |key, value| ! local_keys.include?(key) }
  foreign = options.reject { |key, value| local_keys.include?(key) }
  return foreign, locals
end

#Output(signal, semantic = nil) ⇒ Object

Output( Left, :failure )

Output( :failure ) #=> Output::Semantic


24
25
26
27
28
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 24

def Output(signal, semantic=nil)
  return OutputSemantic.new(signal) if semantic.nil?

  Activity.Output(signal, semantic)
end

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



46
47
48
49
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
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 46

def Path(track_color: "track_#{rand}", connect_to: nil, before: false, **options, &block)
  path      = Activity::Path(track_name: track_color, **options)
  activity  = Class.new(path) { self.instance_exec(&block) }

  seq = activity.instance_variable_get(:@state).to_h[:sequence] # TODO: fix @state interface
  # Strip default ends `Start.default` and `End.success` (if present).
  seq = seq[1..-1].reject{ |row| row[3][:stop_event] && row[3][: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] ? Insert.method(:Append) : Insert.method(:Prepend)

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

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

  # Connect the Output() => Track(path_track)
  return Track.new(track_color, adds, {})
end

#Subprocess(activity, patch: {}) ⇒ Object

Computes the :outputs options for activity.



97
98
99
100
101
102
103
104
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 97

def Subprocess(activity, patch: {})
  activity = Patch.customize(activity, options: patch)

  {
    task:    activity,
    outputs: Hash[activity.to_h[:outputs].collect { |output| [output.semantic, output] }]
  }
end

#Track(color, wrap_around: false) ⇒ Object



38
39
40
# File 'lib/trailblazer/activity/dsl/linear/helper.rb', line 38

def Track(color, wrap_around: false)
  Track.new(color, [], wrap_around: wrap_around).freeze
end