visualize_ruby 0.5.0 → 0.6.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: 22fab7a1a3ea1d95c60b2898fe001fa851f8b67111e8c206e6ae0c190074e481
4
- data.tar.gz: 84ec463c7e0f3f7f5e8585a0c7dd205ec9015fe50b9562549920430cf675e8c3
3
+ metadata.gz: 99eee38b1b8d2b5fc815dc04c37b5fec84a73762b66295898bed31bee5ee235d
4
+ data.tar.gz: aa7d28887b07d5b4ae096b2341fc74e2906bbe7452effcb5f0a19db7ee42f2a9
5
5
  SHA512:
6
- metadata.gz: f3b08e5a1dbed0d824ee5722114746d77bcc36892a45b0d18005921bfd304a1ce0e4e3ab984e83229d54956698e4cb5dd8e52302e4203c5a8ac543b7d17dc90d
7
- data.tar.gz: 0d01fb92334397496ff94cf483f9bf47a98222bfe89429489c66a96b21ff6f860f088d2a013d6a9c3451ec806a8a8a1810e1d9f44fb4e3a62fcead7c1fb36856
6
+ metadata.gz: f552a2787de005e1459951380fb1d58e412e2901298a73c65a1a4b3d984eb2d1f28d7681b1e3ea62fec3d89131e6507263d644037b0e5037155f4492aa4bd1ad
7
+ data.tar.gz: 87539a36683428a0debda868deac2e9fc50c1f207ef4b64a9c87f7664482bb8929e5cdb5fbbfd2cea4cc8db8a0d1312e01e7739183bff415f833e15112b05f20
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## 0.6.0 - 2018-06-27
5
+ ### Enhancement
6
+ * Visualize Enumerable looping
7
+ * Display nodes for block arguments
8
+
4
9
  ## 0.5.0 - 2018-06-22
5
10
  ### Enhancement
6
11
  * Change some visual display for flow charts.
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 TODO: Write your name
3
+ Copyright (c) 2018 Dustin Zeisler
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -36,7 +36,7 @@ ruby_code = <<-RUBY
36
36
  RUBY
37
37
 
38
38
  results = VisualizeRuby::Builder.new(ruby_code: ruby_code).build
