module MethodSource

def self.clear_cache

Clear cache.
def self.clear_cache
  @lines_for_file = {}
end

def self.comment_helper(source_location, name=nil)

Returns:
  • (String) - The comments up to the point of the method.

Parameters:
  • method_name (String) --
  • source_location (Array) -- The array returned by Method#source_location
def self.comment_helper(source_location, name=nil)
  raise SourceNotFoundError, "Could not locate source for #{name}!" unless source_location
  file, line = *source_location
  comment_describing(lines_for(file), line)
end

def self.extract_code(source_location)

Deprecated:
  • — use MethodSource::CodeHelpers#expression_at
def self.extract_code(source_location)
  source_helper(source_location)
end

def self.lines_for(file_name, name=nil)

Raises:
  • (SourceNotFoundError) -

Returns:
  • (Array) - the contents of the file

Parameters:
  • method_name (String) --
  • file_name (String) --
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 SourceNotFoundError, "Could not load source for #{name}: #{e.message}"
end

def self.source_helper(source_location, name=nil)

Returns:
  • (String) - The method body

Parameters:
  • method_name (String) --
  • source_location (Array) -- The array returned by Method#source_location
def self.source_helper(source_location, name=nil)
  raise SourceNotFoundError, "Could not locate source for #{name}!" unless source_location
  file, line = *source_location
  expression_at(lines_for(file), line)
rescue SyntaxError => e
  raise SourceNotFoundError, "Could not parse source for #{name}: #{e.message}"
end

def self.valid_expression?(str)

Deprecated:
  • — use MethodSource::CodeHelpers#complete_expression?
def self.valid_expression?(str)
  complete_expression?(str)
rescue SyntaxError
  false
end