module MethodIntrospection

#
#

Constants

VERSION
#

VERSION

#

Public Class Methods

comment_helper(source_location, name = nil) click to toggle source
#

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
debug?() click to toggle source
#

MethodIntrospection.debug?

#
# File lib/method_introspection/module_methods.rb, line 17
def self.debug?
  @debug
end
enable_debug() click to toggle source
#

MethodIntrospection.enable_debug

#
# File lib/method_introspection/module_methods.rb, line 24
def self.enable_debug
  @debug = true
end
line_number?() click to toggle source
#

MethodIntrospection.line_number?

#
# File lib/method_introspection/module_methods.rb, line 52
def self.line_number?
  @line_number
end
lines_for(file_name, name = nil) click to toggle source
#

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
raise_this(name, optional_extra = nil) click to toggle source
#

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
set_line_number(i) click to toggle source
#

MethodIntrospection.set_line_number

#
# File lib/method_introspection/module_methods.rb, line 45
def self.set_line_number(i) # setter
  @line_number = i
end
set_this_file(i) click to toggle source
#

MethodIntrospection.set_this_file

#
# File lib/method_introspection/module_methods.rb, line 31
def self.set_this_file(i) # setter
  @this_file = i
end
source_helper(source_location, name = nil) click to toggle source
#

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
this_file?() click to toggle source
#

MethodIntrospection.this_file?

#
# File lib/method_introspection/module_methods.rb, line 38
def self.this_file?
  @this_file
end
toggle_debug() click to toggle source
#

MethodIntrospection.toggle_debug

#
# File lib/method_introspection/module_methods.rb, line 10
def self.toggle_debug
  @debug = !@debug
end