class Graphviz::Node

Represents a visual node in the graph, which can be connected to other nodes.

Attributes

attributes[RW]

@return [Hash] Any attributes specified for this node.

connections[R]

@return [Array<Edge>] Any edges connecting to other nodes.

name[R]

@return [String] The unique name of the node.

Public Class Methods

new(name, graph = nil, **attributes) click to toggle source

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_node(name = nil, **attributes) click to toggle source

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(parent) click to toggle source

Attach this node to the given graph:

# File lib/graphviz/node.rb, line 40
def attach(parent)
        @graph = parent
end
connect(destination, attributes = {}) click to toggle source

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
connected?(node) click to toggle source

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_attributes(attributes) click to toggle source

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
dump_graph(buffer, indent, **options) click to toggle source
# 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_value(value) click to toggle source

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
identifier() click to toggle source
# File lib/graphviz/node.rb, line 78
def identifier
        @name
end
to_s() click to toggle source
# File lib/graphviz/node.rb, line 89
def to_s
        dump_value(@name)
end