code_breaker 0.1.2 → 0.2.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: 42be42ca56f164dbac7f2412dc763d943b45ef08
4
- data.tar.gz: 393e5361ed2095c5e74cb36fe94c162e4d68440b
3
+ metadata.gz: 4468ff2521266bb40fef341f1bccccae616ec424
4
+ data.tar.gz: 42237cf2a40ab1a6070deb8518d251cf843c16e7
5
5
  SHA512:
6
- metadata.gz: 3f878d309e9c8819b533612d59fac0b55ebb0e39e1ba739f7aeba4b9433a8ab6debb3c476f5940e19b98216cd652d9067941faf037acf391e4ce19e3b957b18b
7
- data.tar.gz: fa92f5c0fba4ab799a5ce56973aa5997c7cbed8de2a2eb963cd815d0453dfcdf1a241a991d32729eb58ab0546600631eb2b93cb7808ecccbe42d30d8c213e6fd
6
+ metadata.gz: 8eb015b2710c9ec7ac78aa24a6220231131486a9010c4526731e4ce4077249cdda0b347369f27dca219439f5eb0ff7184fd4894df3b07b817017c41bc22868b4
7
+ data.tar.gz: c7bb344918f3b9c04c46ca7b0a86d4a04526b98e251c4087161dc1d71af3e932445f89931bcdf514f287f4851d16ec094db41e330d293a33ebd778040e5ab6c5
data/README.md CHANGED
@@ -79,8 +79,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
79
79
 
80
80
  ## Contributing
81
81
 
