graphviz 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ddb46b8053b2f7327eabf54932a0940bf2495ac7
4
- data.tar.gz: 0f8da017c2d17dba6b9786a09ddd6018bb470dff
3
+ metadata.gz: 3c4f0f3b611287cceb14c4dac3e77e2497ffae13
4
+ data.tar.gz: fe7cf2f64b8febce78609be21cce9ef81f7cccc7
5
5
  SHA512:
6
- metadata.gz: 948aac08b37c57ab910f58ca6f30a672e29f5947cf8aa1a42389a49b8bbbe1ebdb51e60ef0eac5abbfb639a358bbcfb7f407795a5b32ef7799cd5b85956cc6d0
7
- data.tar.gz: 77048e08ac224b0e034e5397c3b048532caf630f05fd605e5010e8a54db807b1044f2c929ed1ed7c13ce4e3bcd327e7ecfdede3692c0d57b5e4f90ea1d0bb32b
6
+ metadata.gz: 65d2682330f3b2d3aaa63590950eeb0c52db5a7071d3e0d89a110ab19de7df2a3f4759cc3f007529db310e8c1d8aa8f742133dca3270b17031332c4ea5193252
7
+ data.tar.gz: 36c924356952539a6856848857607d4b1d67d512132595f09f02373becc85aae462f214490ace2912699e560e8a231531a6f6ae2957af606b1c2fe330777c9c4
@@ -25,21 +25,25 @@ module Graphviz
25
25
  class Node
26
26
  # Initialize the node in the graph with the unique name.
27
27
  # @param attributes [Hash] The associated graphviz attributes for this node.
28
- def initialize(graph, name, attributes = {})
29
- @graph = graph
30
- @graph.nodes[name] = self
31
-
28
+ def initialize(name, graph = nil, **attributes)
32
29
  @name = name
33
30
  @attributes = attributes
34
31
 
35
- @edges = []
32
+ @connections = []
33
+
34
+ graph << self if graph
35
+ end
36
+
37
+ # Attach this node to the given graph:
38
+ def attach(parent)
39
+ @graph = parent
36
40
  end
37
41
 
38
42
  # @return [String] The unique name of the node.
39
43
  attr :name
40
44
 
41
45
  # @return [Array<Edge>] Any edges connecting to other nodes.
42
- attr :edges
46
+ attr :connections
43
47
 
44
48
  # @return [Hash] Any attributes specified for this node.
45
49
  attr_accessor :attributes
@@ -49,25 +53,54 @@ module Graphviz
49
53
  def connect(destination, attributes = {})
50
54
  edge = Edge.new(@graph, self, destination, attributes)
51
55
 
52
- @edges << edge
56
+ @connections << edge
53
57
 
54
58
  return edge
55
59
  end
56
60
 
57
61
  # Calculate if this node is connected to another. +O(N)+ search required.
58
62
  def connected?(node)
59
- return @edges.find{|edge| edge.destination == node}
63
+ return @connections.find{|edge| edge.destination == node}
60
64
  end
61
65
 
62
66
  # Add a node and #connect to it.
63
67
  # @param attributes [Hash] The associated graphviz attributes for the new node.
64
- def add_node(name, attributes = {})
65
- node = Node.new(@graph, name, attributes)
68
+ def add_node(name = nil, **attributes)
69
+ node = @graph.add_node(name, **attributes)
66
70
 
67
71
  connect(node)
68
72
 
69
73
  return node
70
74
  end
75
+
76
+ def identifier
77
+ @name
78
+ end
79
+
80
+ def dump_graph(buffer, indent, options)
81
+ node_attributes_text = dump_attributes(@attributes)
82
+ node_name = dump_value(self.identifier)
83
+
84
+ buffer.puts "#{indent}#{node_name}#{node_attributes_text};"
85
+ end
86
+
87
+ # Dump the value to dot text format.
88
+ def dump_value(value)
89
+ if Symbol === value
90
+ value.to_s
91
+ else
92
+ value.inspect
93
+ end
94
+ end
95
+
96
+ # Dump the attributes to dot text format.
97
+ def dump_attributes(attributes)
98
+ if attributes.size > 0
99
+ "[" + attributes.collect{|(name, value)| "#{name}=#{dump_value(value)}"}.join(", ") + "]"
100
+ else
101
+ ""
102
+ end
103
+ end
71
104
  end
72
105
 
73
106
  # Represents a visual edge between two nodes.
@@ -100,29 +133,21 @@ module Graphviz
100
133
  end
101
134
 
102
135
  # Contains a set of nodes, edges and subgraphs.
103
- class Graph
136
+ class Graph < Node
104
137
  # Initialize the graph with the specified unique name.
105
- def initialize(name = 'G', attributes = {})
106
- @name = name
138
+ def initialize(name = 'G', parent = nil, **attributes)
139
+ super
107
140
 
108
- @nodes = {}
109
141
  @edges = []
110
- @graphs = {}
111
-
112
- @parent = nil
113
-
114
- @attributes = attributes
142
+ @nodes = {}
115
143
  end
116
144
 
117
- # @return [Graph] The parent graph, if any.
118
- attr :parent
145
+ # All edges in the graph
146
+ attr :edges
119
147
 
120
148
  # @return [Array<Node>] All nodes in the graph.
121
149
  attr :nodes
122
150
 
