Class: Graphviz::Node
- Inherits:
-
Object
- Object
- Graphviz::Node
- Defined in:
- lib/graphviz/graph.rb
Overview
Represents a visual node in the graph, which can be connected to other nodes.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
Any attributes specified for this node.
-
#edges ⇒ 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, attributes = {}) ⇒ Object
Add a node and #connect to it.
-
#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.
-
#initialize(graph, name, attributes = {}) ⇒ Node
constructor
Initialize the node in the graph with the unique name.
Constructor Details
#initialize(graph, name, attributes = {}) ⇒ Node
Initialize the node in the graph with the unique name.
28 29 30 31 32 33 34 35 36 |
# File 'lib/graphviz/graph.rb', line 28 def initialize(graph, name, attributes = {}) @graph = graph @graph.nodes[name] = self @name = name @attributes = attributes @edges = [] end |
Instance Attribute Details
#attributes ⇒ Hash
Returns Any attributes specified for this node.
45 46 47 |
# File 'lib/graphviz/graph.rb', line 45 def attributes @attributes end |
#edges ⇒ Array<Edge> (readonly)
Returns Any edges connecting to other nodes.
42 43 44 |
# File 'lib/graphviz/graph.rb', line 42 def edges @edges end |
#name ⇒ String (readonly)
Returns The unique name of the node.
39 40 41 |
# File 'lib/graphviz/graph.rb', line 39 def name @name end |
Instance Method Details
#add_node(name, attributes = {}) ⇒ Object
Add a node and #connect to it.
64 65 66 67 68 69 70 |
# File 'lib/graphviz/graph.rb', line 64 def add_node(name, attributes = {}) node = Node.new(@graph, name, attributes) connect(node) return node end |
#connect(destination, attributes = {}) ⇒ Object
Create an edge between this node and the destination with the specified options.
49 50 51 52 53 54 55 |
# File 'lib/graphviz/graph.rb', line 49 def connect(destination, attributes = {}) edge = Edge.new(@graph, self, destination, attributes) @edges << edge return edge end |
#connected?(node) ⇒ Boolean
Calculate if this node is connected to another. O(N) search required.
58 59 60 |
# File 'lib/graphviz/graph.rb', line 58 def connected?(node) return @edges.find{|edge| edge.destination == node} end |