Class: Graphviz::Node
- Inherits:
-
Object
- Object
- Graphviz::Node
- Defined in:
- lib/graphviz/node.rb
Overview
Represents a visual node in the graph, which can be connected to other nodes.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
Any attributes specified for this node.
-
#connections ⇒ Array<Edge>
readonly
Any edges connecting to other nodes.
-
#name ⇒ String
readonly
The unique name of the node.
Instance Method Summary collapse
-
#add_node(name = nil, **attributes) ⇒ Object
Add a node and #connect to it.
-
#attach(parent) ⇒ Object
Attach this node to the given graph:.
-
#connect(destination, attributes = {}) ⇒ Object
Create an edge between this node and the destination with the specified options.
-
#connected?(node) ⇒ Boolean
Calculate if this node is connected to another.
-
#dump_attributes(attributes) ⇒ Object
Dump the attributes to dot text format.
- #dump_graph(buffer, indent, **options) ⇒ Object
-
#dump_value(value) ⇒ Object
Dump the value to dot text format.
- #identifier ⇒ Object
-
#initialize(name, graph = nil, **attributes) ⇒ Node
constructor
Initialize the node in the graph with the unique name.
- #to_s ⇒ Object
Constructor Details
#initialize(name, graph = nil, **attributes) ⇒ Node
Initialize the node in the graph with the unique name.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/graphviz/node.rb', line 28 def initialize(name, graph = nil, **attributes) @name = name @attributes = attributes @connections = [] # This sets up the connection between the node and the parent. @graph = nil graph << self if graph end |
Instance Attribute Details
#attributes ⇒ Hash
Returns Any attributes specified for this node.
51 52 53 |
# File 'lib/graphviz/node.rb', line 51 def attributes @attributes end |
#connections ⇒ Array<Edge> (readonly)
Returns Any edges connecting to other nodes.
48 49 50 |
# File 'lib/graphviz/node.rb', line 48 def connections @connections end |
#name ⇒ String (readonly)
Returns The unique name of the node.
45 46 47 |
# File 'lib/graphviz/node.rb', line 45 def name @name end |
Instance Method Details
#add_node(name = nil, **attributes) ⇒ Object
Add a node and #connect to it.
70 71 72 73 74 75 76 |
# File 'lib/graphviz/node.rb', line 70 def add_node(name = nil, **attributes) node = @graph.add_node(name, **attributes) connect(node) return node end |
#attach(parent) ⇒ Object
Attach this node to the given graph:
40 41 42 |
# File 'lib/graphviz/node.rb', line 40 def attach(parent) @graph = parent end |
#connect(destination, attributes = {}) ⇒ Object
Create an edge between this node and the destination with the specified options.
55 56 57 58 59 60 61 |
# File 'lib/graphviz/node.rb', line 55 def connect(destination, attributes = {}) edge = Edge.new(@graph, self, destination, attributes) @connections << edge return edge end |
#connected?(node) ⇒ Boolean
Calculate if this node is connected to another. O(N) search required.
64 65 66 |
# File 'lib/graphviz/node.rb', line 64 def connected?(node) return @connections.find{|edge| edge.destination == node} end |
#dump_attributes(attributes) ⇒ Object
Dump the attributes to dot text format.
103 104 105 106 107 108 109 |
# File 'lib/graphviz/node.rb', line 103 def dump_attributes(attributes) if attributes.size > 0 "[" + attributes.collect{|name, value| "#{name}=#{dump_value(value)}"}.join(", ") + "]" else "" end end |
#dump_graph(buffer, indent, **options) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/graphviz/node.rb', line 82 def dump_graph(buffer, indent, **) node_attributes_text = dump_attributes(@attributes) node_name = dump_value(self.identifier) buffer.puts "#{indent}#{node_name}#{node_attributes_text};" end |
#dump_value(value) ⇒ Object
Dump the value to dot text format.
94 95 96 97 98 99 100 |
# File 'lib/graphviz/node.rb', line 94 def dump_value(value) if Symbol === value value.to_s else value.inspect end end |
#identifier ⇒ Object
78 79 80 |
# File 'lib/graphviz/node.rb', line 78 def identifier @name end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/graphviz/node.rb', line 89 def to_s dump_value(@name) end |