39
- VisualizeRuby::Graphviz.new(*results).to_graph(png: "example.png")
39
+ VisualizeRuby::Graphviz.new(*results).to_graph(path: "example.png")
40
40
  ```
41
41
 
42
42
  ## Development
@@ -47,7 +47,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
47
47
 
48
48
  ## Contributing
49
49
 
50
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/visualize_ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zeisler/visualize_ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
51
51
 
52
52
  ## License
53
53
 
@@ -55,4 +55,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
55
55
 
56
56
  ## Code of Conduct
57
57
 
58
- Everyone interacting in the VisualizeRuby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/visualize_ruby/blob/master/CODE_OF_CONDUCT.md).
58
+ Everyone interacting in the VisualizeRuby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zeisler/visualize_ruby/blob/master/CODE_OF_CONDUCT.md).
@@ -4,14 +4,16 @@ module VisualizeRuby
4
4
  :node_a,
5
5
  :node_b,
6
6
  :dir,
7
- :style
7
+ :style,
8
+ :color
8
9
 
9
- def initialize(name: nil, nodes:, dir: :forward, style: :solid)
10
+ def initialize(name: nil, nodes:, dir: :forward, style: :solid, color: nil)
10
11
  @name = name.to_s if name
11
12
  @node_a = nodes[0]
12
13
  @node_b = nodes[1]
13
14
  @dir = dir
14
15
  @style = style
16
+ @color = color
15
17
  end
16
18
 
17
19
  def to_a
@@ -3,7 +3,7 @@ module VisualizeRuby
3
3
  attr_reader :name, :nodes, :edges
4
4
 
5
5
  def initialize(ruby_code:, name: nil)
6
- @name = name.to_s
6
+ @name = name.to_s if name
7
7
  @nodes, @edges = Parser.new(ruby_code).parse
8
8
  end
9
9
 
@@ -1,4 +1,4 @@
1
- require "ruby-graphviz"
1
+ require "graphviz"
2
2
 
3
3
  module VisualizeRuby
4
4
  class Graphviz
@@ -9,19 +9,34 @@ module VisualizeRuby
9
9
  @label = label
10
10
  end
11
11
 
12
- def to_graph(type: :digraph, **output)
13
- g = main_graph(type)
14
- sub_graphs = sub_graphs(g)
15
-
16
- create_edges(sub_graphs)
17
- g.output(output)
12
+ def to_graph(format: nil, path: nil)
13
+ build
14
+ if format == String
15
+ str = StringIO.new
16
+ main_graph.dump_graph(str)
17
+ str.string
18
+ else
19
+ ::Graphviz.output(main_graph, path: path, format: format)
20
+ end
18
21
  end
19
22
 
20
23
  private
21
24
 
22
- def sub_graphs(g)
23
- graphs.reverse.map.with_index do |graph, index|
24
- sub_graph = create_sub_graph(g, graph, index)
25
+ def label
26
+ if graphs.count == 1
27
+ @label = graphs.first.name
28
+ else
29
+ @label
30
+ end
31
+ end
32
+
33
+ def build
34
+ create_edges(sub_graphs)
35
+ end
36
+
37
+ def sub_graphs
38
+ @sub_graphs ||= graphs.reverse.map.with_index do |graph, index|
39
+ sub_graph = create_sub_graph(graph, index)
25
40
  create_nodes(graph, sub_graph)
26
41
  [graph, sub_graph]
27
42
  end
@@ -34,18 +49,19 @@ module VisualizeRuby
34
49
  def create_edges(sub_graphs)
35
50
  sub_graphs.each do |r_graph, g_graph|
36
51
  r_graph.edges.each do |edge|
37
- g_graph.add_edges(
52
+ ::Graphviz::Edge.new(
53
+ g_graph,
38
54
  nodes[edge.node_a.name],
39
55
  nodes[edge.node_b.name],
40
- **compact({ label: edge.name, dir: edge.dir, style: edge.style })
56
+ **compact({ label: edge.name, dir: edge.dir, style: edge.style, color: edge.color })
41
57
  )
42
58
  end
43
59
  end
44
60
  end
45
61
 
46
- def create_sub_graph(g, graph, index)
47
- g.add_graph(
48
- "cluster#{index}",
62
+ def create_sub_graph(graph, index)
63
+ main_graph.add_subgraph(
64
+ "cluster_#{index}",
49
65
  **compact({ label: graph.name, style: graphs.count == 1 ? :invis : :dotted })
50
66
  )
51
67
  end
@@ -60,8 +76,8 @@ module VisualizeRuby
60
76
  end
61
77
  end
62
78
 
63
- def main_graph(type)
64
- GraphViz.new(:G, type: type, label: label)
79
+ def main_graph
80
+ @main_graph ||= ::Graphviz::Graph.new(:G, compact(label: label))
65
81
  end
66
82
 
67
83
  def compact(hash)
@@ -23,6 +23,8 @@ module VisualizeRuby
23
23
  "<>"
24
24
  when :action
25
25
  "[]"
26
+ when :argument
27
+ "[>"
26
28
  end
27
29
  end
28
30
 
@@ -32,6 +34,8 @@ module VisualizeRuby
32
34
  :diamond
33
35
  when :action
34
36
  :ellipse
37
+ when :argument
38
+ :box
35
39
  end
36
40
  end
37
41
 
@@ -1,10 +1,6 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class And
4
- def initialize(ast)
5
- @ast = ast
6
- end
7
-
3
+ class And < Base
8
4
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
5
  def parse
10
6
  last_node = nil
@@ -6,6 +6,7 @@ module VisualizeRuby
6
6
  end
7
7
 
8
8
  def description(ast: @ast)
9
+ return ast if ast.is_a?(Symbol)
9
10
  Unparser.unparse(ast)
10
11
  end
11
12
  end
@@ -0,0 +1,19 @@
1
+ module VisualizeRuby
2
+ class Parser
3
+ class Base
4
+ def initialize(ast)
5
+ @ast = ast
6
+ end
7
+
8
+ private
9
+
10
+ def nodes
11
+ @nodes ||= []
12
+ end
13
+
14
+ def edges
15
+ @edges ||= []
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,14 +1,8 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class Begin
4
- def initialize(ast)
5
- @ast = ast
6
- end
7
-
3
+ class Begin < Base
8
4
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
5
  def parse
10
- edges = []
11
- nodes = []
12
6
  last_node = nil
13
7
  @ast.children.to_a.compact.reverse.each do |a|
14
8
  _nodes, _edges = Parser.new(ast: a).parse
@@ -0,0 +1,43 @@
1
+ module VisualizeRuby
2
+ class Parser
3
+ class Block < Base
4
+ # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
5
+ def parse
6
+ iterator, arguments, action = @ast.children
7
+ item = arguments.children[0]
8
+ collection, iterator_type = iterator.to_a
9
+ if enumerable?(collection) || enumerable?(iterator_type)
10
+ enumerable(action, collection, iterator_type, item)
11
+ else
12
+ yield_block(action, item, iterator)
13
+ end
14
+ return nodes, edges
15
+ end
16
+
17
+ private
18
+
19
+ def yield_block(action, item, on_object)
20
+ nodes << on_object_node = Node.new(name: AstHelper.new(on_object).description)
21
+ nodes << item_node = Node.new(name: AstHelper.new(item).description, type: :argument)
22
+ nodes << action_node = Node.new(name: AstHelper.new(action).description)
23
+ edges << Edge.new(nodes: [on_object_node, item_node])
24
+ edges << Edge.new(nodes: [item_node, action_node], color: "orange")
25
+ end
26
+
27
+ def enumerable(action, collection, iterator_type, item)
28
+ nodes << collection_node = Node.new(name: AstHelper.new(collection).description)
29
+ nodes << item_node = Node.new(name: AstHelper.new(item).description, type: :argument)
30
+ nodes << iterator_node = Node.new(name: iterator_type)
31
+ nodes << action_node = Node.new(name: AstHelper.new(action).description)
32
+ edges << Edge.new(nodes: [collection_node, iterator_node])
33
+ edges << Edge.new(nodes: [iterator_node, item_node], color: "blue")
34
+ edges << Edge.new(nodes: [item_node, action_node], color: "blue")
35
+ edges << Edge.new(nodes: [action_node, iterator_node], color: "blue", name: "↺")
36
+ end
37
+
38
+ def enumerable?(meth)
39
+ meth == :each || Enumerable.instance_methods.include?(meth)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,10 +1,6 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class Case
4
- def initialize(ast)
5
- @ast = ast
6
- end
7
-
3
+ class Case < Base
8
4
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
5
  def parse
10
6
  condition, *_whens, _else = @ast.children
@@ -23,14 +19,6 @@ module VisualizeRuby
23
19
  edges << _else_edge
24
20
  return nodes, edges
25
21
  end
26
-
27
- def nodes
28
- @nodes ||= []
29
- end
30
-
31
- def edges
32
- @edges ||= []
33
- end
34
22
  end
35
23
  end
36
24
  end
@@ -1,10 +1,6 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class If
4
- def initialize(ast)
5
- @ast = ast
6
- end
7
-
3
+ class If < Base
8
4
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
5
  def parse
10
6
  break_ast
@@ -44,14 +40,6 @@ module VisualizeRuby
44
40
  def break_ast
45
41
  @condition, @on_true, @on_false = @ast.children.to_a
46
42
  end
47
-
48
- def nodes
49
- @nodes ||= []
50
- end
51
-
52
- def edges
53
- @edges ||= []
54
- end
55
43
  end
56
44
  end
57
45
  end
@@ -1,19 +1,15 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class Or
4
- def initialize(ast)
5
- @ast = ast
6
- end
3
+ class Or < Base
7
4
 
8
5
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
6
  def parse
10
7
  last_node = nil
11
- edges = []
12
- nodes = @ast.children.reverse.map do |c|
8
+ @ast.children.reverse.map do |c|
13
9
  node = Node.new(name: AstHelper.new(c).description, type: :decision)
14
10
  edges << Edge.new(name: "OR", nodes: [node, last_node]) if last_node
15
11
  last_node = node
16
- node
12
+ nodes << node
17
13
  end.reverse
18
14
  return nodes, edges
19
15
  end
@@ -1,10 +1,6 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class Send
4
- def initialize(ast)
5
- @ast = ast
6
- end
7
-
3
+ class Send < Base
8
4
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
5
  def parse
10
6
  return [Node.new(name: AstHelper.new(@ast).description, type: :action)], []
@@ -1,10 +1,6 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class Str
4
- def initialize(ast)
5
- @ast = ast
6
- end
7
-
3
+ class Str < Base
8
4
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
5
  def parse
10
6
  return [Node.new(name: AstHelper.new(@ast).description, type: :action)], []
@@ -1,10 +1,6 @@
1
1
  module VisualizeRuby
2
2
  class Parser
3
- class Type
4
- def initialize(ast)
5
- @ast = ast
6
- end
7
-
3
+ class Type < Base
8
4
  # @return [Array<VisualizeRuby::Node>, Array<VisualizeRuby::Edge>]
9
5
  def parse
10
6
  return [Node.new(name: @ast.type, type: :action)], []
@@ -1,4 +1,5 @@
1
1
  require "parser/current"
2
+ require_relative "parser/base"
2
3
  require_relative "parser/or"
3
4
  require_relative "parser/and"
4
5
  require_relative "parser/ast_helper"
@@ -10,6 +11,7 @@ require_relative "parser/type"
10
11
  require_relative "parser/true"
11
12
  require_relative "parser/false"
12
13
  require_relative "parser/case"
14
+ require_relative "parser/block"
13
15
 
14
16
  module VisualizeRuby
15
17
  class Parser
@@ -1,3 +1,3 @@
1
1
  module VisualizeRuby
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "rake", "~> 12.3", ">= 12.3.1"
27
27
  spec.add_development_dependency "rspec", "~> 3.7"
28
28
 
29
- spec.add_runtime_dependency "ruby-graphviz", "~> 1.2", ">= 1.2.3"
29
+ spec.add_runtime_dependency "graphviz", "~> 1.0"
30
30
  spec.add_runtime_dependency "dissociated_introspection", "~> 0.9.1"
31
31
  spec.add_runtime_dependency "parser", "~> 2.1", ">= 2.5.1.0"
32
32
  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.5.0
4
+ version: 0.6.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-22 00:00:00.000000000 Z
11
+ date: 2018-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,25 +59,19 @@ dependencies:
59
59
  - !ruby/object:Gem::Version
60
60
  version: '3.7'
61
61
  - !ruby/object:Gem::Dependency
62
- name: ruby-graphviz
62
+ name: graphviz
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '1.2'
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: 1.2.3
67
+ version: '1.0'
71
68
  type: :runtime
72
69
  prerelease: false
73
70
  version_requirements: !ruby/object:Gem::Requirement
74
71
  requirements:
75
72
  - - "~>"
76
73
  - !ruby/object:Gem::Version
77
- version: '1.2'
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: 1.2.3
74
+ version: '1.0'
81
75
  - !ruby/object:Gem::Dependency
82
76
  name: dissociated_introspection
83
77
  requirement: !ruby/object:Gem::Requirement
@@ -141,7 +135,9 @@ files:
141
135
  - lib/visualize_ruby/parser.rb
142
136
  - lib/visualize_ruby/parser/and.rb
143
137
  - lib/visualize_ruby/parser/ast_helper.rb
138
+ - lib/visualize_ruby/parser/base.rb
144
139
  - lib/visualize_ruby/parser/begin.rb
140
+ - lib/visualize_ruby/parser/block.rb
145
141
  - lib/visualize_ruby/parser/case.rb
146
142
  - lib/visualize_ruby/parser/false.rb
147
143
  - lib/visualize_ruby/parser/if.rb