Module: Plumbing::Operation::Mermaid

Included in:
Plumbing::Operation
Defined in:
lib/plumbing/operation/mermaid.rb

Overview

Renders a Task's states as a mermaid flowchart TD. Pure function of the class's States — the structure is real; only edge labels are author text.

Constant Summary collapse

SHAPES =
{
  action: ->(name) { %(#{name}["#{name}"]) },
  decision: ->(name) { %(#{name}{"#{name}"}) },
  wait: ->(name) { %(#{name}{{"#{name}"}}) },
  result: ->(name) { %(#{name}(["#{name}"])) }
}.freeze

Instance Method Summary collapse

Instance Method Details

#to_mermaidObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/plumbing/operation/mermaid.rb', line 15

def to_mermaid
  lines = ["flowchart TD", "  start([Start]) --> #{start_state}"]
  states.each_value do |state|
    shape = SHAPES.fetch(state.kind).call(state.name)
    if state.transitions.length == 1 && state.transitions.first.label.nil?
      # Combine node shape and unconditional edge on one line
      lines << "  #{shape} --> #{state.transitions.first.target}"
    else
      lines << "  #{shape}"
      state.transitions.each do |transition|
        edge = transition.label.nil? ? "-->" : "-->|#{transition.label}|"
        lines << "  #{state.name} #{edge} #{transition.target}"
      end
    end
  end
  lines.join("\n")
end