gviz 0.2.0 → 0.3.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: 9143e573c91a80a888a66007403500d36f5406dc
4
- data.tar.gz: d8512ead6616fd21edd84d61730ec0c70af97682
3
+ metadata.gz: 65adf31d163c16b8127354d5529a1dada514fd8d
4
+ data.tar.gz: 0d5baf89bafd6865fd39d3904bcf3e1d528e57c4
5
5
  SHA512:
6
- metadata.gz: 8c9af9c343b65c8d03669d4b604934fcbb3c0e65a88f2da3701be27f87acc2a199bcdb83237cdb6492736c419661438d5157a50e63d9349d6ec81622102e9586
7
- data.tar.gz: cf4ee9d30253268b5c6115491ec7f2f136f89f5d82667cfbd41e69b3d06ba13da4e7c8ecbd5547b32767142cbb05365ed1d4e6af4b950da0445b27bf338b17fb
6
+ metadata.gz: 4a79d270bc63ea086223a6a8769a2e267f89d32c51528c6aced6a9c8fa6e7c528653b82fd7c55595a826dafa3a9bf0b02100f0663b716a80f6238a2c6900e6cb
7
+ data.tar.gz: 2a1dabc7ba08e9d49ff5e919b3d82f4a2dc0ca097b31e8685dda0bb1dbae2a21765ea18b9d085085a35aa56222d776cac22e2c938742fdbd7682f890c7779a40
data/README.md CHANGED
@@ -48,7 +48,7 @@ Add some attributes to the graph, nodes, edges.
48
48
  ## add bgcolor to a graph(global method)
49
49
  require "gviz"
50
50
 
51
- Graph.graph do
51
+ Graph do
52
52
  route :main => [:init, :parse, :cleanup, :printf]
53
53
  route :init => :make, :parse => :execute
54
54
  route :execute => [:make, :compare, :printf]
@@ -74,7 +74,7 @@ Modify some.
74
74
  ## define subgraph(subgraph method)
75
75
  require "gviz"
76
76
 
77
- Graph.graph do
77
+ Graph do
78
78
  route :main => [:init, :parse, :cleanup, :printf]
79
79
  route :init => :make, :parse => :execute
80
80
  route :execute => [:make, :compare, :printf]
@@ -102,6 +102,27 @@ This outputs below.
102
102
 
