1
1
module Acronym
2
2
class Representation < SolutionRepresentation
3
3
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 )
16
30
end
17
31
18
32
def uses_scan?
@@ -32,11 +46,7 @@ def uses_scan?
32
46
} ,
33
47
]
34
48
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 )
40
50
end
41
51
42
52
def uses_split?
@@ -60,11 +70,7 @@ def uses_split?
60
70
}
61
71
]
62
72
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 )
68
74
end
69
75
70
76
private
@@ -78,6 +84,13 @@ def target_module
78
84
SA ::Helpers . extract_module_or_class ( root_node , "Acronym" )
79
85
end
80
86
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
+
81
94
def node_matches? ( node , matcher )
82
95
matcher .
83
96
map do |criteria |
0 commit comments