diff options
author | Adam Sanderson <netghost@gmail.com> | 2010-12-17 10:55:16 -0800 |
---|---|---|
committer | John Mair <jrmair@gmail.com> | 2010-12-18 19:28:42 +1300 |
commit | e61e36e4f937b18e258fe0f8f2733528d132cc5c (patch) | |
tree | 5a6eb4d0e9ab2d570ac4d23200059ce80c889cff /lib/method_source.rb | |
parent | 32e6e8495c36782e6362676a32d7a7e808226437 (diff) | |
download | method_source-e61e36e4f937b18e258fe0f8f2733528d132cc5c.tar.gz |
version 0.2.0, support for method comments (and a few minor changes), thanks to Adam Sanderson
* Merged in Adam's changes
* Refactored source_helper to reflect comment_helper style
* Made some adjustments to Adam's code: removed buffer.strip, now doing an lstrip on each line before adding to buffer
* No longer including blank lines in comment buffer (blank lines ignored)
* Added large number of comment-related tests
Diffstat (limited to 'lib/method_source.rb')
-rw-r--r-- | lib/method_source.rb | 78 |
1 files changed, 61 insertions, 17 deletions
diff --git a/lib/method_source.rb b/lib/method_source.rb index 57397c7..ecb546e 100644 --- a/lib/method_source.rb +++ b/lib/method_source.rb @@ -20,9 +20,8 @@ module MethodSource !!Ripper::SexpBuilder.new(code).parse end - # Helper method responsible for opening source file and advancing to - # the correct linenumber. Defined here to avoid polluting `Method` - # class. + # Helper method responsible for extracting method body. + # Defined here to avoid polluting `Method` class. # @param [Array] source_location The array returned by Method#source_location # @return [File] The opened source file def self.source_helper(source_location) @@ -31,7 +30,44 @@ module MethodSource file_name, line = source_location file = File.open(file_name) (line - 1).times { file.readline } - file + + code = "" + loop do + val = file.readline + code << val + + return code if MethodSource.valid_expression?(code) + end + + ensure + file.close if file + end + + # Helper method responsible for opening source file and buffering up + # the comments for a specified method. Defined here to avoid polluting + # `Method` class. + # @param [Array] source_location The array returned by Method#source_location + # @return [String] The comments up to the point of the method. + def self.comment_helper(source_location) + return nil if !source_location.is_a?(Array) + + file_name, line = source_location + file = File.open(file_name) + buffer = "" + (line - 1).times do + line = file.readline + # Add any line that is a valid ruby comment, + # but clear as soon as we hit a non comment line. + if (line =~ /^\s*#/) || (line =~ /^\s*$/) + buffer << line.lstrip + else + buffer.clear + end + end + + buffer + ensure + file.close if file end # This module is to be included by `Method` and `UnboundMethod` and @@ -49,26 +85,34 @@ module MethodSource # self # end def source - file = nil - if respond_to?(:source_location) - file = MethodSource.source_helper(source_location) + source = MethodSource.source_helper(source_location) - raise "Cannot locate source for this method: #{name}" if !file + raise "Cannot locate source for this method: #{name}" if !source else raise "Method#source not supported by this Ruby version (#{RUBY_VERSION})" end - - code = "" - loop do - val = file.readline - code += val + + source + end + + # Return the comments associated with the method as a string. + # (This functionality is only supported in Ruby 1.9 and above) + # @return [String] The method's comments as a string + # @example + # Set.instance_method(:clear).comment.display + # => + # # Removes all elements and returns self. + def comment + if respond_to?(:source_location) + comment = MethodSource.comment_helper(source_location) - return code if MethodSource.valid_expression?(code) + raise "Cannot locate source for this method: #{name}" if !comment + else + raise "Method#comment not supported by this Ruby version (#{RUBY_VERSION})" end - - ensure - file.close if file + + comment end end end |