class Graphviz::Node
Represents a visual node in the graph, which can be connected to other nodes.
Attributes
@return [Hash] Any attributes specified for this node.
@return [Array<Edge>] Any edges connecting to other nodes.
@return [String] The unique name of the node.
Public Class Methods
Initialize the node in the graph with the unique name. @param attributes [Hash] The associated graphviz attributes for this node.
# 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
Public Instance Methods
Add a node and connect
to it. @param attributes [Hash] The associated graphviz attributes for the new node.
# 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 this node to the given graph:
# File lib/graphviz/node.rb, line 40 def attach(parent) @graph = parent end
Create an edge between this node and the destination with the specified options. @param attributes [Hash] The associated graphviz attributes for the edge.
# File lib/graphviz/node.rb, line 55 def connect(destination, attributes = {}) edge = Edge.new(@graph, self, destination, attributes) @connections << edge return edge end
Calculate if this node is connected to another. +O(N)+ search required.
# File lib/graphviz/node.rb, line 64 def connected?(node) return @connections.find{|edge| edge.destination == node} end
Dump the attributes to dot text format.
# 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
# File lib/graphviz/node.rb, line 82 def dump_graph(buffer, indent, **options) node_attributes_text = dump_attributes(@attributes) node_name = dump_value(self.identifier) buffer.puts "#{indent}#{node_name}#{node_attributes_text};" end
Dump the value to dot text format.
# File lib/graphviz/node.rb, line 94 def dump_value(value) if Symbol === value value.to_s else value.inspect end end
# File lib/graphviz/node.rb, line 78 def identifier @name end
# File lib/graphviz/node.rb, line 89 def to_s dump_value(@name) end