visualize_ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in visualize_ruby.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ visualize_ruby (0.1.0)
5
+ dissociated_introspection (~> 0.9.1)
6
+ parser (~> 2.1, >= 2.5.1.0)
7
+ ruby-graphviz (~> 1.2, >= 1.2.3)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ abstract_type (0.0.7)
13
+ adamantium (0.2.0)
14
+ ice_nine (~> 0.11.0)
15
+ memoizable (~> 0.4.0)
16
+ ast (2.4.0)
17
+ concord (0.1.5)
18
+ adamantium (~> 0.2.0)
19
+ equalizer (~> 0.0.9)
20
+ diff-lcs (1.3)
21
+ dissociated_introspection (0.9.1)
22
+ parser (~> 2.1, >= 2.1.9)
23
+ unparser (~> 0.2, >= 0.2.6)
24
+ equalizer (0.0.11)
25
+ ice_nine (0.11.2)
26
+ memoizable (0.4.2)
27
+ thread_safe (~> 0.3, >= 0.3.1)
28
+ parser (2.5.1.0)
29
+ ast (~> 2.4.0)
30
+ procto (0.0.3)
31
+ rake (12.3.1)
32
+ rspec (3.7.0)
33
+ rspec-core (~> 3.7.0)
34
+ rspec-expectations (~> 3.7.0)
35
+ rspec-mocks (~> 3.7.0)
36
+ rspec-core (3.7.1)
37
+ rspec-support (~> 3.7.0)
38
+ rspec-expectations (3.7.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.7.0)
41
+ rspec-mocks (3.7.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.7.0)
44
+ rspec-support (3.7.1)
45
+ ruby-graphviz (1.2.3)
46
+ thread_safe (0.3.6)
47
+ unparser (0.2.7)
48
+ abstract_type (~> 0.0.7)
49
+ adamantium (~> 0.2.0)
50
+ concord (~> 0.1.5)
51
+ diff-lcs (~> 1.3)
52
+ equalizer (~> 0.0.9)
53
+ parser (>= 2.3.1.2, < 2.6)
54
+ procto (~> 0.0.2)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ bundler (~> 1.16)
61
+ rake (~> 12.3, >= 12.3.1)
62
+ rspec (~> 3.7)
63
+ visualize_ruby!
64
+
65
+ BUNDLED WITH
66
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 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.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # VisualizeRuby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/visualize_ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'visualize_ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install visualize_ruby
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 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.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ 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).
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/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "visualize_ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "visualize_ruby"
@@ -0,0 +1,9 @@
1
+ require "visualize_ruby/version"
2
+ require "visualize_ruby/parser"
3
+ require "visualize_ruby/node"
4
+ require "visualize_ruby/edge"
5
+ require "visualize_ruby/graphviz"
6
+
7
+ module VisualizeRuby
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,36 @@
1
+ module VisualizeRuby
2
+ class Edge
3
+ attr_reader :name,
4
+ :node_a,
5
+ :node_b,
6
+ :dir
7
+ def initialize(name: nil, nodes:, dir: :forward)
8
+ @name = name
9
+ @node_a = nodes[0]
10
+ @node_b = nodes[1]
11
+ @dir = dir
12
+ end
13
+
14
+ def to_a
15
+ [
16
+ node_a.to_sym,
17
+ name,
18
+ direction_symbol,
19
+ node_b.to_sym,
20
+ ].compact
21
+ end
22
+
23
+ def direction_symbol
24
+ case dir
25
+ when :forward
26
+ "->"
27
+ end
28
+ end
29
+
30
+ def inspect
31
+ "#<VisualizeRuby::Edge #{to_a.join(" ")}>"
32
+ end
33
+
34
+ alias_method :to_s, :inspect
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ require "ruby-graphviz"
2
+
3
+ module VisualizeRuby
4
+ class Graphviz
5
+ attr_reader :nodes, :edges
6
+
7
+ def initialize(nodes, edges)
8
+ @nodes = nodes
9
+ @edges = edges
10
+ end
11
+
12
+ def to_graph(type: :digraph, **output)
13
+ g = GraphViz.new(:G, :type => type)
14
+ edges.each do |edge|
15
+ node_a = g.add_node(edge.node_a.name.to_s, shape: edge.node_a.shape)
16
+ node_b = g.add_node(edge.node_b.name.to_s, shape: edge.node_b.shape)
17
+ g.add_edges(node_a, node_b, label: edge.name.to_s, dir: edge.dir)
18
+ end
19
+
20
+ g.output(output)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ module VisualizeRuby
2
+ class Node
3
+ attr_reader :name
4
+ attr_accessor :type
5
+ def initialize(name:, type: :action)
6
+ @name = name
7
+ @type = type
8
+ end
9
+
10
+ def to_sym
11
+ name.to_s.gsub(" ", "_").to_sym
12
+ end
13
+
14
+ def to_a
15
+ [type, to_sym]
16
+ end
17
+
18
+ def type_display
19
+ case type
20
+ when :decision
21
+ "<>"
22
+ when :action
23
+ "[]"
24
+ end
25
+ end
26
+
27
+ def shape
28
+ case type
29
+ when :decision
30
+ :diamond
31
+ when :action
32
+ :ellipse
33
+ end
34
+ end
35
+
36
+ def inspect
37
+ "#<VisualizeRuby::Node #{type_display} #{name}>"
38
+ end
39
+
40
+ alias_method :to_s, :inspect
41
+ end
42
+ end
@@ -0,0 +1,154 @@
1
+ require "parser/current"
2
+
3
+ module VisualizeRuby
4
+ class Parser
5
+ attr_reader :ast
6
+
7
+ def initialize(ruby_code = nil, ast: ::Parser::CurrentRuby.parse(ruby_code))
8
+ @ast = ast
9
+ end
10
+
11
+ def parse
12
+ merge *Parser.const_get(ast.type.to_s.capitalize).new(ast).parse
13
+
14
+ return nodes, edges
15
+ end
16
+
17
+ class If
18
+ def initialize(ast)
19
+ @ast = ast
20
+ end
21
+
22
+ def parse
23
+ condition, on_true, on_false = @ast.children.to_a
24
+ nodes = []
25
+ edges = []
26
+ on_false_nodes, on_false_edges = Parser.new(ast: on_false).parse
27
+ on_false_node = on_false_nodes.first
28
+ condition_nodes, condition_edges = Parser.new(ast: condition).parse
29
+ condition_nodes.first.type = :decision
30
+ nodes.concat(condition_nodes)
31
+ edges.concat(condition_edges)
32
+ on_true_node = Node.new(name: AstHelper.new(on_true).description)
33
+ nodes << on_true_node
34
+ nodes.concat(on_false_nodes)
35
+
36
+ condition_nodes.each do |condition_node|
37
+ edges << Edge.new(name: "true", nodes: [condition_node, on_true_node])
38
+ edges << Edge.new(name: "false", nodes: [condition_node, on_false_node])
39
+ end
40
+ edges.concat(on_false_edges)
41
+ return [nodes, edges]
42
+ end
43
+ end
44
+
45
+ class Or
46
+ def initialize(ast)
47
+ @ast = ast
48
+ end
49
+
50
+ def parse
51
+ last_node = nil
52
+ edges = []
53
+ nodes = @ast.children.reverse.map do |c|
54
+ node = Node.new(name: AstHelper.new(c).description, type: :decision)
55
+ edges << Edge.new(name: "OR", nodes: [node, last_node]) if last_node
56
+ last_node = node
57
+ node
58
+ end.reverse
59
+ return nodes, edges
60
+ end
61
+ end
62
+
63
+ class And
64
+ def initialize(ast)
65
+ @ast = ast
66
+ end
67
+
68
+ def parse
69
+ last_node = nil
70
+ edges = []
71
+ nodes = @ast.children.reverse.map do |c|
72
+ node = Node.new(name: c.children.last, type: :decision)
73
+ edges << Edge.new(name: "AND", nodes: [node, last_node]) if last_node
74
+ last_node = node
75
+ node
76
+ end.reverse
77
+ return nodes, edges
78
+ end
79
+ end
80
+
81
+ class AstHelper
82
+ def initialize(ast)
83
+ @ast = ast
84
+ end
85
+
86
+ def description(ast: @ast)
87
+ case ast
88
+ when Symbol, String
89
+ ast
90
+ when NilClass
91
+ nil
92
+ else
93
+ ast.children.flat_map do |c|
94
+ description(ast: c)
95
+ end.reject do |c|
96
+ c.nil? || c == :""
97
+ end.join(" ")
98
+ end
99
+ end
100
+
101
+ end
102
+
103
+ class Begin
104
+ def initialize(ast)
105
+ @ast = ast
106
+ end
107
+
108
+ def parse
109
+ edges = []
110
+ last_node = nil
111
+ nodes = @ast.children.to_a.compact.reverse.map do |a|
112
+ node = Node.new(name: AstHelper.new(a).description, type: :action)
113
+ edges << Edge.new(nodes: [node, last_node]) if last_node
114
+ last_node = node
115
+ end
116
+
117
+ return nodes.reverse, edges.reverse
118
+ end
119
+ end
120
+
121
+ class Send
122
+ def initialize(ast)
123
+ @ast = ast
124
+ end
125
+
126
+ def parse
127
+ return [Node.new(name: AstHelper.new(@ast).description, type: :action)], []
128
+ end
129
+ end
130
+
131
+ class Str
132
+ def initialize(ast)
133
+ @ast = ast
134
+ end
135
+
136
+ def parse
137
+ return [Node.new(name: AstHelper.new(@ast).description, type: :action)], []
138
+ end
139
+ end
140
+
141
+ def nodes
142
+ @nodes ||= []
143
+ end
144
+
145
+ def edges
146
+ @edges ||= []
147
+ end
148
+
149
+ def merge(nodes, edges)
150
+ self.nodes.concat(nodes)
151
+ self.edges.concat(edges)
152
+ end
153
+ end
154
+ end