class Statesman::Diagram
Constants
- VERSION
Public Class Methods
new(name:, graph:)
click to toggle source
@param [String] name - name of the diagram. @param [Hash] graph - list of vertices and edges.
# File lib/statesman/diagram.rb, line 7 def initialize(name:, graph:) @name = name @graph = graph end
Public Instance Methods
to_dot()
click to toggle source
@return [String] diagram in DOT format.
# File lib/statesman/diagram.rb, line 13 def to_dot format("digraph %{name} {\n %{body}\n}", name: @name, body: dot_body.join("\n ")) end
to_png(file_name = nil)
click to toggle source
# File lib/statesman/diagram.rb, line 17 def to_png(file_name = nil) file_name ||= @name file_name += '.png' build_png(file_name) end
Private Instance Methods
build_png(file_name)
click to toggle source
# File lib/statesman/diagram.rb, line 35 def build_png(file_name) cmd = ['dot', '-Tpng', "-o#{file_name}"] puts "Running '#{cmd.join(' ')}' with this ^ as stdin..." output, status = Open3.capture2e(*cmd, stdin_data: to_dot) if status.success? puts "Success. You can open #{file_name} and see the diagram." else puts 'The command failed:' puts output end end
dot_body()
click to toggle source
@return [String]
# File lib/statesman/diagram.rb, line 27 def dot_body @graph.map do |vertex, edges| edges.map do |to| "#{vertex} -> #{to};" end end.flatten end