Skip to content

Commit f5c8c7a

Browse files
committed
Merge branch 'release/0.2.0'
2 parents 2f84f0c + cd258c5 commit f5c8c7a

25 files changed

Lines changed: 232 additions & 197 deletions

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
7979

8080
## Contributing
8181

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).
8483

8584
## License
8685

Rakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
33

44
RSpec::Core::RakeTask.new(:spec)
55

6-
task :default => :spec
6+
task default: :spec

bin/console

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
require "code_breaker"
3+
require 'bundler/setup'
4+
require 'code_breaker'
55

66
# You can add fixtures and/or initialization code here to make experimenting
77
# with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "code_breaker"
1010
# require "pry"
1111
# Pry.start
1212

13-
require "irb"
13+
require 'irb'
1414
IRB.start

code_breaker.gemspec

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44
require 'code_breaker/version'
55

66
Gem::Specification.new do |spec|
7-
spec.name = "code_breaker"
7+
spec.name = 'code_breaker'
88
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']
1111

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'
1616

1717
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
1818
# delete this section to allow pushing this gem to any host.
1919
if spec.respond_to?(:metadata)
20-
spec.metadata['allowed_push_host'] = "https://rubygems.org"
20+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
2121
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.'
2323
end
2424

2525
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26-
spec.bindir = "exe"
26+
spec.bindir = 'exe'
2727
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28-
spec.require_paths = ["lib"]
28+
spec.require_paths = ['lib']
2929

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'
3232

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'
3636
end

lib/code_breaker.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'code_breaker/parser'
33

44
module CodeBreaker
5-
65
def self.parse(code)
76
CodeBreaker::Parser.new(code).run
87
end

lib/code_breaker/parsable/assignments.rb

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,35 @@
33
module CodeBreaker
44
module Parsable
55
module Assignments
6-
76
extend ActiveSupport::Concern
87
include Parsable::Node
98

109
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
1830

1931
# multiple assignment
2032
def parse_masgn_node(node)
2133
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)
2835

2936
{ node.type => { lhs => values } }
3037
end
@@ -36,15 +43,23 @@ def parse_mlhs_node(node)
3643

3744
# constant assignment
3845
def parse_casgn_node(node)
39-
name = node.children[1]
46+
name = node.children[1]
4047
children = node.children[2]
41-
value = children.nil? ? name : [name, parse(node.children[2])]
48+
value = children.nil? ? name : [name, parse(node.children[2])]
4249

4350
{ node.type => value }
4451
end
4552

46-
end
53+
private
4754

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
4863
end
4964
end
5065
end

lib/code_breaker/parsable/data_types.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
module CodeBreaker
44
module Parsable
55
module DataTypes
6-
76
extend ActiveSupport::Concern
87
include Parsable::Node
98

@@ -29,6 +28,19 @@ def parse_xstr_node(node)
2928
{ node.type => parse_children(node).first }
3029
end
3130

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+
3244
def parse_sym_node(node)
3345
Symbol
3446
end
@@ -49,8 +61,8 @@ def parse_pair_node(node)
4961
{ parse(node.children[0]) => parse(node.children[1]) }
5062
end
5163

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
5466
end
5567
end
5668
end

lib/code_breaker/parsable/keywords.rb

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@
33
module CodeBreaker
44
module Parsable
55
module Keywords
6-
76
extend ActiveSupport::Concern
87
include Parsable::Node
98

109
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
2221

2322
def parse_loop_node(node)
2423
condition = node.children[0]
25-
body = node.children[1]
24+
body = node.children[1]
2625

2726
{ node.type => parse(condition), do: parse(body) }
2827
end
2928

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
3231

3332
def parse_for_node(node)
3433
variable = node.children[0]
@@ -40,7 +39,7 @@ def parse_for_node(node)
4039

4140
def parse_if_node(node)
4241
condition = node.children[0]
43-
if_body = node.children[1]
42+
if_body = node.children[1]
4443
else_body = node.children[2]
4544

4645
clause = { node.type => parse(condition), then: parse(if_body) }
@@ -50,16 +49,16 @@ def parse_if_node(node)
5049
end
5150

5251
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])
5554
value = body ? [name, body] : [name]
5655

5756
{ node.type => value }
5857
end
5958

6059
def parse_return_node(node)
6160
children = parse_children(node)
62-
values = children.length == 1 ? children[0] : children
61+
values = children.length == 1 ? children[0] : children
6362

6463
{ node.type => values }
6564
end
@@ -80,9 +79,9 @@ def parse_kwbegin_node(node)
8079
end
8180

8281
def parse_case_node(node)
83-
case_part = parse(node.children.first)
82+
case_part = parse(node.children.first)
8483
when_parts = node.children[1...-1].map { |child| parse(child) }
85-
else_part = parse(node.children.last)
84+
else_part = parse(node.children.last)
8685

8786
statement = { case: when_parts.unshift(case_part) }
8887
statement[:case] << { else: else_part } if else_part
@@ -96,7 +95,6 @@ def parse_when_node(node)
9695
{ when: when_part, then: then_part }
9796
end
9897
end
99-
10098
end
10199
end
102100
end

lib/code_breaker/parsable/language_elements.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,39 @@
33
module CodeBreaker
44
module Parsable
55
module LanguageElements
6-
76
extend ActiveSupport::Concern
87
include Parsable::Node
98

109
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
2027

2128
def parse_block_pass_node(node)
2229
{ node.type => node.children.first.children.last }
2330
end
2431

2532
def parse_splat_node(node)
2633
children = parse_children(node).flatten(1)
27-
values = children.length == 1 ? children[0] : children
34+
values = children.length == 1 ? children[0] : children
2835

29-
{ node.type => values }
36+
{ node.type => values }
3037
end
3138
end
3239
end
3340
end
34-
end
41+
end

0 commit comments

Comments
 (0)