class Pry::WrappedModule::Candidate
for a monkeypatch (reopening) of a class/module.
It provides access to the source, documentation, line and file
This class represents a single candidate for a module/class definition.
def doc
-
(String)- The documentation for the candidate.
Raises:
-
(Pry::CommandError)- If documentation cannot be found.
def doc return nil if file.nil? @doc ||= get_comment_content(Pry::Code.from_file(file).comment_describing(line)) end
def first_line_of_module_definition(file, line)
-
(Fixnum)- The line where the module is defined. This
Parameters:
-
line(Fixnum) -- The module definition should appear -
file(String) -- The file that contains the module
def first_line_of_module_definition(file, line) searchable_lines = lines_for_file(file)[0..(line - 2)] searchable_lines.rindex { |v| module_definition_first_line?(v) } + 1 end
def first_method_source_location
-
(Array)- The source location of the base method used to
def first_method_source_location @first_method_source_location ||= method_candidates[@rank].first.source_location end
def initialize(wrapper, rank)
-
rank(Fixnum) -- The rank of the candidate to -
wrapper(Pry::WrappedModule) -- The associated
Raises:
-
(Pry::CommandError)- If `rank` is out of bounds.
def initialize(wrapper, rank) @wrapper = wrapper if number_of_candidates <= 0 raise CommandError, "Cannot find a definition for #{name} module!" end if rank > (number_of_candidates - 1) raise CommandError, "No such module candidate. Allowed candidates range is " \ "from 0 to #{number_of_candidates - 1}" end @source = @source_location = nil @rank = rank @file, @line = source_location end
def last_method_source_location
-
(Array)- The source location of the last method in this
def last_method_source_location @last_method_source_location ||= method_candidates[@rank].last.source_location end
def module_definition_first_line?(line)
def module_definition_first_line?(line) mod_type_string = wrapped.class.to_s.downcase wrapped_name_last = wrapped.name.split(/::/).last /(^|=)\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped_name_last}/ =~ line || /^\s*(::)?#{wrapped_name_last}\s*?=\s*?#{wrapped.class}/ =~ line || /^\s*(::)?#{wrapped_name_last}\.(class|instance)_eval/ =~ line end
def number_of_lines_in_first_chunk
-
(Integer)- number of lines.
def number_of_lines_in_first_chunk end_method_line = last_method_source_location.last end_method_line - line end
def source
-
(String)- The source for the candidate, i.e the
Raises:
-
(Pry::CommandError)- If source code cannot be found.
def source return nil if file.nil? return @source if @source @source ||= strip_leading_whitespace( Pry::Code.from_file(file).expression_at(line, number_of_lines_in_first_chunk) ) end
def source_location
-
(Array, nil)- A `[String, Fixnum]` pair representing the
def source_location return @source_location if @source_location file, line = first_method_source_location return nil unless file.is_a?(String) @source_location = [file, first_line_of_module_definition(file, line)] rescue Pry::RescuableException nil end