module MethodSource

Constants

VERSION

Public Class Methods

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

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 MethodSource::SourceLocation::MethodExtensions#source_location @param [String] method_name @return [String] The comments up to the point of the method.

# File lib/method_source.rb, line 38
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
extract_code(source_location) click to toggle source

@deprecated — use MethodSource::CodeHelpers#expression_at

# File lib/method_source.rb, line 66
def self.extract_code(source_location)
  source_helper(source_location)
end
lines_for(file_name, name=nil) click to toggle source

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_source.rb, line 51
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
source_helper(source_location, name=nil) click to toggle source

Helper method responsible for extracting method body. Defined here to avoid polluting `Method` class. @param [Array] source_location The array returned by MethodSource::SourceLocation::MethodExtensions#source_location @param [String] method_name @return [String] The method body

# File lib/method_source.rb, line 23
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
valid_expression?(str) click to toggle source

@deprecated — use MethodSource::CodeHelpers#complete_expression?

# File lib/method_source.rb, line 59
def self.valid_expression?(str)
  complete_expression?(str)
rescue SyntaxError
  false
end