103
103
  ![sample3](http://github.com/melborne/Gviz/raw/master/examples/sample3.png)
104
104
 
105
+ ## Use for Drawing
106
+
107
+ Gviz helps you easily to draw shapes on an exact position using shape-named methods.
108
+
109
+ require "gviz"
110
+
111
+ Graph do
112
+ line :a, from:[-100,0], to:[100,0]
113
+ line :b, from:[0,-100], to:[0,100]
114
+ circle :c
115
+ rect :d, x:50, y:50, fillcolor:"green", label:"Rect"
116
+ triangle :e, x:50, y:-50, fillcolor:"cyan"
117
+ diamond :f, x:-50, y:50, fillcolor:"magenta"
118
+ egg :g, x:-50, y:-50, fillcolor:"yellow", label:"Egg"
119
+
120
+ save :draw
121
+ end
122
+
123
+ This outputs below(consider a scale if save to output formats).
124
+
125
+ ![sample4](http://github.com/melborne/Gviz/raw/drawing/examples/sample4.png)
105
126
 
106
127
  Another examples are at `examples` directory
107
128
 
Binary file
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
- gem.required_ruby_version = '>=1.9.3'
19
+ gem.required_ruby_version = '>=2.0.0'
20
20
  gem.add_development_dependency 'rspec'
21
21
  gem.add_dependency 'thor'
22
22
  end
@@ -1,5 +1,9 @@
1
1
  # encoding: UTF-8
2
+ require "gviz/graphviz_attrs"
3
+ require "gviz/draw"
4
+
2
5
  class Gviz
6
+ include Draw
3
7
  end
4
8
 
5
9
  # +Graph+ fuction is a shortcut method of `Gviz#graph`.
@@ -7,6 +11,6 @@ def Graph(name=:G, type=:digraph, &blk)
7
11
  Gviz.new(name, type).graph(&blk)
8
12
  end
9
13
 
10
- %w(core node edge version system_extension graphviz_attrs command).each do |lib|
11
- require_relative 'gviz/' + lib
14
+ %w(core node edge version system_extension command).each do |lib|
15
+ require 'gviz/' + lib
12
16
  end
@@ -0,0 +1,64 @@
1
+ # Draw module includes a set of methods for specific shapes.
2
+ # Each methods defines with shape name and its positions.
3
+ module Draw
4
+ # Define a node with ellipse shape.
5
+ def ellipse(id, x:0, y:0, **attrs)
6
+ draw_init
7
+ attrs = {label:"", color:"black", fillcolor:"#FFFFFF00"}.merge(attrs)
8
+ attrs.update(shape:"ellipse", pos:"#{x},#{y}!")
9
+ node(id, attrs)
10
+ end
11
+
12
+ # Define a node with circle shape.
13
+ def circle(id, x:0, y:0, r:0.5, **attrs)
14
+ attrs.update(width:r*2, height:r*2)
15
+ ellipse(id, x:x, y:y, **attrs)
16
+ end
17
+
18
+ # Define a node with square shape.
19
+ def square(id, x:0, y:0, **attrs)
20
+ w, h = %i(width height).map { |at| attrs.delete at }
21
+ size = w || h || 1
22
+ attrs.update(width:size, height:size)
23
+ rect(id, x:x, y:y, **attrs)
24
+ end
25
+
26
+ # Define a node with point shape.
27
+ def point(id, x:0, y:0, **attrs)
28
+ draw_init
29
+ attrs.update(shape:"point", pos:"#{x},#{y}!")
30
+ node(id, attrs)
31
+ end
32
+
33
+ # Define a line with an edge and two point nodes.
34
+ def line(id, from:[0,0], **attrs)
35
+ draw_init
36
+ unless to = attrs.delete(:to)
37
+ raise ArgumentError, "Argument 'to' is required"
38
+ end
39
+ n1_id, n2_id = [1, 2].map { |i| "#{id}#{i}".to_id }
40
+ point(n1_id, x:from[0], y:from[1],
41
+ color:"#FFFFFF00", fillcolor:"#FFFFFF00")
42
+ point(n2_id, x:to[0], y:to[1],
43
+ color:"#FFFFFF00", fillcolor:"#FFFFFF00")
44
+ attrs.update(arrowhead:"none")
45
+ edge(:"#{n1_id}_#{n2_id}", attrs)
46
+ end
47
+
48
+ # Define methods for other shapes
49
+ Gviz::SHAPES.each do |shape|
50
+ next if %w(ellipse circle square point plaintext none).include?(shape)
51
+ define_method(shape) do |id, x:0, y:0, **attrs|
52
+ draw_init
53
+ attrs = {label:"", color:"black", fillcolor:"#FFFFFF00"}.merge(attrs)
54
+ attrs.update(shape:shape, pos:"#{x},#{y}!")
55
+ node(id, attrs)
56
+ end
57
+ end
58
+
59
+ private
60
+ def draw_init
61
+ global(layout:"neato")
62
+ nodes(style:"filled")
63
+ end
64
+ end
@@ -1,3 +1,3 @@
1
1
  class Gviz
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,157 @@
1
+ require "spec_helper"
2
+
3
+ describe Gviz do
4
+ let(:gv) { Gviz.new }
5
+
6
+ describe "#circle" do
7
+ let(:defo_attrs) { {shape:"ellipse", pos:"0,0!", width:1, height:1, label:"", color:"black", fillcolor:"#FFFFFF00"} }
8
+ context 'without attributes' do
9
+ it 'returns a circle node with default attributes' do
10
+ circle = gv.circle(:a)
11
+ expect(circle).to be_a_instance_of Gviz::Node
12
+ expect(circle.id).to eq :a
13
+ expect(circle.attrs).to eq defo_attrs
14
+ end
15
+ end
16
+
17
+ context 'with some attributes' do
18
+ it 'returns a circle node with attributes' do
19
+ circle = gv.circle(:a, x:10, y:-20, r:1, fillcolor:"green", label:"a")
20
+ attrs = defo_attrs.merge(pos:"10,-20!", width:2, height:2, label:"a", fillcolor:"green")
21
+ expect(circle.attrs).to eq attrs
22
+ end
23
+
24
+ it 'overwrites shape and pos attrs if they are passed' do
25
+ circle = gv.circle(:a, shape:"box", pos:"10,10!")
26
+ expect(circle.attrs).to eq defo_attrs
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "#ellipse" do
32
+ let(:defo_attrs) { {shape:"ellipse", pos:"0,0!", label:"", color:"black", fillcolor:"#FFFFFF00"} }
33
+ context 'without attributes' do
34
+ it 'returns a ellipse node with default attributes' do
35
+ ellipse = gv.ellipse(:a)
36
+ expect(ellipse).to be_a_instance_of Gviz::Node
37
+ expect(ellipse.id).to eq :a
38
+ expect(ellipse.attrs).to eq defo_attrs
39
+ end
40
+ end
41
+
42
+ context 'with some attributes' do
43
+ it 'returns a ellipse node with attributes' do
44
+ ellipse = gv.ellipse(:a, x:10, y:-20, fillcolor:"green", label:"a", width:2, height:4)
45
+ attrs = defo_attrs.merge(pos:"10,-20!", width:2, height:4, label:"a", fillcolor:"green")
46
+ expect(ellipse.attrs).to eq attrs
47
+ end
48
+
49
+ it 'overwrites shape and pos attrs if they are passed' do
50
+ ellipse = gv.ellipse(:a, shape:"box", pos:"10,10!")
51
+ expect(ellipse.attrs).to eq defo_attrs
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "#rect" do
57
+ let(:defo_attrs) { {shape:"rect", pos:"0,0!", label:"", color:"black", fillcolor:"#FFFFFF00"} }
58
+ context 'without attributes' do
59
+ it 'returns a rect node with default attributes' do
60
+ rect = gv.rect(:a)
61
+ expect(rect).to be_a_instance_of Gviz::Node
62
+ expect(rect.id).to eq :a
63
+ expect(rect.attrs).to eq defo_attrs
64
+ end
65
+ end
66
+
67
+ context 'with some attributes' do
68
+ it 'returns a rect node with attributes' do
69
+ rect = gv.rect(:a, x:10, y:-20, fillcolor:"green", label:"a", width:2, height:4)
70
+ attrs = defo_attrs.merge(pos:"10,-20!", width:2, height:4, label:"a", fillcolor:"green")
71
+ expect(rect.attrs).to eq attrs
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "#square" do
77
+ let(:defo_attrs) { {shape:"rect", pos:"0,0!", width:1, height:1, label:"", color:"black", fillcolor:"#FFFFFF00"} }
78
+ context 'without attributes' do
79
+ it 'returns a square node with default attributes' do
80
+ square = gv.square(:a)
81
+ expect(square.attrs).to eq defo_attrs
82
+ end
83
+ end
84
+
85
+ context 'with some attributes' do
86
+ it 'returns a square node with attributes' do
87
+ square = gv.square(:a, x:10, y:-20, fillcolor:"green", label:"a", width:2)
88
+ attrs = defo_attrs.merge(pos:"10,-20!", width:2, height:2, label:"a", fillcolor:"green")
89
+ expect(square.attrs).to eq attrs
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "#point" do
95
+ let(:defo_attrs) { {shape:"point", pos:"0,0!"} }
96
+ context 'without attributes' do
97
+ it 'returns a point node with default attributes' do
98
+ point = gv.point(:a)
99
+ expect(point.attrs).to eq defo_attrs
100
+ end
101
+ end
102
+
103
+ context 'with some attributes' do
104
+ it 'returns a point node with attributes' do
105
+ point = gv.point(:a, x:10, y:-20, color:"green", width:0.1)
106
+ attrs = defo_attrs.merge(pos:"10,-20!", width:0.1, color:"green")
107
+ expect(point.attrs).to eq attrs
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "#line" do
113
+ it 'generate an edge between two points' do
114
+ line = gv.line(:a, from:[50,50], to:[100,100], color:"green")
115
+ expect(gv.nodeset.size).to eq 2
116
+ expect(gv.edgeset.size).to eq 1
117
+ n1, n2 = gv.nodeset
118
+ expect(n1.attrs).to eq(shape:"point", pos:"50,50!", color:"#FFFFFF00", fillcolor:"#FFFFFF00")
119
+ expect(n2.attrs).to eq(shape:"point", pos:"100,100!", color:"#FFFFFF00", fillcolor:"#FFFFFF00")
120
+ expect(line.id).to eq :"#{n1.id}_#{n2.id}"
121
+ expect(line.attrs).to eq(arrowhead:"none", color:"green")
122
+ end
123
+
124
+ context 'without from or to arguments' do
125
+ it 'returns [0,0] without "from" argument' do
126
+ line = gv.line(:a, to:[100,100])
127
+ n1, n2 = gv.nodeset
128
+ expect(n1.attrs).to eq(shape:"point", pos:"0,0!", color:"#FFFFFF00", fillcolor:"#FFFFFF00")
129
+ expect(n2.attrs).to eq(shape:"point", pos:"100,100!", color:"#FFFFFF00", fillcolor:"#FFFFFF00")
130
+ end
131
+
132
+ it 'raise an ArgumentError without "to" argument' do
133
+ expect{ gv.line(:a, from:[0,0]) }.to raise_error ArgumentError
134
+ end
135
+ end
136
+ end
137
+
138
+ describe "shapes" do
139
+ let(:defo_attrs) { {pos:"0,0!", label:"", color:"black", fillcolor:"#FFFFFF00"} }
140
+ context 'without attributes' do
141
+ it 'returns a egg node with default attributes' do
142
+ shape = gv.egg(:a)
143
+ expect(shape).to be_a_instance_of Gviz::Node
144
+ expect(shape.id).to eq :a
145
+ expect(shape.attrs).to eq defo_attrs.update(shape:"egg")
146
+ end
147
+ end
148
+
149
+ context 'with some attributes' do
150
+ it 'returns a egg node with attributes' do
151
+ shape = gv.egg(:a, x:10, y:-20, fillcolor:"green", label:"a", width:2, height:4)
152
+ attrs = defo_attrs.merge(pos:"10,-20!", width:2, height:4, label:"a", fillcolor:"green")
153
+ expect(shape.attrs).to eq attrs.update(shape:"egg")
154
+ end
155
+ end
156
+ end
157
+ end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gviz
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
  - kyoendo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-08 00:00:00.000000000 Z
11
+ date: 2014-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: thor
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Ruby's interface of graphviz
@@ -46,7 +46,7 @@ executables:
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
- - .gitignore
49
+ - ".gitignore"
50
50
  - Gemfile
51
51
  - LICENSE.txt
52
52
  - README.md
@@ -58,11 +58,13 @@ files:
58
58
  - examples/sample1.png
59
59
  - examples/sample2.png
60
60
  - examples/sample3.png
61
+ - examples/sample4.png
61
62
  - examples/shapes.rb
62
63
  - gviz.gemspec
63
64
  - lib/gviz.rb
64
65
  - lib/gviz/command.rb
65
66
  - lib/gviz/core.rb
67
+ - lib/gviz/draw.rb
66
68
  - lib/gviz/edge.rb
67
69
  - lib/gviz/graphviz_attrs.rb
68
70
  - lib/gviz/node.rb
@@ -70,6 +72,7 @@ files:
70
72
  - lib/gviz/version.rb
71
73
  - spec/fixtures/graph.ru
72
74
  - spec/gviz_command_spec.rb
75
+ - spec/gviz_draw_spec.rb
73
76
  - spec/gviz_edge_spec.rb
74
77
  - spec/gviz_node_spec.rb
75
78
  - spec/gviz_spec.rb
@@ -84,23 +87,24 @@ require_paths:
84
87
  - lib
85
88
  required_ruby_version: !ruby/object:Gem::Requirement
86
89
  requirements:
87
- - - '>='
90
+ - - ">="
88
91
  - !ruby/object:Gem::Version
89
- version: 1.9.3
92
+ version: 2.0.0
90
93
  required_rubygems_version: !ruby/object:Gem::Requirement
91
94
  requirements:
92
- - - '>='
95
+ - - ">="
93
96
  - !ruby/object:Gem::Version
94
97
  version: '0'
95
98
  requirements: []
96
99
  rubyforge_project:
97
- rubygems_version: 2.0.3
100
+ rubygems_version: 2.2.0
98
101
  signing_key:
99
102
  specification_version: 4
100
103
  summary: Ruby's interface of graphviz. It generate a dot file with simple ruby's syntax.
101
104
  test_files:
102
105
  - spec/fixtures/graph.ru
103
106
  - spec/gviz_command_spec.rb
107
+ - spec/gviz_draw_spec.rb
104
108
  - spec/gviz_edge_spec.rb
105
109
  - spec/gviz_node_spec.rb
106
110
  - spec/gviz_spec.rb