module MethodIntrospection
#¶ ↑
#¶ ↑
Constants
- VERSION
#¶ ↑
VERSION
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
MethodIntrospection.comment_helper
¶ ↑
This is a helper method responsible for opening source file and buffering up the comments for a specified method. It is defined here to avoid polluting the `Method` class.
The expected input should be an Array, which is returned by the method Method#source_location.
This will typically be in the form of:
["/Programs/Ruby/2.2.3/lib/ruby/site_ruby/2.2.0/config.rb", 297]
The first one yields to us the name of the file in question, and the second gives us the specific line number.
@param [String] method_name @return [String] The comments up to the point of the method.
#¶ ↑
# File lib/method_introspection/module_methods.rb, line 137 def self.comment_helper(source_location, name = nil) raise_this(name) unless source_location this_file, line_number = *source_location MethodIntrospection.set_this_file(this_file) MethodIntrospection.set_line_number(line_number) # ======================================================================= # # Next, we extract all the lines of a given file. lines will thus be a # very big Array if we have a big file. # ======================================================================= # lines = lines_for(this_file) # ======================================================================= # # Next tap into comment_describing(). This resides in code_helpers.rb # ======================================================================= # comment_describing(lines, line_number) end
#¶ ↑
MethodIntrospection.lines_for
¶ ↑
Load a memoized copy of the lines in a file.
@param [String] file_name @param [String] method_name @return [Array<String>] the contents of the file @raise [SourceNotFoundError]
#¶ ↑
# File lib/method_introspection/module_methods.rb, line 110 def self.lines_for(file_name, name = nil) @lines_for_file ||= {} @lines_for_file[file_name] ||= File.readlines(file_name) rescue Errno::ENOENT => e raise_this(name, e.message) end
#¶ ↑
MethodIntrospection.raise_this
¶ ↑
The second argument to this method is optional, and allows for a longer description for the error message at hand.
#¶ ↑
# File lib/method_introspection/module_methods.rb, line 62 def self.raise_this(name, optional_extra = nil) result = "We could not locate the source for #{name}" if optional_extra result << ": #{optional_extra}" else result << '.' end raise SourceNotFoundError, result end
#¶ ↑
MethodIntrospection.source_helper
¶ ↑
Helper method responsible for extracting the body of a method.
This is defined here to avoid polluting the `Method` class.
@param [Array] source_location The array returned by Method#source_location @param [String] method_name @return [String] The method body
#¶ ↑
# File lib/method_introspection/module_methods.rb, line 83 def self.source_helper(source_location, name = nil) raise_this(name) unless source_location this_file, line_number = *source_location # ======================================================================= # # Set the file. # ======================================================================= # MethodIntrospection.set_this_file(this_file) # ======================================================================= # # Set the line number in that file. # ======================================================================= # MethodIntrospection.set_line_number(line_number) expression_at(lines_for(this_file), line_number) rescue SyntaxError => error raise SourceNotFoundError, "Could not parse source for #{name}: #{error.message}" end