summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Mair <jrmair@gmail.com>2021-11-03 04:17:29 +0100
committerGitHub <noreply@github.com>2021-11-03 04:17:29 +0100
commite7ec1370b4c4283c2b6876977a0462a7634f7935 (patch)
treef2431f64adbbc21a369dc89c953a6f4d89b59aac /lib
parent836d7047ab5e7a1b2e7779e9a4b4fc2ba7f6cb1c (diff)
parent8e5a1520c80661c6997638b45e5050ff946d844d (diff)
downloadmethod_source-e7ec1370b4c4283c2b6876977a0462a7634f7935.tar.gz
Merge pull request #72 from stas/class_method_comment
Allow fetching class/module comments
Diffstat (limited to 'lib')
-rw-r--r--lib/method_source.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/method_source.rb b/lib/method_source.rb
index 7d16c3b..3c6086b 100644
--- a/lib/method_source.rb
+++ b/lib/method_source.rb
@@ -121,6 +121,37 @@ module MethodSource
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