82
- Bug reports and pull requests are welcome on GitHub at https://github.com/daigaku-ruby/code_breaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
83
-
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/daigaku-ruby/code_breaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant code of conduct](http://contributor-covenant.org/version/1/2/0).
84
83
 
85
84
  ## License
86
85
 
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "code_breaker"
3
+ require 'bundler/setup'
4
+ require 'code_breaker'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "code_breaker"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/code_breaker.gemspec CHANGED
@@ -4,33 +4,33 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'code_breaker/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "code_breaker"
7
+ spec.name = 'code_breaker'
8
8
  spec.version = CodeBreaker::VERSION
9
- spec.authors = ["Paul Götze"]
10
- spec.email = ["paul.christoph.goetze@gmail.com"]
9
+ spec.authors = ['Paul Götze']
10
+ spec.email = ['paul.christoph.goetze@gmail.com']
11
11
 
12
- spec.summary = %q{Breaking a Ruby code snippet into a sequence of classes and their connecting methods.}
13
- spec.description = %q{Breaking a Ruby code snippet into a sequence of classes and their connecting methods.}
14
- spec.homepage = "https://github.com/daigaku-ruby/code_breaker"
15
- spec.license = "MIT"
12
+ spec.summary = 'Breaking a Ruby code snippet into a sequence of classes and their connecting methods.'
13
+ spec.description = 'Breaking a Ruby code snippet into a sequence of classes and their connecting methods.'
14
+ spec.homepage = 'https://github.com/daigaku-ruby/code_breaker'
15
+ spec.license = 'MIT'
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
18
  # delete this section to allow pushing this gem to any host.
19
19
  if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
23
  end
24
24
 
25
25
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "exe"
26
+ spec.bindir = 'exe'
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
28
+ spec.require_paths = ['lib']
29
29
 
30
- spec.add_dependency "parser", "~> 2.2"
31
- spec.add_dependency "activesupport", "~> 4"
30
+ spec.add_dependency 'parser', '~> 2.2'
31
+ spec.add_dependency 'activesupport', '~> 4'
32
32
 
33
- spec.add_development_dependency "bundler", "~> 1.10"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "rspec", " ~> 3.2"
33
+ spec.add_development_dependency 'bundler', '~> 1.10'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.2'
36
36
  end
data/lib/code_breaker.rb CHANGED
@@ -2,7 +2,6 @@ require 'code_breaker/version'
2
2
  require 'code_breaker/parser'
3
3
 
4
4
  module CodeBreaker
5
-
6
5
  def self.parse(code)
7
6
  CodeBreaker::Parser.new(code).run
8
7
  end
@@ -3,28 +3,35 @@ require 'active_support/concern'
3
3
  module CodeBreaker
4
4
  module Parsable
5
5
  module Assignments
6
-
7
6
  extend ActiveSupport::Concern
8
7
  include Parsable::Node
9
8
 
10
9
  included do
11
- alias :parse_lvasgn_node :parse_as_hash # local variable assignment
12
- alias :parse_ivasgn_node :parse_as_hash # instance variable assignment
13
- alias :parse_cvasgn_node :parse_as_hash # class variable assignment
14
- alias :parse_gvasgn_node :parse_as_hash # global variable assignment
15
- alias :parse_op_asgn_node :parse_as_hash # operation assignment
16
- alias :parse_or_asgn_node :parse_as_hash # or assignment
17
- alias :parse_and_asgn_node :parse_as_hash # and assignment
10
+ # local variable assignment
11
+ alias_method :parse_lvasgn_node, :parse_as_hash
12
+
13
+ # instance variable assignment
14
+ alias_method :parse_ivasgn_node, :parse_as_hash
15
+
16
+ # class variable assignment
17
+ alias_method :parse_cvasgn_node, :parse_as_hash
18
+
19
+ # global variable assignment
20
+ alias_method :parse_gvasgn_node, :parse_as_hash
21
+
22
+ # operation assignment
23
+ alias_method :parse_op_asgn_node, :parse_as_hash
24
+
25
+ # or assignment
26
+ alias_method :parse_or_asgn_node, :parse_as_hash
27
+
28
+ # and assignment
29
+ alias_method :parse_and_asgn_node, :parse_as_hash
18
30
 
19
31
  # multiple assignment
20
32
  def parse_masgn_node(node)
21
33
  lhs, rhs = parse_children(node)
22
-
23
- values = if rhs.kind_of?(Hash) && rhs.has_key?(:array)
24
- rhs[:array]
25
- else
26
- [rhs] + (1...lhs.count).map { NilClass }
27
- end
34
+ values = multiple_assignment_values(lhs, rhs)
28
35
 
29
36
  { node.type => { lhs => values } }
30
37
  end
@@ -36,15 +43,23 @@ module CodeBreaker
36
43
 
37
44
  # constant assignment
38
45
  def parse_casgn_node(node)
39
- name = node.children[1]
46
+ name = node.children[1]
40
47
  children = node.children[2]
41
- value = children.nil? ? name : [name, parse(node.children[2])]
48
+ value = children.nil? ? name : [name, parse(node.children[2])]
42
49
 
43
50
  { node.type => value }
44
51
  end
45
52
 
46
- end
53
+ private
47
54
 
55
+ def multiple_assignment_values(lhs, rhs)
56
+ if rhs.is_a?(Hash) && rhs.key?(:array)
57
+ rhs[:array]
58
+ else
59
+ [rhs] + (1...lhs.count).map { NilClass }
60
+ end
61
+ end
62
+ end
48
63
  end
49
64
  end
50
65
  end
@@ -3,7 +3,6 @@ require 'active_support/concern'
3
3
  module CodeBreaker
4
4
  module Parsable
5
5
  module DataTypes
6
-
7
6
  extend ActiveSupport::Concern
8
7
  include Parsable::Node
9
8
 
@@ -29,6 +28,19 @@ module CodeBreaker
29
28
  { node.type => parse_children(node).first }
30
29
  end
31
30
 
31
+ # interpolated string
32
+ def parse_dstr_node(node)
33
+ values = parse_as_hash(node)[node.type].map do |value|
34
+ if value.is_a?(Array)
35
+ value.flatten(1)
36
+ else
37
+ value
38
+ end
39
+ end
40
+
41
+ { node.type => values }
42
+ end
43
+
32
44
  def parse_sym_node(node)
33
45
  Symbol
34
46
  end
@@ -49,8 +61,8 @@ module CodeBreaker
49
61
  { parse(node.children[0]) => parse(node.children[1]) }
50
62
  end
51
63
 
52
- alias :parse_hash_node :parse_as_hash
53
- alias :parse_array_node :parse_as_hash
64
+ alias_method :parse_hash_node, :parse_as_hash
65
+ alias_method :parse_array_node, :parse_as_hash
54
66
  end
55
67
  end
56
68
  end
@@ -3,32 +3,31 @@ require 'active_support/concern'
3
3
  module CodeBreaker
4
4
  module Parsable
5
5
  module Keywords
6
-
7
6
  extend ActiveSupport::Concern
8
7
  include Parsable::Node
9
8
 
10
9
  included do
11
- alias :parse_or_node :parse_as_hash
12
- alias :parse_and_node :parse_as_hash
13
- alias :parse_def_node :parse_as_hash
14
- alias :parse_yield_node :parse_as_hash
15
- alias :parse_rescue_node :parse_as_hash
16
- alias :parse_resbody_node :parse_as_hash
17
-
18
- alias :parse_break_node :parse_as_node_type
19
- alias :parse_next_node :parse_as_node_type
20
- alias :parse_retry_node :parse_as_node_type
21
- alias :parse_self_node :parse_as_node_type
10
+ alias_method :parse_or_node, :parse_as_hash
11
+ alias_method :parse_and_node, :parse_as_hash
12
+ alias_method :parse_def_node, :parse_as_hash
13
+ alias_method :parse_yield_node, :parse_as_hash
14
+ alias_method :parse_rescue_node, :parse_as_hash
15
+ alias_method :parse_resbody_node, :parse_as_hash
16
+
17
+ alias_method :parse_break_node, :parse_as_node_type
18
+ alias_method :parse_next_node, :parse_as_node_type
19
+ alias_method :parse_retry_node, :parse_as_node_type
20
+ alias_method :parse_self_node, :parse_as_node_type
22
21
 
23
22
  def parse_loop_node(node)
24
23
  condition = node.children[0]
25
- body = node.children[1]
24
+ body = node.children[1]
26
25
 
27
26
  { node.type => parse(condition), do: parse(body) }
28
27
  end
29
28
 
30
- alias :parse_while_node :parse_loop_node
31
- alias :parse_until_node :parse_loop_node
29
+ alias_method :parse_while_node, :parse_loop_node
30
+ alias_method :parse_until_node, :parse_loop_node
32
31
 
33
32
  def parse_for_node(node)
34
33
  variable = node.children[0]
@@ -40,7 +39,7 @@ module CodeBreaker
40
39
 
41
40
  def parse_if_node(node)
42
41
  condition = node.children[0]
43
- if_body = node.children[1]
42
+ if_body = node.children[1]
44
43
  else_body = node.children[2]
45
44
 
46
45
  clause = { node.type => parse(condition), then: parse(if_body) }
@@ -50,8 +49,8 @@ module CodeBreaker
50
49
  end
51
50
 
52
51
  def parse_module_node(node)
53
- name = parse(node.children[0])
54
- body = node.children[1].nil? ? nil : parse(node.children[1])
52
+ name = parse(node.children[0])
53
+ body = node.children[1].nil? ? nil : parse(node.children[1])
55
54
  value = body ? [name, body] : [name]
56
55
 
57
56
  { node.type => value }
@@ -59,7 +58,7 @@ module CodeBreaker
59
58
 
60
59
  def parse_return_node(node)
61
60
  children = parse_children(node)
62
- values = children.length == 1 ? children[0] : children
61
+ values = children.length == 1 ? children[0] : children
63
62
 
64
63
  { node.type => values }
65
64
  end
@@ -80,9 +79,9 @@ module CodeBreaker
80
79
  end
81
80
 
82
81
  def parse_case_node(node)
83
- case_part = parse(node.children.first)
82
+ case_part = parse(node.children.first)
84
83
  when_parts = node.children[1...-1].map { |child| parse(child) }
85
- else_part = parse(node.children.last)
84
+ else_part = parse(node.children.last)
86
85
 
87
86
  statement = { case: when_parts.unshift(case_part) }
88
87
  statement[:case] << { else: else_part } if else_part
@@ -96,7 +95,6 @@ module CodeBreaker
96
95
  { when: when_part, then: then_part }
97
96
  end
98
97
  end
99
-
100
98
  end
101
99
  end
102
100
  end
@@ -3,20 +3,27 @@ require 'active_support/concern'
3
3
  module CodeBreaker
4
4
  module Parsable
5
5
  module LanguageElements
6
-
7
6
  extend ActiveSupport::Concern
8
7
  include Parsable::Node
9
8
 
10
9
  included do
11
- alias :parse_block_node :parse_as_hash
12
- alias :parse_args_node :parse_as_hash
13
- alias :parse_arg_node :parse_as_last_child_hash
14
- alias :parse_blockarg_node :parse_as_last_child_hash
15
- alias :parse_restarg_node :parse_as_last_child_hash
16
- alias :parse_optarg_node :parse_as_hash # optional argument
17
- alias :parse_kwarg_node :parse_as_last_child_hash # keyword argument
18
- alias :parse_kwoptarg_node :parse_as_hash # optional keyword argument
19
- alias :parse_kwrestarg_node :parse_as_last_child_hash # keyword rest argument
10
+ alias_method :parse_block_node, :parse_as_hash
11
+ alias_method :parse_args_node, :parse_as_hash
12
+ alias_method :parse_arg_node, :parse_as_last_child_hash
13
+ alias_method :parse_blockarg_node, :parse_as_last_child_hash
14
+ alias_method :parse_restarg_node, :parse_as_last_child_hash
15
+
16
+ # optional argument
17
+ alias_method :parse_optarg_node, :parse_as_hash
18
+
19
+ # keyword argument
20
+ alias_method :parse_kwarg_node, :parse_as_last_child_hash
21
+
22
+ # optional keyword argument
23
+ alias_method :parse_kwoptarg_node, :parse_as_hash
24
+
25
+ # keyword rest argument
26
+ alias_method :parse_kwrestarg_node, :parse_as_last_child_hash
20
27
 
21
28
  def parse_block_pass_node(node)
22
29
  { node.type => node.children.first.children.last }
@@ -24,11 +31,11 @@ module CodeBreaker
24
31
 
25
32
  def parse_splat_node(node)
26
33
  children = parse_children(node).flatten(1)
27
- values = children.length == 1 ? children[0] : children
34
+ values = children.length == 1 ? children[0] : children
28
35
 
29
- { node.type => values }
36
+ { node.type => values }
30
37
  end
31
38
  end
32
39
  end
33
40
  end
34
- end
41
+ end
@@ -1,11 +1,10 @@
1
1
  module CodeBreaker
2
2
  module Parsable
3
3
  module Node
4
-
5
4
  def parse(node)
6
5
  return if node.nil?
7
6
 
8
- if node.kind_of?(Symbol)
7
+ if node.is_a?(Symbol)
9
8
  node
10
9
  else
11
10
  send("parse_#{node.type}_node", node)
@@ -13,7 +12,7 @@ module CodeBreaker
13
12
  end
14
13
 
15
14
  def parse_children(node)
16
- node.children.reduce([]) do |nodes, child|
15
+ node.children.each_with_object([]) do |child, nodes|
17
16
  nodes << parse(child) unless child.nil?
18
17
  nodes
19
18
  end
@@ -37,16 +36,18 @@ module CodeBreaker
37
36
  if node_type.empty?
38
37
  super
39
38
  else
40
- message = [
41
- "Breaking the node type \"#{node_type}\" is not yet implemented.",
42
- "You can open an issue on this in the project's Github repo under:",
43
- "https://github.com/daigaku-ruby/code_breaker/issues/new\n"
44
- ].join("\n")
45
-
46
- raise NotImplementedError, message
39
+ raise NotImplementedError, not_implemented_message(node_type)
47
40
  end
48
41
  end
49
42
 
43
+ def not_implemented_message(node_type)
44
+ [
45
+ %(Breaking the node type "#{node_type}" is not yet implemented.),
46
+ 'You can open an issue on this in the project’s Github repo under:',
47
+ 'https://github.com/daigaku-ruby/code_breaker/issues/new',
48
+ ''
49
+ ].join("\n")
50
+ end
50
51
  end
51
52
  end
52
53
  end
@@ -3,13 +3,15 @@ require 'active_support/concern'
3
3
  module CodeBreaker
4
4
  module Parsable
5
5
  module Ranges
6
-
7
6
  extend ActiveSupport::Concern
8
7
  include Parsable::Node
9
8
 
10
9
  included do
11
- alias :parse_irange_node :parse_as_hash # inclusive range a..b
12
- alias :parse_erange_node :parse_as_hash # exclusive range a...b
10
+ # inclusive range a..b
11
+ alias_method :parse_irange_node, :parse_as_hash
12
+
13
+ # exclusive range a...b
14
+ alias_method :parse_erange_node, :parse_as_hash
13
15
  end
14
16
  end
15
17
  end
@@ -3,16 +3,23 @@ require 'active_support/concern'
3
3
  module CodeBreaker
4
4
  module Parsable
5
5
  module VariableTypes
6
-
7
6
  extend ActiveSupport::Concern
8
7
  include Parsable::Node
9
8
 
10
9
  included do
11
- alias :parse_const_node :parse_as_last_child_hash
12
- alias :parse_lvar_node :parse_as_last_child_hash # local variable
13
- alias :parse_ivar_node :parse_as_last_child_hash # instance variable
14
- alias :parse_cvar_node :parse_as_last_child_hash # class variable
15
- alias :parse_gvar_node :parse_as_last_child_hash # global variable
10
+ alias_method :parse_const_node, :parse_as_last_child_hash
11
+
12
+ # local variable
13
+ alias_method :parse_lvar_node, :parse_as_last_child_hash
14
+
15
+ # instance variable
16
+ alias_method :parse_ivar_node, :parse_as_last_child_hash
17
+
18
+ # class variable
19
+ alias_method :parse_cvar_node, :parse_as_last_child_hash
20
+
21
+ # global variable
22
+ alias_method :parse_gvar_node, :parse_as_last_child_hash
16
23
  end
17
24
  end
18
25
  end
@@ -3,7 +3,6 @@ require 'active_support/concern'
3
3
  module CodeBreaker
4
4
  module Parsable
5
5
  module Wrappers
6
-
7
6
  extend ActiveSupport::Concern
8
7
  include Parsable::Node
9
8
 
@@ -16,7 +15,7 @@ module CodeBreaker
16
15
  parse_children(node).flatten(1)
17
16
  end
18
17
 
19
- alias :parse_begin_node :parse_children
18
+ alias_method :parse_begin_node, :parse_children
20
19
  end
21
20
  end
22
21
  end
@@ -27,6 +27,6 @@ module CodeBreaker
27
27
  @output
28
28
  end
29
29
 
30
- alias :output :run
30
+ alias output run
31
31
  end
32
32
  end
@@ -1,3 +1,3 @@
1
1
  module CodeBreaker
2
- VERSION = "0.1.2"
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_breaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Götze
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-04 00:00:00.000000000 Z
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  requirements: []
134
134
  rubyforge_project:
135
- rubygems_version: 2.4.6
135
+ rubygems_version: 2.6.4
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: Breaking a Ruby code snippet into a sequence of classes and their connecting