Module: Trailblazer::Developer::Render::Circuit

Defined in:
lib/trailblazer/developer/render/circuit.rb

Class Method Summary collapse

Class Method Details

.call(activity, path: nil, **options) ⇒ Object

Render an Activity‘s circuit as a simple hash.



14
15
16
17
18
19
20
21
22
23
# File 'lib/trailblazer/developer/render/circuit.rb', line 14

def call(activity, path: nil, **options)
  if path # TODO: move to place where all renderers can use this logic!
    node, _, graph   = Developer::Introspect.find_path(activity, path)
    activity  = node.task
  end

  graph = Activity::Introspect::Graph(activity)

  circuit_hash(graph, **options)
end

.circuit_hash(graph, **options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trailblazer/developer/render/circuit.rb', line 25

def circuit_hash(graph, **options)
  content = graph.collect do |node|
    conns = node.outgoings.collect do |outgoing|
      " {#{outgoing.output.signal}} => #{inspect_with_matcher(outgoing.task, **options)}"
    end

    [ inspect_with_matcher(node.task, **options), conns.join("\n") ]
  end

  content = content.join("\n")

  "\n#{content}".gsub(/0x\w+/, "0x")
end

.inspect_end(task) ⇒ Object



49
50
51
52
53
54
# File 'lib/trailblazer/developer/render/circuit.rb', line 49

def inspect_end(task)
  class_name = Render::Circuit.strip(task.class)
  options    = task.to_h

  "#<#{class_name}/#{options[:semantic].inspect}>"
end

.inspect_task(task) ⇒ Object



45
46
47
# File 'lib/trailblazer/developer/render/circuit.rb', line 45

def inspect_task(task)
  task.inspect
end

.inspect_with_matcher(task, inspect_task: method(:inspect_task), inspect_end: method(:inspect_end)) ⇒ Object

If Ruby had pattern matching, this function wasn’t necessary.



40
41
42
43
# File 'lib/trailblazer/developer/render/circuit.rb', line 40

def inspect_with_matcher(task, inspect_task: method(:inspect_task), inspect_end: method(:inspect_end))
  return inspect_task.(task) unless task.kind_of?(Trailblazer::Activity::End)
  inspect_end.(task)
end

.strip(string) ⇒ Object



56
57
58
# File 'lib/trailblazer/developer/render/circuit.rb', line 56

def self.strip(string)
  string.to_s.sub("Trailblazer::Activity::", "")
end