Skip to content

Commit 4521ff1

Browse files
committed
Improve method extraction
1 parent 0066e3a commit 4521ff1

File tree

12 files changed

+245
-83
lines changed

12 files changed

+245
-83
lines changed

lib/analyzer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
require 'active_support/inflector'
1414

1515
require_relative "generic/helpers"
16-
require_relative "generic/extract_class_method"
16+
require_relative "generic/extract_module_method"
1717
require_relative "generic/extract_instance_method"
1818
require_relative "generic/extract_module_or_class"
1919

lib/analyzers/two_fer/analyze.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def target_module
189189

190190
memoize
191191
def target_method
192-
SA::Helpers.extract_class_method(target_module, "two_fer")
192+
SA::Helpers.extract_module_method(target_module, "two_fer")
193193
end
194194

195195
memoize

lib/generic/extract_class_method.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/generic/extract_module_method.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module SA
2+
class ExtractModuleMethod < Parser::AST::Processor
3+
include Mandate
4+
5+
def initialize(node_to_search, name)
6+
@node_to_search = node_to_search
7+
@name = name.to_sym
8+
@found_method = nil
9+
@found_instance_method = nil
10+
11+
@module_function_called = false
12+
end
13+
14+
def call
15+
process(node_to_search)
16+
found_method
17+
end
18+
19+
def on_sclass(node)
20+
# If we're in a class << self context
21+
if node.children[0].self_type?
22+
@found_method = ExtractInstanceMethod.(node.children[1], name)
23+
end
24+
end
25+
26+
def on_def(node)
27+
return unless node.method_name == name
28+
if @module_function_called
29+
@found_method = node
30+
else
31+
@found_instance_method = node
32+
end
33+
end
34+
35+
def on_defs(node)
36+
# Return unless the name matches
37+
return unless node.method_name == name
38+
39+
# Is this defined as self (where there is no constant)
40+
# or on a specific module, and if so is this module the
41+
# correct one?
42+
defined_on = node.children[0].const_name
43+
return unless defined_on.nil? || defined_on == module_name
44+
45+
@found_method = node
46+
end
47+
48+
# We want to see if we have the syntax:
49+
# module_function
50+
# def foobar
51+
# end
52+
def on_send(node)
53+
return unless node.method_name == :module_function
54+
if node.arguments?
55+
return unless @found_instance_method
56+
57+
if node.arguments.map{|a|a.value}.include?(name)
58+
@found_method = @found_instance_method
59+
@found_instance_method = nil
60+
end
61+
else
62+
@module_function_called = true
63+
end
64+
end
65+
66+
private
67+
attr_reader :node_to_search, :name,
68+
:found_method,
69+
:found_instance_method, :module_function_called
70+
71+
memoize
72+
def module_name
73+
node_to_search.children[0].const_name
74+
end
75+
end
76+
end
77+

lib/generic/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ def self.extract_module_or_class(*args)
44
ExtractModuleOrClass.(*args)
55
end
66

7-
def self.extract_class_method(*args)
8-
ExtractClassMethod.(*args)
7+
def self.extract_module_method(*args)
8+
ExtractModuleMethod.(*args)
99
end
1010

1111
def self.extract_first_line_from_method(method)

test/fixtures/two-fer/102/two_fer.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
class TwoFer
1+
2+
module TwoFer
23
def self.two_fer(name = 'you')
34
"One for #{name}, one for me."
45
end
5-
end
6+
end
7+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"status": "approve_as_optimal",
3+
"comments": []
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"status": "approve_as_optimal",
3+
"comments": []
4+
}

test/fixtures/two-fer/108/two_fer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ class TwoFer
22
def TwoFer.two_fer(name='you')
33
"One for #{name}, one for me."
44
end
5-
end
5+
end

test/generic/extract_class_method_test.rb

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)