Class: Graphviz::Graph

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

Overview

Contains a set of nodes, edges and subgraphs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'G', attributes = {}) ⇒ Graph

Initialize the graph with the specified unique name.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/graphviz/graph.rb', line 105

def initialize(name = 'G', attributes = {})
	@name = name
	
	@nodes = {}
	@edges = []
	@graphs = {}
	
	@parent = nil
	
	@attributes = attributes
end

Instance Attribute Details

#attributesHash

Returns Any associated graphviz attributes.

Returns:

  • (Hash)

    Any associated graphviz attributes.



130
131
132
# File 'lib/graphviz/graph.rb', line 130

def attributes
  @attributes
end

#edgesArray<Edge> (readonly)

Returns All edges in the graph.

Returns:

  • (Array<Edge>)

    All edges in the graph.



124
125
126
# File 'lib/graphviz/graph.rb', line 124

def edges
  @edges
end

#graphsArray<Graph> (readonly)

Returns Any subgraphs.

Returns:

  • (Array<Graph>)

    Any subgraphs.



127
128
129
# File 'lib/graphviz/graph.rb', line 127

def graphs
  @graphs
end

#nodesArray<Node> (readonly)

Returns All nodes in the graph.

Returns:

  • (Array<Node>)

    All nodes in the graph.



121
122
123
# File 'lib/graphviz/graph.rb', line 121

def nodes
  @nodes
end

#parentGraph (readonly)

Returns The parent graph, if any.

Returns:

  • (Graph)

    The parent graph, if any.



118
119
120
# File 'lib/graphviz/graph.rb', line 118

def parent
  @parent
end

Instance Method Details

#add_node(name, attributes = {}) ⇒ Node

Returns Add a node to this graph.

Returns:

  • (Node)

    Add a node to this graph.



133
134
135
# File 'lib/graphviz/graph.rb', line 133

def add_node(name, attributes = {})
	Node.new(self, name, attributes)
end

#add_subgraph(name, attributes = {}) ⇒ Graph

Add a subgraph with a given name and attributes.

Returns:

  • (Graph)

    the new graph.



139
140
141
142
143
144
145
146
# File 'lib/graphviz/graph.rb', line 139

def add_subgraph(name, attributes = {})
	graph = Graph.new(name, attributes)
	
	graph.attach(self)
	@graphs[name] = graph
	
	return graph
end

#attach(parent) ⇒ Object

Make this graph a subgraph of the parent.



149
150
151
# File 'lib/graphviz/graph.rb', line 149

def attach(parent)
	@parent = parent
end

#to_dot(options = {}) ⇒ String

Returns Output the graph using the dot format.

Returns:

  • (String)

    Output the graph using the dot format.



154
155
156
157
158
159
160
# File 'lib/graphviz/graph.rb', line 154

def to_dot(options = {})
	buffer = StringIO.new
	
	dump_graph(buffer, "", options)
	
	return buffer.string
end