Class: Graphviz::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/graphviz/node.rb

Overview

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

Direct Known Subclasses

Graph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, graph = nil, **attributes) ⇒ Node

Initialize the node in the graph with the unique name.

Parameters:

  • attributes (Hash)

    The associated graphviz attributes for this node.



14
15
16
17
18
19
20
21
22
23
# File 'lib/graphviz/node.rb', line 14

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

#attributesHash

Returns Any attributes specified for this node.

Returns:

  • (Hash)

    Any attributes specified for this node.



37
38
39
# File 'lib/graphviz/node.rb', line 37

def attributes
  @attributes
end

#connectionsArray<Edge> (readonly)

Returns Any edges connecting to other nodes.

Returns:

  • (Array<Edge>)

    Any edges connecting to other nodes.



34
35
36
# File 'lib/graphviz/node.rb', line 34

def connections
  @connections
end

#nameString (readonly)

Returns The unique name of the node.

Returns:

  • (String)

    The unique name of the node.



31
32
33
# File 'lib/graphviz/node.rb', line 31

def name
  @name
end

Instance Method Details

#add_node(name = nil, **attributes) ⇒ Object

Add a node and #connect to it.

Parameters:

  • attributes (Hash)

    The associated graphviz attributes for the new node.



56
57
58
59
60
61
62
# File 'lib/graphviz/node.rb', line 56

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:



26
27
28
# File 'lib/graphviz/node.rb', line 26

def attach(parent)
  @graph = parent
end

#connect(destination, attributes = {}) ⇒ Object

Create an edge between this node and the destination with the specified options.

Parameters:

  • attributes (Hash) (defaults to: {})

    The associated graphviz attributes for the edge.



41
42
43
44
45
46
47
# File 'lib/graphviz/node.rb', line 41

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.

Returns:

  • (Boolean)


50
51
52
# File 'lib/graphviz/node.rb', line 50

def connected?(node)
  return @connections.find{|edge| edge.destination == node}
end

#dump_attributes(attributes) ⇒ Object

Dump the attributes to dot text format.



89
90
91
92
93
94
95
# File 'lib/graphviz/node.rb', line 89

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



68
69
70
71
72
73
# File 'lib/graphviz/node.rb', line 68

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) ⇒ Object

Dump the value to dot text format.



80
81
82
83
84
85
86
# File 'lib/graphviz/node.rb', line 80

def dump_value(value)
  if Symbol === value
    value.to_s
  else
    value.inspect
  end
end

#identifierObject



64
65
66
# File 'lib/graphviz/node.rb', line 64

def identifier
  @name
end

#to_sObject



75
76
77
# File 'lib/graphviz/node.rb', line 75

def to_s
  dump_value(@name)
end