class EnumStateMachine::Graph

Provides a set of higher-order features on top of the raw GraphViz graphs

Attributes

file_format[R]

The image format to generate the graph in

file_path[R]

The graph's full filename

font[R]

The name of the font to draw state names in

Public Class Methods

new(name, options = {}) click to toggle source

Creates a new graph with the given name.

Configuration options:

  • :path - The path to write the graph file to. Default is the current directory (“.”).

  • :format - The image format to generate the graph in. Default is “png'.

  • :font - The name of the font to draw state names in. Default is “Arial”.

  • :orientation - The direction of the graph (“portrait” or “landscape”). Default is “portrait”.

Calls superclass method
   # File lib/enum_state_machine/graph.rb
37 def initialize(name, options = {})
38   font = (RUBY_PLATFORM =~ /darwin/) ? 'ArialMT' : 'Arial'
39   options = {:path => '.', :format => 'png', :font => font, :orientation => 'portrait'}.merge(options)
40   assert_valid_keys(options, :path, :format, :font, :orientation)
41   
42   @font = options[:font]
43   @file_path = File.join(options[:path], "#{name}.#{options[:format]}")
44   @file_format = options[:format]
45   
46   super('G', :rankdir => options[:orientation] == 'landscape' ? 'LR' : 'TB')
47 end

Public Instance Methods

add_edges(*args) click to toggle source

Adds a new edge to the graph. The font for the edge will be automatically set based on the graph configuration. The generated edge will be returned.

For example,

graph = EnumStateMachine::Graph.new('test')
graph.add_edges('parked', 'idling', :label => 'ignite')
Calls superclass method
   # File lib/enum_state_machine/graph.rb
76 def add_edges(*args)
77   edge = v0_api? ? add_edge(*args) : super
78   edge.fontname = @font
79   edge
80 end
add_nodes(*args) click to toggle source

Adds a new node to the graph. The font for the node will be automatically set based on the graph configuration. The generated node will be returned.

For example,

graph = EnumStateMachine::Graph.new('test')
graph.add_nodes('parked', :label => 'Parked', :width => '1', :height => '1', :shape => 'ellipse')
Calls superclass method
   # File lib/enum_state_machine/graph.rb
63 def add_nodes(*args)
64   node = v0_api? ? add_node(*args) : super
65   node.fontname = @font
66   node
67 end
output() click to toggle source

Generates the actual image file based on the nodes / edges added to the graph. The path to the file is based on the configuration options for this graph.

Calls superclass method
   # File lib/enum_state_machine/graph.rb
52 def output
53   super(@file_format => @file_path)
54 end

Private Instance Methods

v0_api?() click to toggle source

Determines whether the old v0 api is in use

   # File lib/enum_state_machine/graph.rb
84 def v0_api?
85   version[0] == '0' || version[0] == '1' && version[1] == '0' && version[2] <= '2'
86 end
version() click to toggle source

The ruby-graphviz version data

   # File lib/enum_state_machine/graph.rb
89 def version
90   Constants::RGV_VERSION.split('.')
91 end