Skip to content

Commit

Permalink
Merge pull request #72 from stas/class_method_comment
Browse files Browse the repository at this point in the history
Allow fetching class/module comments
  • Loading branch information
banister committed Nov 3, 2021
2 parents 836d704 + 8e5a152 commit e7ec137
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Example: display method comments
# Merges the elements of the given enumerable object to the set and
# returns self.

Example: display module/class comments
--------------------------------------

MethodSource::MethodExtensions.method(:included).module_comment
# =>
# This module is to be included by `Method` and `UnboundMethod` and
# provides the `#source` functionality

Limitations:
------------

Expand Down
31 changes: 31 additions & 0 deletions lib/method_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ def source
def comment
MethodSource.comment_helper(source_location, defined?(name) ? name : inspect)
end

# Return the comments associated with the method class/module.
# @return [String] The method's comments as a string
# @raise SourceNotFoundException
#
# @example
# MethodSource::MethodExtensions.method(:included).module_comment
# =>
# # This module is to be included by `Method` and `UnboundMethod` and
# # provides the `#source` functionality
def class_comment
if self.respond_to?(:receiver)
class_inst_or_module = self.receiver
elsif self.respond_to?(:owner)
class_inst_or_module = self.owner
else
return comment
end

if class_inst_or_module.respond_to?(:name)
const_name = class_inst_or_module.name
else
const_name = class_inst_or_module.class.name
class_inst_or_module = class_inst_or_module.class
end

location = class_inst_or_module.const_source_location(const_name)

MethodSource.comment_helper(location, defined?(name) ? name : inspect)
end
alias module_comment class_comment
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/method_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
@hello_source = "def hello; :hello; end\n"
@hello_comment = "# A comment for hello\n# It spans two lines and is indented by 2 spaces\n"
@lambda_comment = "# This is a comment for MyLambda\n"
@module_comment = "# This is a comment for module\n"
@class_comment = "# This is a comment for class\n"
@lambda_source = "MyLambda = lambda { :lambda }\n"
@proc_source = "MyProc = Proc.new { :proc }\n"
@hello_instance_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
Expand Down Expand Up @@ -109,6 +111,18 @@
it 'should return comment for lambda' do
expect(MyLambda.comment).to eq(@lambda_comment)
end

it 'should return comment for module' do
expect(M.instance_method(:hello).module_comment).to eq(@module_comment)
end

it 'should return comment for class' do
expect(C.method(:hello).class_comment).to eq(@class_comment)
end

it 'should return comment for class instance' do
expect(C.new.method(:hello).class_comment).to eq(@class_comment)
end
end
# end
describe "Comment tests" do
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ def jruby?
end


# This is a comment for module
module M
def hello; :hello_module; end
end

# This is a comment for class
class C
include M
end

$o = Object.new
def $o.hello; :hello_singleton; end

Expand Down

0 comments on commit e7ec137

Please sign in to comment.