graphviz 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53f53e53933990cd7d1db7893e889953abfcb29f
4
- data.tar.gz: e5937034681308f0cc005cff8927427b5a14f8e4
3
+ metadata.gz: 5ecf43f4dc20f7751fdcab695ed60c56c05d2c99
4
+ data.tar.gz: caa5d9ee7b24714b8804085456ab55f020f069e1
5
5
  SHA512:
6
- metadata.gz: e34c656a49ce0dc4ad30973e1093fef0bfc8fbef62622645e44d3f993bfc11c60a0ab8ad52a3f0badcd1102717132b8350b6c4632444d82a78ee8b7fe89057f4
7
- data.tar.gz: 5b9196eed4421dc028400df9aa52a1610e8ddd8f20d654169823870ca90ff137c80f7b0b29f719db88bf13cfaca3ea56d624dbdd50c1de35488f12c15f5c548f
6
+ metadata.gz: 704db8989d9806ae9846f224272e10c4c80e10179e6da9259798e1cf9c0c7b34330733432de305f1f3e88de56a34812011861ac736d076eed31e35c066fdeba0
7
+ data.tar.gz: 7c4a946acc7edc8dc0d93e14803e960299ed8d17c57d3684a0ce4d7242fe01b7b079d485a9f57b9a5935d808ea1842f02012d2e3eea1e9427dd66fd4cb80275e
data/README.md CHANGED
@@ -18,7 +18,23 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Some example code:
22
+
23
+ require 'graphviz';
24
+
25
+ g = Graphviz::Graph.new
26
+
27
+ foo = g.add_node("Foo")
28
+ foo.add_node("Bar")
29
+
30
+ foo.attributes[:shape] = 'box3d'
31
+ foo.attributes[:color] = 'red'
32
+
33
+ # Dup the dot data:
34
+ puts g.to_dot
35
+
36
+ # Process the graph to output:
37
+ Graphviz::output(g, :path => "test.pdf")
22
38
 
23
39
  ## Contributing
24
40
 
@@ -22,12 +22,12 @@ require 'stringio'
22
22
 
23
23
  module Graphviz
24
24
  class Node
25
- def initialize(graph, name, options)
25
+ def initialize(graph, name, attributes = {})
26
26
  @graph = graph
27
27
  @graph.nodes[name] = self
28
28
 
29
29
  @name = name
30
- @options = options
30
+ @attributes = attributes
31
31
 
32
32
  @edges = []
33
33
  end
@@ -36,6 +36,7 @@ module Graphviz
36
36
  attr :options
37
37
 
38
38
  attr :edges
39
+ attr_accessor :attributes
39
40
 
40
41
  def connect(destination, options = {})
41
42
  edge = Edge.new(@graph, self, destination, options)
@@ -45,6 +46,10 @@ module Graphviz
45
46
  return edge
46
47
  end
47
48
 
49
+ def connected?(node)
50
+ return @edges.find{|edge| edge.destination == node}
51
+ end
52
+
48
53
  def add_node(name, options = {})
49
54
  node = Node.new(@graph, name, options)
50
55
 
@@ -55,61 +60,124 @@ module Graphviz
55
60
  end
56
61
 
57
62
  class Edge
58
- def initialize(graph, source, destination, options = {})
63
+ def initialize(graph, source, destination, attributes = {})
59
64
  @graph = graph
60
65
  @graph.edges << self
61
66
 
62
67
  @source = source
63
68
  @destination = destination
64
69
 
65
- @options = options
70
+ @attributes = attributes
66
71
  end
67
72
 
68
73
  attr :source
69
74
  attr :destination
70
75
 
76
+ attr_accessor :attributes
77
+
71
78
  attr :options
79
+ attr_accessor :line
72
80
 
73
81
  def to_s
74
- "->"
82
+ '->'
75
83
  end
76
84
  end
77
85
 
86
+ class EdgeNode < Node
87
+ end
88
+
78
89
  class Graph
