graphviz 0.0.2 → 0.1.0

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: 5ecf43f4dc20f7751fdcab695ed60c56c05d2c99
4
- data.tar.gz: caa5d9ee7b24714b8804085456ab55f020f069e1
3
+ metadata.gz: 52082025c42ba958329ac76f235b64c81e2408f8
4
+ data.tar.gz: 0b6592cfcda1720987e9565eff5b8ae4382624a7
5
5
  SHA512:
6
- metadata.gz: 704db8989d9806ae9846f224272e10c4c80e10179e6da9259798e1cf9c0c7b34330733432de305f1f3e88de56a34812011861ac736d076eed31e35c066fdeba0
7
- data.tar.gz: 7c4a946acc7edc8dc0d93e14803e960299ed8d17c57d3684a0ce4d7242fe01b7b079d485a9f57b9a5935d808ea1842f02012d2e3eea1e9427dd66fd4cb80275e
6
+ metadata.gz: cf66ca0b9b9c46f539f8705afcb210469703f9ed83be6ad95ddca9b52b0e9283357bfa7fe9ae58cc244d6c71cf71fa0374e24c28e16a91006ed4685dbc4513da
7
+ data.tar.gz: 4d99844ff54a143ddde00d64bcd36738c605c5edfee9457f2970f16c3adc7644c17007c97faaa7283bbf569fbb446ca0dc478a1950f4e92288b1f60a432d1192
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ before_install:
3
+ - sudo apt-get install graphviz
4
+ rvm:
5
+ - "1.9"
6
+ - "2.0"
7
+ - "2.1"
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: "1.9"
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  Graphviz is a graph visualisation system. This gem is a lightweight interface for generating graphs with Graphviz.
4
4
 
