visualize_ruby 0.2.1 → 0.5.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
  SHA256:
3
- metadata.gz: 284bdd9d3e46c28221780826ae5376b9630aa1d528614a2aa0fc4d577cfabbaa
4
- data.tar.gz: a56f98ea0a7b26cdb206594bd9e1b08407b4cc3035452a56bd022ab1b6046ce4
3
+ metadata.gz: 22fab7a1a3ea1d95c60b2898fe001fa851f8b67111e8c206e6ae0c190074e481
4
+ data.tar.gz: 84ec463c7e0f3f7f5e8585a0c7dd205ec9015fe50b9562549920430cf675e8c3
5
5
  SHA512:
6
- metadata.gz: 3637961a42bca6ea8f9c017d23c42b3627fbf24575faa5da775d98fb9f0b09006d0f93fce0bae4cf3ca7bad7cdfd5283e4b1e6dc3e6a82e3bc5df3d1afdcc23e
7
- data.tar.gz: 0b1660513b29d821a74f402f93aa5383182f50e885777550b577d2e8ca87d8d81a51b6b35ec18c4fc82be01875e7289d3cf1aa2c676f4b31ebf11ca8063729f8
6
+ metadata.gz: f3b08e5a1dbed0d824ee5722114746d77bcc36892a45b0d18005921bfd304a1ce0e4e3ab984e83229d54956698e4cb5dd8e52302e4203c5a8ac543b7d17dc90d
7
+ data.tar.gz: 0d01fb92334397496ff94cf483f9bf47a98222bfe89429489c66a96b21ff6f860f088d2a013d6a9c3451ec806a8a8a1810e1d9f44fb4e3a62fcead7c1fb36856
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## 0.5.0 - 2018-06-22
5
+ ### Enhancement
6
+ * Change some visual display for flow charts.
7
+
8
+ ## 0.4.0 - 2018-06-22
9
+
10
+ ### Enhancement
11
+ * Add special parsing for case statements.
12
+
13
+ ## 0.3.1 - 2018-06-22
14
+
15
+ ### Fix
16
+ * Better handle variables assignment before control flow.
17
+
18
+ ## 0.3.0 - 2018-06-22
19
+
20
+ ### Enhancement
21
+ * Node text now parses ast back to Ruby code. Improve accurately displaying all type of ruby code.
@@ -30,7 +30,8 @@ module VisualizeRuby
30
30
  sub_graph.nodes.each do |node|
31
31
  sub_graph.edges << Edge.new(
32
32
  nodes: [node, graph.nodes.first],
33
- dir: :none
33
+ dir: :none,
34
+ style: :dashed
34
35
  ) if node.name == graph.name
35
36
  end
36
37
  end
@@ -3,20 +3,23 @@ module VisualizeRuby
3
3
  attr_reader :name,
4
4
  :node_a,
5
5
  :node_b,
6
- :dir
7
- def initialize(name: nil, nodes:, dir: :forward)
6
+ :dir,
7
+ :style
8
+
9
+ def initialize(name: nil, nodes:, dir: :forward, style: :solid)
8
10
  @name = name.to_s if name
9
11
  @node_a = nodes[0]
10
12
  @node_b = nodes[1]
11
13
  @dir = dir
14
+ @style = style
12
15
  end
13
16
 
14
17
  def to_a
15
18
  [
16
- node_a.to_sym,
19
+ node_a.name.to_s,
17
20
  name,
18
21
  direction_symbol,
19
- node_b.to_sym,
22
+ node_b.name.to_s,
20
23
  ].compact
21
24
  end
22
25
 
@@ -10,22 +10,62 @@ module VisualizeRuby
10
10
  end
11
11
 
12
12
  def to_graph(type: :digraph, **output)
13
- g = GraphViz.new(:G, type: type, label: label)
14
- nodes = {}
15
- sub_graphs = graphs.reverse.map.with_index do |graph, index|
16
- sub_graph = g.add_graph("cluster#{index}", **{ label: graph.name }.reject { |_, v| v.nil? })
17
- graph.nodes.each do |node|
18
- nodes[node.name] = sub_graph.add_node(node.name, shape: node.shape)
19
- end
13
+ g = main_graph(type)
14
+ sub_graphs = sub_graphs(g)
15
+
16
+ create_edges(sub_graphs)
17
+ g.output(output)
18
+ end
19
+
20
+ private
21
+
22
+ def sub_graphs(g)
23
+ graphs.reverse.map.with_index do |graph, index|
24
+ sub_graph = create_sub_graph(g, graph, index)
25
+ create_nodes(graph, sub_graph)
20
26
  [graph, sub_graph]
21
27
  end
28
+ end
22
29
 
30
+ def nodes
31
+ @nodes ||= {}
32
+ end
33
+
34
+ def create_edges(sub_graphs)
23
35
  sub_graphs.each do |r_graph, g_graph|
24
36
  r_graph.edges.each do |edge|
25
- g_graph.add_edges(nodes[edge.node_a.name], nodes[edge.node_b.name], **{ label: edge.name, dir: edge.dir }.reject { |_, v| v.nil? })
37
+ g_graph.add_edges(
38
+ nodes[edge.node_a.name],
39
+ nodes[edge.node_b.name],
40
+ **compact({ label: edge.name, dir: edge.dir, style: edge.style })
41
+ )
26
42
  end
27
43
  end