123
- # @return [Array<Edge>] All edges in the graph.
124
- attr :edges
125
-
126
151
  # @return [Array<Graph>] Any subgraphs.
127
152
  attr :graphs
128
153
 
@@ -130,24 +155,28 @@ module Graphviz
130
155
  attr_accessor :attributes
131
156
 
132
157
  # @return [Node] Add a node to this graph.
133
- def add_node(name, attributes = {})
134
- Node.new(self, name, attributes)
158
+ def add_node(name = nil, **attributes)
159
+ name ||= "#{@name}N#{@nodes.count}"
160
+
161
+ Node.new(name, self, attributes)
135
162
  end
136
163
 
137
164
  # Add a subgraph with a given name and attributes.
138
165
  # @return [Graph] the new graph.
139
- def add_subgraph(name, attributes = {})
140
- graph = Graph.new(name, attributes)
166
+ def add_subgraph(name = nil, **attributes)
167
+ name ||= "#{@name}S#{@nodes.count}"
168
+
169
+ subgraph = Graph.new(name, self, attributes)
141
170
 
142
- graph.attach(self)
143
- @graphs[name] = graph
171
+ self << subgraph
144
172
 
145
- return graph
173
+ return subgraph
146
174
  end
147
175
 
148
- # Make this graph a subgraph of the parent.
149
- def attach(parent)
150
- @parent = parent
176
+ def << node
177
+ @nodes[node.name] = node
178
+
179
+ node.attach(self)
151
180
  end
152
181
 
153
182
  # @return [String] Output the graph using the dot format.
@@ -159,57 +188,49 @@ module Graphviz
159
188
  return buffer.string
160
189
  end
161
190
 
162
- protected
191
+ def graph_format(options)
192
+ if @graph
193
+ 'subgraph'
194
+ else
195
+ options[:format] || 'digraph'
196
+ end
197
+ end
198
+
199
+ def identifier
200
+ if @attributes[:cluster]
201
+ "cluster_#{@name}"
202
+ else
203
+ super
204
+ end
205
+ end
206
+
207
+ def dump_edges(buffer, indent, options)
208
+ @edges.each do |edge|
209
+ from_name = dump_value(edge.source.identifier)
210
+ to_name = dump_value(edge.destination.identifier)
211
+ edge_attributes_text = dump_attributes(edge.attributes)
212
+
213
+ buffer.puts "#{indent}#{from_name} #{edge} #{to_name}#{edge_attributes_text};"
214
+ end
215
+ end
163
216
 
164
217
  # Dump the entire graph and all subgraphs to dot text format.
165
218
  def dump_graph(buffer, indent, options)
166
- format = options[:format] || 'digraph'
219
+ format = graph_format(options)
167
220
 
168
- buffer.puts "#{indent}#{format} #{dump_value(@name)} {"
221
+ buffer.puts "#{indent}#{format} #{dump_value(self.identifier)} {"
169
222
 
170
223
  @attributes.each do |(name, value)|
171
224
  buffer.puts "#{indent}\t#{name}=#{dump_value(value)};"
172
225
  end
173
226
 
174
227
  @nodes.each do |(name, node)|
175
- node_attributes_text = dump_attributes(node.attributes)
176
- node_name = dump_value(node.name)
177
- buffer.puts "#{indent}\t#{node_name}#{node_attributes_text};"
178
-
179
- if node.edges.size > 0
180
- node.edges.each do |edge|
181
- from_name = dump_value(edge.source.name)
182
- to_name = dump_value(edge.destination.name)
183
- edge_attributes_text = dump_attributes(edge.attributes)
184
-
185
- buffer.puts "#{indent}\t#{from_name} #{edge} #{to_name}#{edge_attributes_text};"
186
- end
187
- end
228
+ node.dump_graph(buffer, indent + "\t", options)
188
229
  end
189
230
 
190
- @graphs.each do |(name, graph)|
191
- graph.dump_graph(buffer, indent + "\t", options.merge(:format => 'subgraph'))
192
- end
231
+ dump_edges(buffer, indent + "\t", options)
193
232
 
194
233
  buffer.puts "#{indent}}"
195
234
  end
196
-
197
- # Dump the value to dot text format.
198
- def dump_value(value)
199
- if Symbol === value
200
- value.to_s
201
- else
202
- value.inspect
203
- end
204
- end
205
-
206
- # Dump the attributes to dot text format.
207
- def dump_attributes(attributes)
208
- if attributes.size > 0
209
- "[" + attributes.collect{|(name, value)| "#{name}=#{dump_value(value)}"}.join(", ") + "]"
210
- else
211
- ""
212
- end
213
- end
214
235
  end
215
236
  end
@@ -20,5 +20,5 @@
20
20
 
21
21
  module Graphviz
22
22
  # The version.
23
- VERSION = "0.2.0"
23
+ VERSION = "0.3.0"
24
24
  end
@@ -24,8 +24,8 @@ module Graphviz::GraphSpec
24
24
  SAMPLE_GRAPH_DOT = <<-EOF
25
25
  digraph "G" {
26
26
  "Foo"[shape="box3d", color="red"];
27
- "Foo" -> "Bar";
28
27
  "Bar";
28
+ "Foo" -> "Bar";
29
29
  }
30
30
  EOF
31
31
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphviz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-09 00:00:00.000000000 Z
11
+ date: 2016-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 2.4.6
108
+ rubygems_version: 2.2.2
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: A lightweight interface for generating graphs with Graphviz.