Skip to content

Commit 1990b24

Browse files
committed
Handle defining access scope for existing methods
Fixes #78
1 parent 26be372 commit 1990b24

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

lib/ripper-tags/parser.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def on_command(name, *args)
7171
"has_one", "has_many",
7272
"belongs_to", "has_and_belongs_to_many",
7373
"scope", "named_scope",
74-
"public_class_method", "private_class_method",
74+
"public_class_method", "private_class_method", "protected_class_method",
7575
"public", "protected", "private",
7676
/^[mc]?attr_(accessor|reader|writer)$/
7777
on_method_add_arg([:fcall, name], args[0])
@@ -171,18 +171,22 @@ def on_method_add_arg(call, args)
171171
[:alias, args[1][0], args[2][0], line] if args[1] && args[2]
172172
when "define_method"
173173
[:def, args[1][0], line]
174-
when "public_class_method", "private_class_method", "private", "public", "protected"
174+
when "public_class_method", "private_class_method", "protected_class_method", "private", "public", "protected"
175175
access = name.sub("_class_method", "")
176+
klass = name == access ? nil : 'self'
177+
procedure = :def_with_access
176178

177-
if args[1][1] == 'self'
178-
klass = 'self'
179+
if args[1][0].is_a?(String)
180+
procedure = :redefine_access
181+
method_name = args[1][0]
182+
elsif args[1][1] == 'self'
179183
method_name = args[1][2]
180184
else
181185
klass = nil
182186
method_name = args[1][1]
183187
end
184188

185-
[:def_with_access, klass, method_name, access, line]
189+
[procedure, klass, method_name, access, line]
186190
when "scope", "named_scope"
187191
[:rails_def, :scope, args[1][0], line]
188192
when /^[mc]?attr_(accessor|reader|writer)$/
@@ -457,6 +461,14 @@ def on_def_with_access(klass, name, access, line)
457461
:access => access
458462
end
459463

464+
def on_redefine_access(klass, name, access, line)
465+
ns = (@namespace.empty? ? 'Object' : @namespace.join('::'))
466+
singleton = @is_singleton || klass == 'self'
467+
full_name = "#{ns}#{singleton ? '.' : '#'}#{name}"
468+
tag = @tags.select { |t| t[:full_name] == full_name }.last
469+
tag[:access] = access if tag
470+
end
471+
460472
def on_rails_def(kind, name, line)
461473
ns = (@namespace.empty?? 'Object' : @namespace.join('::'))
462474

test/test_ripper_tags.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,37 @@ def self.def() end
204204
end
205205
end
206206

207+
def test_extract_one_line_definition_access_by_symbol
208+
%w(private public protected).each do |visibility|
209+
tags = extract(<<-EOC)
210+
class Test
211+
def foo() end
212+
#{visibility} :foo
213+
end
214+
EOC
215+
216+
assert_equal 2, tags.size
217+
tag = tags.find { |t| t[:name] == "foo" }
218+
assert_equal visibility, tag[:access]
219+
assert_equal 2, tag[:line]
220+
end
221+
222+
%w(private_class_method public_class_method protected_class_method).each do |visibility|
223+
tags = extract(<<-EOC)
224+
class Test
225+
def self.bar() end
226+
#{visibility} :bar
227+
end
228+
EOC
229+
230+
assert_equal 2, tags.size
231+
tag = tags.find { |t| t[:name] == "bar" }
232+
scope = visibility.sub("_class_method", "")
233+
assert_equal scope, tag[:access]
234+
assert_equal 2, tag[:line]
235+
end
236+
end
237+
207238
def test_extract_module_eval
208239
tags = extract(<<-EOC)
209240
M.module_eval do

0 commit comments

Comments
 (0)