28
- g.output(output)
44
+ end
45
+
46
+ def create_sub_graph(g, graph, index)
47
+ g.add_graph(
48
+ "cluster#{index}",
49
+ **compact({ label: graph.name, style: graphs.count == 1 ? :invis : :dotted })
50
+ )
51
+ end
52
+
53
+ def create_nodes(graph, sub_graph)
54
+ graph.nodes.each do |node|
55
+ nodes[node.name] = sub_graph.add_node(
56
+ node.name,
57
+ shape: node.shape,
58
+ style: node.style
59
+ )
60
+ end
61
+ end
62
+
63
+ def main_graph(type)
64
+ GraphViz.new(:G, type: type, label: label)
65
+ end
66
+
67
+ def compact(hash)
68
+ hash.reject { |_, v| v.nil? }
29
69
  end
30
70
  end
31
71
  end
@@ -1,10 +1,12 @@
1
1
  module VisualizeRuby
2
2
  class Node
3
- attr_reader :name
3
+ attr_reader :name, :style
4
4
  attr_accessor :type
5
- def initialize(name:, type: :action)
6
- @name = name.to_s
7
- @type = type
5
+
6
+ def initialize(name:, type: :action, style: :rounded)
7
+ @name = name.to_s
8
+ @type = type
9
+ @style = style
8
10
  end
9
11
 
10
12
  def to_sym
@@ -12,7 +14,7 @@ module VisualizeRuby
12
14
  end
13
15
 
14
16
  def to_a
15
- [type, to_sym]
17
+ [type, name.to_s]
16
18
  end
17
19
 
18
20
  def type_display
@@ -9,6 +9,7 @@ require_relative "parser/if"
9
9
  require_relative "parser/type"
10
10
  require_relative "parser/true"
11
11
  require_relative "parser/false"
12
+ require_relative "parser/case"
12
13
 
13
14
  module VisualizeRuby
14
15
  class Parser
@@ -6,21 +6,7 @@ module VisualizeRuby
6
6
  end
7
7
 
8
8
  def description(ast: @ast)
9
- case ast
10
- when Symbol, String
11
- ast
12
- when NilClass
13
- nil
14
-
15
- else
16
- ast.children.flat_map do |c|
17
- description(ast: c)
18
- end.reject do |c|
19
- c.nil? || c == :""
20
- end.join(" ")
21
- end
22
- rescue NoMethodError
23
- ast
9
+ Unparser.unparse(ast)
24
10
  end
25
11
  end
26
12
  end
@@ -8,11 +8,14 @@ module VisualizeRuby
8
8
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
9
  def parse
10
10
  edges = []
11
+ nodes = []
11
12
  last_node = nil
12
- nodes = @ast.children.to_a.compact.reverse.map do |a|
13
- node = Node.new(name: AstHelper.new(a).description, type: :action)
14
- edges << Edge.new(nodes: [node, last_node]) if last_node
15
- last_node = node
13
+ @ast.children.to_a.compact.reverse.each do |a|
14
+ _nodes, _edges = Parser.new(ast: a).parse
15
+ edges.concat(_edges.reverse)
16
+ nodes.concat(_nodes.reverse)
17
+ edges << Edge.new(nodes: [_nodes.first, last_node]) if last_node
18
+ last_node = _nodes.first
16
19
  end
17
20
 
18
21
  return nodes.reverse, edges.reverse
@@ -0,0 +1,36 @@
1
+ module VisualizeRuby
2
+ class Parser
3
+ class Case
4
+ def initialize(ast)
5
+ @ast = ast
6
+ end
7
+
8
+ # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
+ def parse
10
+ condition, *_whens, _else = @ast.children
11
+ condition_node = Node.new(name: AstHelper.new(condition).description, type: :decision)
12
+ nodes << condition_node
13
+ _whens.each do |_when|
14
+ edge_name, actions = _when.children
15
+ action_nodes, action_edges = Parser.new(ast: actions).parse
16
+ edges << Edge.new(name: AstHelper.new(edge_name).description, nodes: [condition_node, action_nodes.first])
17
+ nodes.concat(action_nodes)
18
+ edges.concat(action_edges)
19
+ end
20
+ _else_node = Node.new(name: AstHelper.new(_else).description, type: :action)
21
+ _else_edge = Edge.new(name: "else", nodes: [condition_node, _else_node])
22
+ nodes << _else_node
23
+ edges << _else_edge
24
+ return nodes, edges
25
+ end
26
+
27
+ def nodes
28
+ @nodes ||= []
29
+ end
30
+
31
+ def edges
32
+ @edges ||= []
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module VisualizeRuby
2
- VERSION = "0.2.1"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visualize_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-17 00:00:00.000000000 Z
11
+ date: 2018-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -123,6 +123,7 @@ files:
123
123
  - ".gitignore"
124
124
  - ".rspec"
125
125
  - ".travis.yml"
126
+ - CHANGELOG.md
126
127
  - CODE_OF_CONDUCT.md
127
128
  - Gemfile
128
129
  - LICENSE.txt
@@ -141,6 +142,7 @@ files:
141
142
  - lib/visualize_ruby/parser/and.rb
142
143
  - lib/visualize_ruby/parser/ast_helper.rb
143
144
  - lib/visualize_ruby/parser/begin.rb
145
+ - lib/visualize_ruby/parser/case.rb
144
146
  - lib/visualize_ruby/parser/false.rb
145
147
  - lib/visualize_ruby/parser/if.rb
146
148
  - lib/visualize_ruby/parser/or.rb