Parent

Pry::WrappedModule

Public Class Methods

from_str(mod_name, target=TOPLEVEL_BINDING) click to toggle source

Convert a string to a module.

@param [String] mod_name @param [Binding] target The binding where the lookup takes place. @return [Module, nil] The module or `nil` (if conversion failed). @example

Pry::WrappedModule.from_str("Pry::Code")
# File lib/pry/wrapped_module.rb, line 29
def self.from_str(mod_name, target=TOPLEVEL_BINDING)
  kind = target.eval("defined?(#{mod_name})")

  # if we dont limit it to constants then from_str could end up
  # executing methods which is not good, i.e `show-source pry`
  if (kind == "constant" && target.eval(mod_name).is_a?(Module))
    Pry::WrappedModule.new(target.eval(mod_name))
  else
    nil
  end
rescue RescuableException
  nil
end
new(mod) click to toggle source

@raise [ArgumentError] if the argument is not a `Module` @param [Module] mod

# File lib/pry/wrapped_module.rb, line 45
def initialize(mod)
  raise ArgumentError, "Tried to initialize a WrappedModule with a non-module #{mod.inspect}" unless ::Module === mod
  @wrapped = mod
  @memoized_candidates = []
  @host_file_lines = nil
  @source = nil
  @source_location = nil
  @doc = nil
end

Public Instance Methods

candidate(rank) click to toggle source

Return a candidate for this module of specified rank. A `rank` of 0 is equivalent to the 'primary candidate', which is the module definition with the highest number of methods. A `rank` of 1 is the module definition with the second highest number of methods, and so on. Module candidates are necessary as modules can be reopened multiple times and in multiple places in Ruby, the candidate API gives you access to the module definition representing each of those reopenings. @raise [Pry::CommandError] If the `rank` is out of range. That

is greater than `number_of_candidates - 1`.

@param [Fixnum] rank @return [Pry::WrappedModule::Candidate]

# File lib/pry/wrapped_module.rb, line 191
def candidate(rank)
  @memoized_candidates[rank] ||= Candidate.new(self, rank)
end
doc() click to toggle source

Returns documentation for the module. This documentation is for the primary candidate, if you would like documentation for other candidates use `WrappedModule#candidate` to select the candidate you're interested in. @raise [Pry::CommandError] If documentation cannot be found. @return [String] The documentation for the module.

# File lib/pry/wrapped_module.rb, line 147
def doc
  @doc ||= primary_candidate.doc
end
file() click to toggle source

@return [String, nil] The associated file for the module (i.e

the primary candidate: highest ranked monkeypatch).
# File lib/pry/wrapped_module.rb, line 128
def file
  Array(source_location).first
end
Also aliased as: source_file
line() click to toggle source

@return [Fixnum, nil] The associated line for the module (i.e

the primary candidate: highest ranked monkeypatch).
# File lib/pry/wrapped_module.rb, line 135
def line
  Array(source_location).last
end
Also aliased as: source_line
method_missing(method_name, *args, &block) click to toggle source

Forward method invocations to the wrapped module

# File lib/pry/wrapped_module.rb, line 105
def method_missing(method_name, *args, &block)
  wrapped.send(method_name, *args, &block)
end
method_prefix() click to toggle source

The prefix that would appear before methods defined on this class.

i.e. the "String." or "String#" in String.new and String#initialize.

@return String

# File lib/pry/wrapped_module.rb, line 60
def method_prefix
  if singleton_class?
    if Module === singleton_instance
      "#{WrappedModule.new(singleton_instance).nonblank_name}."
    else
      "self."
    end
  else
    "#{nonblank_name}#"
  end
end
nonblank_name() click to toggle source

The name of the Module if it has one, otherwise #<Class:0xf00>.

@return [String]

# File lib/pry/wrapped_module.rb, line 75
def nonblank_name
  if name.to_s == ""
    wrapped.inspect
  else
    name
  end
end
number_of_candidates() click to toggle source

@return [Fixnum] The number of candidate definitions for the

current module.
# File lib/pry/wrapped_module.rb, line 198
def number_of_candidates
  method_candidates.count
end
respond_to?(method_name) click to toggle source
# File lib/pry/wrapped_module.rb, line 109
def respond_to?(method_name)
  super || wrapped.respond_to?(method_name)
end
singleton_class?() click to toggle source

Is this a singleton class? @return [Boolean]

# File lib/pry/wrapped_module.rb, line 85
def singleton_class?
  wrapped != wrapped.ancestors.first
end
singleton_instance() click to toggle source

Get the instance associated with this singleton class.

@raise ArgumentError: tried to get instance of non singleton class

@return [Object]

# File lib/pry/wrapped_module.rb, line 94
def singleton_instance
  raise ArgumentError, "tried to get instance of non singleton class" unless singleton_class?

  if Helpers::BaseHelpers.jruby?
    wrapped.to_java.attached
  else
    @singleton_instance ||= ObjectSpace.each_object(wrapped).detect{ |x| (class << x; self; end) == wrapped }
  end
end
source() click to toggle source

Returns the source for the module. This source is for the primary candidate, if you would like source for other candidates use `WrappedModule#candidate` to select the candidate you're interested in. @raise [Pry::CommandError] If source cannot be found. @return [String] The source for the module.

# File lib/pry/wrapped_module.rb, line 158
def source
  @source ||= primary_candidate.source
end
source_file() click to toggle source
Alias for: file
source_line() click to toggle source
Alias for: line
source_location() click to toggle source

Retrieve the source location of a module. Return value is in same format as Method#source_location. If the source location cannot be found this method returns `nil`.

@param [Module] mod The module (or class). @return [Array<String, Fixnum>, nil] The source location of the

module (or class), or `nil` if no source location found.
# File lib/pry/wrapped_module.rb, line 120
def source_location
  @source_location ||= primary_candidate.source_location
rescue Pry::RescuableException
  nil
end
yard_doc() click to toggle source

@return [String] Return the YARD docs for this module.

# File lib/pry/wrapped_module.rb, line 175
def yard_doc
  YARD::Registry.at(name).docstring.to_s if yard_docs?
end
yard_docs?() click to toggle source

@return [Boolean] Whether YARD docs are available for this module.

# File lib/pry/wrapped_module.rb, line 203
def yard_docs?
  !!(defined?(YARD) && YARD::Registry.at(name))
end
yard_file() click to toggle source

@return [String] Return the associated file for the

module from YARD, if one exists.
# File lib/pry/wrapped_module.rb, line 164
def yard_file
  YARD::Registry.at(name).file if yard_docs?
end
yard_line() click to toggle source

@return [Fixnum] Return the associated line for the

module from YARD, if one exists.
# File lib/pry/wrapped_module.rb, line 170
def yard_line
  YARD::Registry.at(name).line if yard_docs?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.