class Pry::WrappedModule::Candidate - pry-0.10.1 Documentation

# class Pry::WrappedModule::Candidate

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.

### Attributes

file[R]

@return [String] The file where the module definition is located.

line[R]

@return [Fixnum] The line where the module definition is located.

source_file[R]

@return [String] The file where the module definition is located.

source_line[R]

@return [Fixnum] The line where the module definition is located.

### Public Class Methods

new(wrapper, rank) click to toggle source

@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 43
    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

### Public Instance Methods

doc() click to toggle source

@raise [Pry::CommandError] If documentation cannot be found. @return [String] The documentation for the candidate.

    # File lib/pry/module_candidate.rb, line 68
    def doc
      return nil if file.nil?
      return @doc if @doc

      @doc = get_comment_content(Pry::Code.from_file(file).comment_describing(line))
    end

source() click to toggle source

@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 59
    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

source_location() click to toggle source

@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 78
    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