5
+ [![Build Status](https://travis-ci.org/ioquatix/graphviz.svg)](https://travis-ci.org/ioquatix/graphviz)
6
+ [![Code Climate](https://codeclimate.com/github/ioquatix/graphviz.png)](https://codeclimate.com/github/ioquatix/graphviz)
7
+
5
8
  ## Installation
6
9
 
7
10
  Add this line to your application's Gemfile:
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/graphviz.gemspec CHANGED
@@ -20,8 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
  spec.require_paths = ["lib"]
22
22
 
23
+ spec.has_rdoc = 'yard'
24
+
25
+ spec.add_development_dependency "yard", "~> 0.8.7"
23
26
  spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rspec", "~> 3.0.0"
24
28
  spec.add_development_dependency "rake"
25
-
26
- spec.add_dependency "rexec"
27
29
  end
data/lib/graphviz.rb CHANGED
@@ -1,15 +1,15 @@
1
- # Copyright (c) 2013 Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
5
5
  # in the Software without restriction, including without limitation the rights
6
6
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
7
  # copies of the Software, and to permit persons to whom the Software is
8
8
  # furnished to do so, subject to the following conditions:
9
- #
9
+ #
10
10
  # The above copyright notice and this permission notice shall be included in
11
11
  # all copies or substantial portions of the Software.
12
- #
12
+ #
13
13
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
14
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
15
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -19,14 +19,19 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require "graphviz/version"
22
-
23
22
  require "graphviz/graph"
24
- require 'rexec'
25
23
 
26
24
  module Graphviz
25
+ # Signals that the process exited with a non-zero status.
27
26
  class OutputError < StandardError
28
27
  end
29
28
 
29
+ # Outputs the graph using the +dot+ executable.
30
+ # @return [String] any data generated by the command unless an output path is specified.
31
+ #
32
+ # @option options [String] :output_format ('pdf') The output format (e.g. pdf).
33
+ # @option options [String] :path The output path, if not specified data is returned.
34
+ # @option options [String] :dot ('dot') The +dot+ executable to use.
30
35
  def self.output(graph, options = {})
31
36
  text = graph.to_dot
32
37
 
@@ -43,20 +48,24 @@ module Graphviz
43
48
  output_file = IO.pipe
44
49
  end
45
50
 
46
- RExec::Task.open(["dot", "-T#{output_format}"], :out => output_file) do |task|
47
- task.input.puts(graph.to_dot)
48
- task.input.close
49
-
50
- status = task.wait
51
-
52
- if status != 0
53
- raise OutputError.new(task.error.read)
54
- end
51
+ output, input = IO.pipe
52
+ pid = Process.spawn(options[:dot] || "dot", "-T#{output_format}", :out => output_file, :in => output)
53
+
54
+ output.close
55
+
56
+ # Send graph data to process:
57
+ input.write(graph.to_dot)
58
+ input.close
59
+
60
+ _, status = Process.wait2(pid)
61
+
62
+ if status != 0
63
+ raise OutputError.new("dot exited with status #{status}")
64
+ end
55
65
 
56
- # Did we use a local pipe for output?
57
- if Array === output_file
58
- return task.output.read
59
- end
66
+ # Did we use a local pipe for output?
67
+ if Array === output_file
68
+ return task.output.read
60
69
  end
61
70
  end
62
71
  end
@@ -1,15 +1,15 @@
1
- # Copyright (c) 2013 Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
5
5
  # in the Software without restriction, including without limitation the rights
6
6
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
7
  # copies of the Software, and to permit persons to whom the Software is
8
8
  # furnished to do so, subject to the following conditions:
9
- #
9
+ #
10
10
  # The above copyright notice and this permission notice shall be included in
11
11
  # all copies or substantial portions of the Software.
12
- #
12
+ #
13
13
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
14
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
15
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -21,7 +21,10 @@
21
21
  require 'stringio'
22
22
 
23
23
  module Graphviz
24
+ # Represents a visual node in the graph, which can be connected to other nodes.
24
25
  class Node
26
+ # Initialize the node in the graph with the unique name.
27
+ # @param attributes [Hash] The associated graphviz attributes for this node.
25
28
  def initialize(graph, name, attributes = {})
26
29
  @graph = graph
27
30
  @graph.nodes[name] = self
@@ -32,26 +35,34 @@ module Graphviz
32
35
  @edges = []
33
36
  end
34
37
 
38
+ # @return [String] The unique name of the node.
35
39
  attr :name
36
- attr :options
37
40
 
41
+ # @return [Array<Edge>] Any edges connecting to other nodes.
38
42
  attr :edges
43
+
44
+ # @return [Hash] Any attributes specified for this node.
39
45
  attr_accessor :attributes
40
46
 
41
- def connect(destination, options = {})
42
- edge = Edge.new(@graph, self, destination, options)
47
+ # Create an edge between this node and the destination with the specified options.
48
+ # @param attributes [Hash] The associated graphviz attributes for the edge.
49
+ def connect(destination, attributes = {})
50
+ edge = Edge.new(@graph, self, destination, attributes)
43
51
 
44
52
  @edges << edge
45
53
 
46
54
  return edge
47
55
  end
48
56
 
57
+ # Calculate if this node is connected to another. +O(N)+ search required.
49
58
  def connected?(node)
50
59
  return @edges.find{|edge| edge.destination == node}
51
60
  end
52
61
 
53
- def add_node(name, options = {})
54
- node = Node.new(@graph, name, options)
62
+ # Add a node and #connect to it.
63
+ # @param attributes [Hash] The associated graphviz attributes for the new node.
64
+ def add_node(name, attributes = {})
65
+ node = Node.new(@graph, name, attributes)
55
66
 
56
67
  connect(node)
57
68
 
@@ -59,7 +70,10 @@ module Graphviz
59
70
  end
60
71
  end
61
72
 
73
+ # Represents a visual edge between two nodes.
62
74
  class Edge
75
+ # Initialize the edge in the given graph, with a source and destination node.
76
+ # @param attributes [Hash] The associated graphviz attributes for this edge.
63
77
  def initialize(graph, source, destination, attributes = {})
64
78
  @graph = graph
65
79
  @graph.edges << self
@@ -70,23 +84,24 @@ module Graphviz
70
84
  @attributes = attributes
71
85
  end
72
86
 
87
+ # @return [Node] The source node.
73
88
  attr :source
89
+
90
+ # @return [Node] The destination node.
74
91
  attr :destination
75
92
 
93
+ # @return [Hash] Any attributes specified for this edge.
76
94
  attr_accessor :attributes
77
95
 
78
- attr :options
79
- attr_accessor :line
80
-
96
+ # @return [String] A convenient ASCII arrow.
81
97
  def to_s
82
98
  '->'
83
99
  end
84
100
  end
85
101
 
86
- class EdgeNode < Node
87
- end
88
-
102
+ # Contains a set of nodes, edges and subgraphs.
89
103
  class Graph
104
+ # Initialize the graph with the specified unique name.
90
105
  def initialize(name = 'G', attributes = {})
91
106
  @name = name
92
107
 
@@ -99,16 +114,28 @@ module Graphviz
99
114
  @attributes = attributes
100
115
  end
101
116
 
117
+ # @return [Graph] The parent graph, if any.
118
+ attr :parent
119
+
120
+ # @return [Array<Node>] All nodes in the graph.
102
121
  attr :nodes
122
+
123
+ # @return [Array<Edge>] All edges in the graph.
103
124
  attr :edges
125
+
126
+ # @return [Array<Graph>] Any subgraphs.
104
127
  attr :graphs
105
128
 
129
+ # @return [Hash] Any associated graphviz attributes.
106
130
  attr_accessor :attributes
107
131
 
132
+ # @return [Node] Add a node to this graph.
108
133
  def add_node(name, attributes = {})
109
134
  Node.new(self, name, attributes)
110
135
  end
111
136
 
137
+ # Add a subgraph with a given name and attributes.
138
+ # @return [Graph] the new graph.
112
139
  def add_subgraph(name, attributes = {})
113
140
  graph = Graph.new(name, attributes)
114
141
 
@@ -118,10 +145,12 @@ module Graphviz
118
145
  return graph
119
146
  end
120
147
 
148
+ # Make this graph a subgraph of the parent.
121
149
  def attach(parent)
122
150
  @parent = parent
123
151
  end
124
152
 
153
+ # @return [String] Output the graph using the dot format.
125
154
  def to_dot(options = {})
126
155
  buffer = StringIO.new
127
156
 
@@ -132,6 +161,7 @@ module Graphviz
132
161
 
133
162
  protected
134
163
 
164
+ # Dump the entire graph and all subgraphs to dot text format.
135
165
  def dump_graph(buffer, indent, options)
136
166
  format = options[:format] || 'digraph'
137
167
 
@@ -164,6 +194,7 @@ module Graphviz
164
194
  buffer.puts "#{indent}}"
165
195
  end
166
196
 
197
+ # Dump the value to dot text format.
167
198
  def dump_value(value)
168
199
  if Symbol === value
169
200
  value.to_s
@@ -172,6 +203,7 @@ module Graphviz
172
203
  end
173
204
  end
174
205
 
206
+ # Dump the attributes to dot text format.
175
207
  def dump_attributes(attributes)
176
208
  if attributes.size > 0
177
209
  "[" + attributes.collect{|(name, value)| "#{name}=#{dump_value(value)}"}.join(", ") + "]"
@@ -1,3 +1,24 @@
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
1
21
  module Graphviz
2
- VERSION = "0.0.2"
22
+ # The version.
23
+ VERSION = "0.1.0"
3
24
  end
@@ -0,0 +1,48 @@
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'graphviz'
22
+
23
+ module Graphviz::GraphSpec
24
+ SAMPLE_GRAPH_DOT = <<-EOF
25
+ digraph "G" {
26
+ "Foo"[shape="box3d", color="red"];
27
+ "Foo" -> "Bar";
28
+ "Bar";
29
+ }
30
+ EOF
31
+
32
+ describe Graphviz::Graph do
33
+ it "should construct a simple graph" do
34
+ foo = subject.add_node("Foo")
35
+ foo.add_node("Bar")
36
+
37
+ foo.attributes[:shape] = 'box3d'
38
+ foo.attributes[:color] = 'red'
39
+
40
+ expect(subject.to_dot).to be == SAMPLE_GRAPH_DOT
41
+
42
+ # Process the graph to output:
43
+ Graphviz::output(subject, :path => "test.pdf")
44
+
45
+ expect(File.exist? "test.pdf").to be true
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,55 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphviz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.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: 2013-10-04 00:00:00.000000000 Z
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.7
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.7
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - ~>
31
+ - - "~>"
18
32
  - !ruby/object:Gem::Version
19
33
  version: '1.3'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - ~>
38
+ - - "~>"
25
39
  - !ruby/object:Gem::Version
26
40
  version: '1.3'
27
41
  - !ruby/object:Gem::Dependency
28
- name: rake
42
+ name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - '>='
45
+ - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '0'
47
+ version: 3.0.0
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - '>='
52
+ - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '0'
54
+ version: 3.0.0
41
55
  - !ruby/object:Gem::Dependency
42
- name: rexec
56
+ name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - '>='
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
- type: :runtime
62
+ type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - '>='
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  description: "\t\tGraphviz is a graph visualisation system. This gem is a lightweight
@@ -60,15 +74,16 @@ executables: []
60
74
  extensions: []
61
75
  extra_rdoc_files: []
62
76
  files:
63
- - .gitignore
77
+ - ".gitignore"
78
+ - ".travis.yml"
64
79
  - Gemfile
65
80
  - README.md
81
+ - Rakefile
66
82
  - graphviz.gemspec
67
83
  - lib/graphviz.rb
68
84
  - lib/graphviz/graph.rb
69
85
  - lib/graphviz/version.rb
70
- - rakefile.rb
71
- - sample.rb
86
+ - spec/graphviz/graph_spec.rb
72
87
  homepage: ''
73
88
  licenses:
74
89
  - MIT
@@ -79,18 +94,20 @@ require_paths:
79
94
  - lib
80
95
  required_ruby_version: !ruby/object:Gem::Requirement
81
96
  requirements:
82
- - - '>='
97
+ - - ">="
83
98
  - !ruby/object:Gem::Version
84
99
  version: '0'
85
100
  required_rubygems_version: !ruby/object:Gem::Requirement
86
101
  requirements:
87
- - - '>='
102
+ - - ">="
88
103
  - !ruby/object:Gem::Version
89
104
  version: '0'
90
105
  requirements: []
91
106
  rubyforge_project:
92
- rubygems_version: 2.0.6
107
+ rubygems_version: 2.2.2
93
108
  signing_key:
94
109
  specification_version: 4
95
110
  summary: A lightweight interface for generating graphs with Graphviz.
96
- test_files: []
111
+ test_files:
112
+ - spec/graphviz/graph_spec.rb
113
+ has_rdoc: yard
data/rakefile.rb DELETED
@@ -1 +0,0 @@
1
- require "bundler/gem_tasks"
data/sample.rb DELETED
@@ -1,16 +0,0 @@
1
- require 'graphviz';
2
-
3
- g = Graphviz::Graph.new
4
-
5
- foo = g.add_node("Foo")
6
- foo.add_node("Bar")
7
-
8
- foo.attributes[:shape] = 'box3d'
9
- foo.attributes[:color] = 'red'
10
-
11
- # Dup the dot data:
12
- puts g.to_dot
13
-
14
- # Process the graph to output:
15
- Graphviz::output(g, :path => "test.pdf")
16
-