diagrams-rb 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
  SHA256:
3
- metadata.gz: ca6654ecfc36d63b19a5081d1cd9bc05fc071bd78539da3ee0a37776eaf1508e
4
- data.tar.gz: a1509778ac0d02b70017f2e5eb7cb736843d73ad2842aadafad24f7c6387e323
3
+ metadata.gz: 35a88b43b3643bbb75a44ca94744b7cfe4a617cb6f5439520edd899d32d55757
4
+ data.tar.gz: 272798c4dcf1f125604ac817121a61d76f346fc521fcd98d2f0d6e53f7b42999
5
5
  SHA512:
6
- metadata.gz: 46125e7f16938e35d0141c0f8d954b08d7fc5da41fb5f3d6d3860295a0aaca13610dc4dc84f6c1551a82720f74719e533a0b94850ce1d18306722ba2f3e16e0d
7
- data.tar.gz: eee3bba493ed460caf6dde29078950ae14bacba44f898e5df2ed08003c02651e0c7605005ee96a672637ea57ea6913c5d02370676407c65c669f6896281df0bc
6
+ metadata.gz: 0b408960a0da513d5f87935ea8db6bbedb69b734318675352adb69d9bf175345871dc3047eda9d6b927b014dfce916f91decacb8ec35c87a1cc07e163dd96fbf
7
+ data.tar.gz: 5ff6dbb9349aa5bdda6e4c63f95dc7a52ea34bc699aca4e75fb01d8b6280e4825ffeb89cb722408716fe1c7a94225e8d3c1fd5365f1ff1e0a6463908affbb7c2
data/lib/diagrams/dot.rb CHANGED
@@ -4,73 +4,73 @@ require 'open3'
4
4
  require 'tempfile'
5
5
 
6
6
  module Diagrams
