Class: SyntaxTree::Mermaid::FlowChart
- Inherits:
-
Object
- Object
- SyntaxTree::Mermaid::FlowChart
- Defined in:
- lib/syntax_tree/mermaid.rb
Overview
This is the main class that handles rendering a flowchart. It keeps track of its nodes and links and renders them according to the mermaid syntax.
Instance Attribute Summary collapse
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
-
#fetch(id) ⇒ Object
Retrieve a node that has already been added to the flowchart by its id.
-
#initialize ⇒ FlowChart
constructor
A new instance of FlowChart.
-
#link(from, to, label = nil, type: :directed, color: nil) ⇒ Object
Add a link to the flowchart between two nodes with an optional label.
-
#node(id, label = " ", shape: :rectangle) ⇒ Object
Add a node to the flowchart with an optional label.
-
#render ⇒ Object
Return the rendered flowchart.
-
#subgraph(label) ⇒ Object
Add a subgraph to the flowchart.
Constructor Details
#initialize ⇒ FlowChart
Returns a new instance of FlowChart.
15 16 17 18 19 20 21 22 |
# File 'lib/syntax_tree/mermaid.rb', line 15 def initialize @output = StringIO.new @output.puts("flowchart TD") @prefix = " " @nodes = {} @links = [] end |
Instance Attribute Details
#links ⇒ Object (readonly)
Returns the value of attribute links.
13 14 15 |
# File 'lib/syntax_tree/mermaid.rb', line 13 def links @links end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
13 14 15 |
# File 'lib/syntax_tree/mermaid.rb', line 13 def nodes @nodes end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
13 14 15 |
# File 'lib/syntax_tree/mermaid.rb', line 13 def output @output end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
13 14 15 |
# File 'lib/syntax_tree/mermaid.rb', line 13 def prefix @prefix end |
Instance Method Details
#fetch(id) ⇒ Object
Retrieve a node that has already been added to the flowchart by its id.
25 26 27 |
# File 'lib/syntax_tree/mermaid.rb', line 25 def fetch(id) nodes.fetch(id) end |
#link(from, to, label = nil, type: :directed, color: nil) ⇒ Object
Add a link to the flowchart between two nodes with an optional label.
30 31 32 33 34 35 36 |
# File 'lib/syntax_tree/mermaid.rb', line 30 def link(from, to, label = nil, type: :directed, color: nil) link = Link.new(from, to, label, type, color) links << link output.puts("#{prefix}#{link.render}") link end |
#node(id, label = " ", shape: :rectangle) ⇒ Object
Add a node to the flowchart with an optional label.
39 40 41 42 43 44 45 |
# File 'lib/syntax_tree/mermaid.rb', line 39 def node(id, label = " ", shape: :rectangle) node = Node.new(id, label, shape) nodes[id] = node output.puts("#{prefix}#{nodes[id].render}") node end |
#render ⇒ Object
Return the rendered flowchart.
64 65 66 67 68 69 70 71 72 |
# File 'lib/syntax_tree/mermaid.rb', line 64 def render links.each_with_index do |link, index| if link.color output.puts("#{prefix}linkStyle #{index} stroke:#{link.color}") end end output.string end |
#subgraph(label) ⇒ Object
Add a subgraph to the flowchart. Within the given block, all of the nodes will be rendered within the subgraph.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/syntax_tree/mermaid.rb', line 49 def subgraph(label) output.puts("#{prefix}subgraph #{Mermaid.escape(label)}") previous = prefix @prefix = "#{prefix} " begin yield ensure @prefix = previous output.puts("#{prefix}end") end end |