Class: Pry::WrappedModule::Candidate

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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

Constructor Details

#initialize(wrapper, rank) ⇒ Candidate

Returns a new instance of Candidate.

Parameters:

  • wrapper (Pry::WrappedModule)

    The associated Pry::WrappedModule instance that owns the candidates.

  • rank (Fixnum)

    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

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#fileString (readonly)

Returns The file where the module definition is located.

Returns:

  • (String)

    The file where the module definition is located.



14
15
16
# File 'lib/pry/module_candidate.rb', line 14

def file
  @file
end

#lineFixnum (readonly)

Returns The line where the module definition is located.

Returns:

  • (Fixnum)

    The line where the module definition is located.



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

def line
  @line
end

Instance Method Details

#adjusted_source_location(sl) ⇒ Object (private)



119
120
121
122
123
124
125
126
127
# File 'lib/pry/module_candidate.rb', line 119

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

#docString

Returns The documentation for the candidate.

Returns:

  • (String)

    The documentation for the candidate.

Raises:



60
61
62
63
64
65
# 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

#first_method_source_locationArray (private)

This method is used by Candidate#source_location as a starting point for the search for the candidate's definition.

Returns:

  • (Array)

    The source location of the base method used to calculate the source location of the candidate.



98
99
100
# File 'lib/pry/module_candidate.rb', line 98

def first_method_source_location
  @first_method_source_location ||= adjusted_source_location(method_candidates[@rank].first.source_location)
end

#last_method_source_locationArray (private)

Returns The source location of the last method in this candidate's module definition.

Returns:

  • (Array)

    The source location of the last method in this candidate's module definition.



104
105
106
# File 'lib/pry/module_candidate.rb', line 104

def last_method_source_location
  @end_method_source_location ||= adjusted_source_location(method_candidates[@rank].last.source_location)
end

#number_of_lines_in_first_chunkFixum (private)

Return the number of lines between the start of the class definition and the start of the last method. We use this value so we can quickly grab these lines from the file (without having to check each intervening line for validity, which is expensive) speeding up source extraction.

Returns:

  • (Fixum)

    Number of lines.



113
114
115
116
117
# File 'lib/pry/module_candidate.rb', line 113

def number_of_lines_in_first_chunk
  end_method_line = last_method_source_location.last

  end_method_line - line
end

#sourceString

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

Returns:

  • (String)

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

Raises:



51
52
53
54
55
56
# 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

#source_locationArray?

Returns A [String, Fixnum] pair representing the source location (file and line) for the candidate or nil if no source location found.

Returns:

  • (Array, nil)

    A [String, Fixnum] pair representing the source location (file and line) for the candidate or nil if no source location found.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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