7
- class Dot
8
- GRAPH_DEFAULTS = {
9
- pad: '2.0',
10
- splines: 'ortho',
11
- nodesep: '0.60',
12
- ranksep: '0.75',
13
- rankdir: 'TB',
14
- fontname: '"Sans-Serif"',
15
- fontsize: '15',
16
- fontcolor: '"#2D3436"'
17
- }.freeze
18
-
19
- NODE_DEFAULTS = {
20
- shape: 'box',
21
- style: 'rounded',
22
- fixedsize: 'true',
23
- width: '1.4',
24
- height: '1.4',
25
- labelloc: 'b',
26
- imagescale: 'true',
27
- penwidth: '0',
28
- fontname: '"Sans-Serif"',
29
- fontsize: '13',
30
- fontcolor: '"#2D3436"'
31
- }.freeze
32
-
33
- CLUSTER_DEFAULTS = {
34
- shape: 'box',
35
- style: 'rounded',
36
- labeljust: 'l',
37
- pencolor: '"#AEB6BE"'
38
- }.freeze
39
-
40
- CLUSTER_BGCOLORS = %w[#E5F5FD #EBF3E7 #ECE8F6 #FDF7E3].freeze
7
+ class Dot # rubocop:disable Metrics/ClassLength
8
+ DEFAULTS = {
9
+
10
+ GRAPH: {
11
+ pad: '2.0',
12
+ splines: 'ortho',
13
+ nodesep: '0.60',
14
+ ranksep: '0.75',
15
+ rankdir: 'TB',
16
+ fontname: '"Sans-Serif"',
17
+ fontsize: '15',
18
+ fontcolor: '"#2D3436"'
19
+ },
20
+
21
+ NODE: {
22
+ shape: 'box',
23
+ style: 'rounded',
24
+ fixedsize: 'true',
25
+ width: '1.4',
26
+ height: '1.4',
27
+ labelloc: 'b',
28
+ imagescale: 'true',
29
+ penwidth: '0',
30
+ fontname: '"Sans-Serif"',
31
+ fontsize: '13',
32
+ fontcolor: '"#2D3436"'
33
+ },
34
+
35
+ CLUSTER: {
36
+ shape: 'box',
37
+ style: 'rounded',
38
+ labeljust: 'l',
39
+ pencolor: '"#AEB6BE"'
40
+ },
41
+
42
+ CLUSTER_BGCOLORS: %w[#E5F5FD #EBF3E7 #ECE8F6 #FDF7E3],
43
+
44
+ EDGE: {
45
+ color: '"#7B8894"',
46
+ fontcolor: '"#2D3436"',
47
+ fontname: '"Sans-Serif"',
48
+ fontsize: '13'
49
+ },
50
+
51
+ PADDING: 0.45
41
52
 
42
- EDGE_DEFAULTS = {
43
- color: '"#7B8894"',
44
- fontcolor: '"#2D3436"',
45
- fontname: '"Sans-Serif"',
46
- fontsize: '13'
47
53
  }.freeze
48
54
 
49
- attr_accessor :dot_output, :format, :space, :clen
55
+ attr_accessor :format, :space, :bg_color_len
50
56
 
51
57
  def initialize(**attrs)
52
- @dot_output = "digraph G {\n".dup
53
58
  @format = attrs.delete(:format) || 'png'
54
- GRAPH_DEFAULTS.merge(attrs).each do |key, value|
55
- @dot_output << " #{key}=#{value};\n"
56
- end
59
+ @test = attrs.delete(:test)
57
60
  @space = ' '
58
- @clen = CLUSTER_BGCOLORS.length
61
+ @bg_color_len = DEFAULTS[:CLUSTER_BGCOLORS].length
59
62
  @depth = 0
60
63
  @cluster_idx = -1
61
- end
62
-
63
- def indent
64
- space * @depth
64
+ write_digraph_default(attrs)
65
65
  end
66
66
 
67
67
  def add_node(id, label: '', icon: nil, **attrs)
68
- attributes = NODE_DEFAULTS.merge(attrs).map { |k, v| "#{k}=#{v}" }.join(',')
68
+ attributes = DEFAULTS[:NODE].merge(attrs.merge(height: node_height(label))).map { |k, v| "#{k}=#{v}" }.join(',')
69
69
  dot_output << "#{indent} #{id} [label=\"#{label}\", image=\"#{icon}\", #{attributes}];\n"
70
70
  end
71
71
 
72
72
  def add_edge(from, to:, **attrs)
73
- attributes = EDGE_DEFAULTS.merge(attrs).map { |k, v| "#{k}=#{v}" }.join(',')
73
+ attributes = DEFAULTS[:EDGE].merge(attrs).map { |k, v| "#{k}=#{v}" }.join(',')
74
74
  dot_output << " #{from} -> #{to} [#{attributes}];\n"
75
75
  end
76
76
 
@@ -79,11 +79,12 @@ module Diagrams
79
79
 
80
80
  dot_output << "#{indent}subgraph cluster_#{identifier(cluster_label)} {\n"
81
81
  dot_output << "#{indent} label=\"#{label}\";\n"
82
- dot_output << "#{indent} bgcolor=\"#{CLUSTER_BGCOLORS[@depth % clen]}\";\n"
83
- CLUSTER_DEFAULTS.merge(attrs).each do |key, value|
84
- dot_output << "#{indent} #{key}=#{value};\n"
82
+ dot_output << "#{indent} bgcolor=\"#{DEFAULTS[:CLUSTER_BGCOLORS][@depth % bg_color_len]}\";\n"
83
+ DEFAULTS[:CLUSTER].merge(attrs).each do |key, value|
84
+ dot_output << "#{indent} #{key}=#{value};\n"
85
85
  end
86
86
  @depth += 1
87
+ @dot_output
87
88
  end
88
89
 
89
90
  def end_cluster
@@ -91,13 +92,38 @@ module Diagrams
91
92
  @depth -= 1
92
93
  end
93
94
 
95
+ def generate_image
96
+ dot_output << "}\n"
97
+ return dot_output if @test
98
+
99
+ write_output
100
+ end
101
+
102
+ private
103
+
104
+ def dot_output
105
+ @dot_output ||= "digraph G {\n".dup
106
+ end
107
+
108
+ def write_digraph_default(attrs)
109
+ DEFAULTS[:GRAPH].merge(attrs).each do |key, value|
110
+ dot_output << " #{key}=#{value};\n"
111
+ end
112
+ end
113
+
114
+ def indent
115
+ space * @depth
116
+ end
117
+
118
+ def node_height(label)
119
+ (DEFAULTS[:NODE][:height].to_f + (DEFAULTS[:PADDING] * label.split("\\n").size)).round(1).to_s
120
+ end
121
+
94
122
  def identifier(string)
95
123
  string.gsub(/\s+/, '_')
96
124
  end
97
125
 
98
- def generate_image
99
- output_path = caller_locations.last.path.gsub('.rb', ".#{format}")
100
- dot_output << "}\n"
126
+ def write_output
101
127
  Tempfile.open('temp.dot') do |file|
102
128
  File.write(file.path, dot_output)
103
129
 
@@ -106,5 +132,9 @@ module Diagrams
106
132
  puts stderr unless status.success?
107
133
  end
108
134
  end
135
+
136
+ def output_path
137
+ caller_locations.last.path.gsub('.rb', ".#{format}")
138
+ end
109
139
  end
110
140
  end
@@ -16,7 +16,6 @@ module Diagrams
16
16
 
17
17
  current_module = self
18
18
  path_parts.each do |part|
19
- # Create or get the submodule dynamically
20
19
  submodule = current_module.const_get(part.capitalize) rescue nil
21
20
  unless submodule
22
21
  submodule = Module.new
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Diagrams
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diagrams-rb
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
  - Gerard O'Mahony
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-03 00:00:00.000000000 Z
11
+ date: 2024-11-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  This Ruby-based DSL allows you to create complex Graphviz DOT diagrams programmatically.
@@ -25,7 +25,6 @@ files:
25
25
  - CHANGELOG.md
26
26
  - CODE_OF_CONDUCT.md
27
27
  - LICENSE
28
- - LICENSE.txt
29
28
  - README.md
30
29
  - Rakefile
31
30
  - lib/diagrams-rb.rb
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2024 TODO: Write your name
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.