Class: Pry::WrappedModule::Candidate

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CodeObject::Helpers, Helpers::DocumentationHelpers
Defined in:
lib/pry/module_candidate.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CodeObject::Helpers

#c_method?, #command?, #module_with_yard_docs?, #real_method_object?

Methods included from Helpers::DocumentationHelpers

get_comment_content, process_comment_markup, process_rdoc, process_yardoc, process_yardoc_tag, strip_comments_from_c_code, strip_leading_whitespace

Constructor Details

#initialize(wrapper, rank) ⇒ Candidate

Returns a new instance of Candidate

Raises:


43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Attribute Details

#fileString (readonly) Also known as: source_file


16
17
18
# File 'lib/pry/module_candidate.rb', line 16

def file
  @file
end

#lineFixnum (readonly) Also known as: source_line


20
21
22
# File 'lib/pry/module_candidate.rb', line 20

def line
  @line
end

Instance Method Details

#docString

Returns The documentation for the candidate.

Raises:


68
69
70
71
72
73
# 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

#sourceString

Returns The source for the candidate, i.e the complete module/class definition.

Raises:


59
60
61
62
63
64
# 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_locationArray?


78
79
80
81
82
83
84
85
86
87
# 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