diff options
author | John Mair <jrmair@gmail.com> | 2021-11-03 04:17:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-03 04:17:29 +0100 |
commit | e7ec1370b4c4283c2b6876977a0462a7634f7935 (patch) | |
tree | f2431f64adbbc21a369dc89c953a6f4d89b59aac | |
parent | 836d7047ab5e7a1b2e7779e9a4b4fc2ba7f6cb1c (diff) | |
parent | 8e5a1520c80661c6997638b45e5050ff946d844d (diff) | |
download | method_source-e7ec1370b4c4283c2b6876977a0462a7634f7935.tar.gz |
Merge pull request #72 from stas/class_method_comment
Allow fetching class/module comments
-rw-r--r-- | README.markdown | 8 | ||||
-rw-r--r-- | lib/method_source.rb | 31 | ||||
-rw-r--r-- | spec/method_source_spec.rb | 14 | ||||
-rw-r--r-- | spec/spec_helper.rb | 6 |
4 files changed, 59 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index 24dfa45..ccaa690 100644 --- a/README.markdown +++ b/README.markdown @@ -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: ------------ 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 diff --git a/spec/method_source_spec.rb b/spec/method_source_spec.rb index c4e0669..1927670 100644 --- a/spec/method_source_spec.rb +++ b/spec/method_source_spec.rb @@ -30,6 +30,8 @@ describe MethodSource do @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" @@ -109,6 +111,18 @@ describe MethodSource do 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d011657..0cf9739 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 |