Class: Graphviz::Node

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(graph, name, attributes = {}) ⇒ Node

Initialize the node in the graph with the unique name.

Parameters:

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

    The associated graphviz attributes for this node.



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

#attributesHash

Returns Any attributes specified for this node.

Returns:

  • (Hash)

    Any attributes specified for this node.



45
46
47
# File 'lib/graphviz/graph.rb', line 45

def attributes
  @attributes
end

#edgesArray<Edge> (readonly)

Returns Any edges connecting to other nodes.

Returns:

  • (Array<Edge>)

    Any edges connecting to other nodes.



42
43
44
# File 'lib/graphviz/graph.rb', line 42

def edges
  @edges
end

#nameString (readonly)

Returns The unique name of the node.

Returns:

  • (String)

    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.

Parameters:

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

    The associated graphviz attributes for the new node.



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.

Parameters:

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

    The associated graphviz attributes for the edge.



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.

Returns:

  • (Boolean)


58
59
60
# File 'lib/graphviz/graph.rb', line 58

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