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 adjusted_source_location(sl)
def adjusted_source_location(sl) file, line = sl if file && RbxPath.is_core_path?(file) file = RbxPath.convert_path_to_full(file) end [file, line] end
def class_regexes
def class_regexes mod_type_string = wrapped.class.to_s.downcase [/^\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped.name.split(/::/).last}/, /^\s*(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/, /^\s*(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/] end
def doc
-
(String)
- The documentation for the candidate.
Raises:
-
(Pry::CommandError)
- If documentation cannot be found.
def doc return nil if file.nil? return @doc if @doc @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| class_regexes.any? { |r| r =~ 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 ||= adjusted_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!" elsif rank > (number_of_candidates - 1) raise CommandError, "No such module candidate. Allowed candidates range is from 0 to #{number_of_candidates - 1}" end @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 @end_method_source_location ||= adjusted_source_location(method_candidates[@rank].last.source_location) end
def number_of_lines_in_first_chunk
-
(Fixum)
- 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 if !file.is_a?(String) @source_location = [file, first_line_of_module_definition(file, line)] rescue Pry::RescuableException nil end