Class: ActiverecordCallbackLens::Renderer::GraphvizRenderer
- Inherits:
-
Object
- Object
- ActiverecordCallbackLens::Renderer::GraphvizRenderer
- Defined in:
- lib/activerecord_callback_lens/renderer/graphviz_renderer.rb
Overview
Renders a Graph::Graph as a Graphviz DOT language string.
The output is a ‘digraph` block with a left-to-right rank direction, one declaration per node and one arrow per edge:
digraph callback_lens {
rankdir=LR;
n0 [label="before_save"];
n1 [label="active?"];
n1 -> n0;
}
Node labels are derived from the node type (see #node_label) using the same four-case logic as MermaidRenderer. Double quotes in a label are escaped as ‘"` so they cannot break the surrounding DOT label syntax.
Class Method Summary collapse
-
.render(graph) ⇒ String
DOT language string.
Instance Method Summary collapse
-
#initialize(graph) ⇒ GraphvizRenderer
constructor
A new instance of GraphvizRenderer.
-
#render ⇒ String
DOT language string.
-
#to_svg ⇒ String?
Shells out to the ‘dot` binary and returns the rendered SVG string.
Constructor Details
#initialize(graph) ⇒ GraphvizRenderer
Returns a new instance of GraphvizRenderer.
30 31 32 |
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 30 def initialize(graph) @graph = graph end |
Class Method Details
.render(graph) ⇒ String
Returns DOT language string.
25 26 27 |
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 25 def self.render(graph) new(graph).render end |
Instance Method Details
#render ⇒ String
Returns DOT language string.
35 36 37 38 39 40 41 |
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 35 def render lines = ["digraph callback_lens {", " rankdir=LR;"] lines.concat(node_declarations) lines.concat(edge_declarations) lines << "}" lines.join("\n") end |
#to_svg ⇒ String?
Shells out to the ‘dot` binary and returns the rendered SVG string. Returns nil when Graphviz is not installed (the `dot` binary is absent from PATH) rather than raising.
48 49 50 51 52 53 54 55 56 |
# File 'lib/activerecord_callback_lens/renderer/graphviz_renderer.rb', line 48 def to_svg IO.popen(["dot", "-Tsvg"], "r+") do |io| io.write(render) io.close_write io.read end rescue Errno::ENOENT nil end |