Class: EnumStateMachine::Graph
- Inherits:
-
GraphViz
- Object
- GraphViz
- EnumStateMachine::Graph
- Includes:
- Assertions
- Defined in:
- lib/enum_state_machine/graph.rb
Overview
Provides a set of higher-order features on top of the raw GraphViz graphs
Instance Attribute Summary collapse
-
#file_format ⇒ Object
readonly
The image format to generate the graph in.
-
#file_path ⇒ Object
readonly
The graph’s full filename.
-
#font ⇒ Object
readonly
The name of the font to draw state names in.
Instance Method Summary collapse
-
#add_edges(*args) ⇒ Object
Adds a new edge to the graph.
-
#add_nodes(*args) ⇒ Object
Adds a new node to the graph.
-
#initialize(name, options = {}) ⇒ Graph
constructor
Creates a new graph with the given name.
-
#output ⇒ Object
Generates the actual image file based on the nodes / edges added to the graph.
Methods included from Assertions
#assert_exclusive_keys, #assert_valid_keys
Constructor Details
#initialize(name, options = {}) ⇒ Graph
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”.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/enum_state_machine/graph.rb', line 37 def initialize(name, = {}) font = (RUBY_PLATFORM =~ /darwin/) ? 'ArialMT' : 'Arial' = {:path => '.', :format => 'png', :font => font, :orientation => 'portrait'}.merge() assert_valid_keys(, :path, :format, :font, :orientation) @font = [:font] @file_path = File.join([:path], "#{name}.#{[:format]}") @file_format = [:format] super('G', :rankdir => [:orientation] == 'landscape' ? 'LR' : 'TB') end |
Instance Attribute Details
#file_format ⇒ Object (readonly)
The image format to generate the graph in
24 25 26 |
# File 'lib/enum_state_machine/graph.rb', line 24 def file_format @file_format end |
#file_path ⇒ Object (readonly)
The graph’s full filename
21 22 23 |
# File 'lib/enum_state_machine/graph.rb', line 21 def file_path @file_path end |
#font ⇒ Object (readonly)
The name of the font to draw state names in
18 19 20 |
# File 'lib/enum_state_machine/graph.rb', line 18 def font @font end |
Instance Method Details
#add_edges(*args) ⇒ Object
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')
76 77 78 79 80 |
# File 'lib/enum_state_machine/graph.rb', line 76 def add_edges(*args) edge = v0_api? ? add_edge(*args) : super edge.fontname = @font edge end |
#add_nodes(*args) ⇒ Object
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')
63 64 65 66 67 |
# File 'lib/enum_state_machine/graph.rb', line 63 def add_nodes(*args) node = v0_api? ? add_node(*args) : super node.fontname = @font node end |
#output ⇒ Object
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.
52 53 54 |
# File 'lib/enum_state_machine/graph.rb', line 52 def output super(@file_format => @file_path) end |