79
- def initialize(name = 'G', options = {})
90
+ def initialize(name = 'G', attributes = {})
80
91
  @name = name
81
92
 
82
93
  @nodes = {}
83
94
  @edges = []
95
+ @graphs = {}
96
+
97
+ @parent = nil
98
+
99
+ @attributes = attributes
84
100
  end
85
101
 
86
102
  attr :nodes
87
103
  attr :edges
104
+ attr :graphs
88
105
 
89
- def add_node(name, options = {})
90
- Node.new(self, name, options)
106
+ attr_accessor :attributes
107
+
108
+ def add_node(name, attributes = {})
109
+ Node.new(self, name, attributes)
110
+ end
111
+
112
+ def add_subgraph(name, attributes = {})
113
+ graph = Graph.new(name, attributes)
114
+
115
+ graph.attach(self)
116
+ @graphs[name] = graph
117
+
118
+ return graph
119
+ end
120
+
121
+ def attach(parent)
122
+ @parent = parent
91
123
  end
92
124
 
93
125
  def to_dot(options = {})
94
126
  buffer = StringIO.new
95
127
 
128
+ dump_graph(buffer, "", options)
129
+
130
+ return buffer.string
131
+ end
132
+
133
+ protected
134
+
135
+ def dump_graph(buffer, indent, options)
96
136
  format = options[:format] || 'digraph'
97
137
 
98
- buffer.puts "#{format} #{@name} {"
138
+ buffer.puts "#{indent}#{format} #{dump_value(@name)} {"
139
+
140
+ @attributes.each do |(name, value)|
141
+ buffer.puts "#{indent}\t#{name}=#{dump_value(value)};"
142
+ end
99
143
 
100
144
  @nodes.each do |(name, node)|
145
+ node_attributes_text = dump_attributes(node.attributes)
146
+ node_name = dump_value(node.name)
147
+ buffer.puts "#{indent}\t#{node_name}#{node_attributes_text};"
148
+
101
149
  if node.edges.size > 0
102
150
  node.edges.each do |edge|
103
- buffer.puts "\t#{edge.source.name.dump} #{edge} #{edge.destination.name.dump};"
151
+ from_name = dump_value(edge.source.name)
152
+ to_name = dump_value(edge.destination.name)
153
+ edge_attributes_text = dump_attributes(edge.attributes)
154
+
155
+ buffer.puts "#{indent}\t#{from_name} #{edge} #{to_name}#{edge_attributes_text};"
104
156
  end
105
- else
106
- buffer.puts "\t#{node.name.dump};"
107
157
  end
108
158
  end
109
159
 
110
- buffer.puts "}"
160
+ @graphs.each do |(name, graph)|
161
+ graph.dump_graph(buffer, indent + "\t", options.merge(:format => 'subgraph'))
162
+ end
111
163
 
112
- return buffer.string
164
+ buffer.puts "#{indent}}"
165
+ end
166
+
167
+ def dump_value(value)
168
+ if Symbol === value
169
+ value.to_s
170
+ else
171
+ value.inspect
172
+ end
173
+ end
174
+
175
+ def dump_attributes(attributes)
176
+ if attributes.size > 0
177
+ "[" + attributes.collect{|(name, value)| "#{name}=#{dump_value(value)}"}.join(", ") + "]"
178
+ else
179
+ ""
180
+ end
113
181
  end
114
182
  end
115
183
  end
@@ -1,3 +1,3 @@
1
1
  module Graphviz
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/sample.rb CHANGED
@@ -5,6 +5,12 @@ g = Graphviz::Graph.new
5
5
  foo = g.add_node("Foo")
6
6
  foo.add_node("Bar")
7
7
 
8
+ foo.attributes[:shape] = 'box3d'
9
+ foo.attributes[:color] = 'red'
10
+
11
+ # Dup the dot data:
8
12
  puts g.to_dot
9
13
 
14
+ # Process the graph to output:
10
15
  Graphviz::output(g, :path => "test.pdf")
16
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphviz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams