Skip to content

Commit e7ec137

Browse files
authored
Merge pull request #72 from stas/class_method_comment
Allow fetching class/module comments
2 parents 836d704 + 8e5a152 commit e7ec137

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ Example: display method comments
5050
# Merges the elements of the given enumerable object to the set and
5151
# returns self.
5252

53+
Example: display module/class comments
54+
--------------------------------------
55+
56+
MethodSource::MethodExtensions.method(:included).module_comment
57+
# =>
58+
# This module is to be included by `Method` and `UnboundMethod` and
59+
# provides the `#source` functionality
60+
5361
Limitations:
5462
------------
5563

lib/method_source.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ def source
121121
def comment
122122
MethodSource.comment_helper(source_location, defined?(name) ? name : inspect)
123123
end
124+
125+
# Return the comments associated with the method class/module.
126+
# @return [String] The method's comments as a string
127+
# @raise SourceNotFoundException
128+
#
129+
# @example
130+
# MethodSource::MethodExtensions.method(:included).module_comment
131+
# =>
132+
# # This module is to be included by `Method` and `UnboundMethod` and
133+
# # provides the `#source` functionality
134+
def class_comment
135+
if self.respond_to?(:receiver)
136+
class_inst_or_module = self.receiver
137+
elsif self.respond_to?(:owner)
138+
class_inst_or_module = self.owner
139+
else
140+
return comment
141+
end
142+
143+
if class_inst_or_module.respond_to?(:name)
144+
const_name = class_inst_or_module.name
145+
else
146+
const_name = class_inst_or_module.class.name
147+
class_inst_or_module = class_inst_or_module.class
148+
end
149+
150+
location = class_inst_or_module.const_source_location(const_name)
151+
152+
MethodSource.comment_helper(location, defined?(name) ? name : inspect)
153+
end
154+
alias module_comment class_comment
124155
end
125156
end
126157

spec/method_source_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
@hello_source = "def hello; :hello; end\n"
3131
@hello_comment = "# A comment for hello\n# It spans two lines and is indented by 2 spaces\n"
3232
@lambda_comment = "# This is a comment for MyLambda\n"
33+
@module_comment = "# This is a comment for module\n"
34+
@class_comment = "# This is a comment for class\n"
3335
@lambda_source = "MyLambda = lambda { :lambda }\n"
3436
@proc_source = "MyProc = Proc.new { :proc }\n"
3537
@hello_instance_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
@@ -109,6 +111,18 @@
109111
it 'should return comment for lambda' do
110112
expect(MyLambda.comment).to eq(@lambda_comment)
111113
end
114+
115+
it 'should return comment for module' do
116+
expect(M.instance_method(:hello).module_comment).to eq(@module_comment)
117+
end
118+
119+
it 'should return comment for class' do
120+
expect(C.method(:hello).class_comment).to eq(@class_comment)
121+
end
122+
123+
it 'should return comment for class instance' do
124+
expect(C.new.method(:hello).class_comment).to eq(@class_comment)
125+
end
112126
end
113127
# end
114128
describe "Comment tests" do

spec/spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ def jruby?
1010
end
1111

1212

13+
# This is a comment for module
1314
module M
1415
def hello; :hello_module; end
1516
end
1617

18+
# This is a comment for class
19+
class C
20+
include M
21+
end
22+
1723
$o = Object.new
1824
def $o.hello; :hello_singleton; end
1925

0 commit comments

Comments
 (0)