This class represents a single candidate for a module/class definition. It provides access to the source, documentation, line and file for a monkeypatch (reopening) of a class/module.
@raise [Pry::CommandError] If `rank` is out of bounds. @param [Pry::WrappedModule] wrapper The associated
`Pry::WrappedModule` instance that owns the candidates.
@param [Fixnum] rank The rank of the candidate to
retrieve. Passing 0 returns 'primary candidate' (the candidate with largest number of methods), passing 1 retrieves candidate with second largest number of methods, and so on, up to `Pry::WrappedModule#number_of_candidates() - 1`
# File lib/pry/module_candidate.rb, line 35 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
@raise [Pry::CommandError] If documentation cannot be found. @return [String] The documentation for the candidate.
# File lib/pry/module_candidate.rb, line 60 def doc return @doc if @doc raise CommandError, "Could not locate doc for #{wrapped}!" if file.nil? @doc = process_doc(Pry::Code.from_file(file).comment_describing(line)) end
@raise [Pry::CommandError] If source code cannot be found. @return [String] The source for the candidate, i.e the
complete module/class definition.
# File lib/pry/module_candidate.rb, line 51 def source return @source if @source raise CommandError, "Could not locate source for #{wrapped}!" if file.nil? @source = strip_leading_whitespace(Pry::Code.from_file(file).expression_at(line, number_of_lines_in_first_chunk)) end
@return [Array, nil] A `[String, Fixnum]` pair representing the
source location (file and line) for the candidate or `nil` if no source location found.
# File lib/pry/module_candidate.rb, line 70 def source_location return @source_location if @source_location mod_type_string = wrapped.class.to_s.downcase file, line = first_method_source_location return nil if !file.is_a?(String) class_regexes = [/^\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/] host_file_lines = lines_for_file(file) search_lines = host_file_lines[0..(line - 2)] idx = search_lines.rindex { |v| class_regexes.any? { |r| r =~ v } } @source_location = [file, idx + 1] rescue Pry::RescuableException nil end
Generated with the Darkfish Rdoc Generator 2.