Skip to content

Commit c3bf34b

Browse files
committed
Refactor method chain matcher to use matcher syntax
1 parent 98c1503 commit c3bf34b

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

lib/analyzers/acronym/representation.rb

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
module Acronym
22
class Representation < SolutionRepresentation
33
def uses_method_chain?
4-
target_method.body == s(:send,
5-
s(:send,
6-
s(:send,
7-
s(:send,
8-
s(:send, s(:lvar, :words), :tr, s(:str, "-"), s(:str, " ")),
9-
:split
10-
),
11-
:map,
12-
s(:block_pass, s(:sym, :chr))
13-
),
14-
:join),
15-
:upcase)
4+
matchers = [
5+
{
6+
method_name: :upcase,
7+
},
8+
{
9+
method_name: :join,
10+
chained?: true
11+
},
12+
{
13+
method_name: :map,
14+
arguments: [{ to_ast: s(:block_pass, s(:sym, :chr)) }],
15+
chained?: true
16+
},
17+
{
18+
method_name: :split,
19+
chained?: true
20+
},
21+
{
22+
method_name: :tr,
23+
receiver: s(:lvar, :words),
24+
chained?: true,
25+
arguments: [{ to_ast: s(:str, "-") }, { to_ast: s(:str, " ") }]
26+
}
27+
]
28+
29+
matches?(target_method.body, matchers)
1630
end
1731

1832
def uses_scan?
@@ -32,11 +46,7 @@ def uses_scan?
3246
},
3347
]
3448

35-
target_method.
36-
body.
37-
each_node(:send).
38-
with_index.
39-
all? { |node, i| node_matches?(node, matchers[i]) }
49+
matches?(target_method.body, matchers)
4050
end
4151

4252
def uses_split?
@@ -60,11 +70,7 @@ def uses_split?
6070
}
6171
]
6272

63-
target_method.
64-
body.
65-
each_node(:send).
66-
with_index.
67-
all? { |node, i| node_matches?(node, matchers[i]) }
73+
matches?(target_method.body, matchers)
6874
end
6975

7076
private
@@ -78,6 +84,13 @@ def target_module
7884
SA::Helpers.extract_module_or_class(root_node, "Acronym")
7985
end
8086

87+
def matches?(body, matchers)
88+
body.
89+
each_node(:send).
90+
with_index.
91+
all? { |node, i| node_matches?(node, matchers[i]) }
92+
end
93+
8194
def node_matches?(node, matcher)
8295
matcher.
8396
map do |criteria|

0 commit comments

Comments
 (0)