2011-11-12 08:03:02 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-04-12 10:17:47 -04:00
|
|
|
require 'pry/helpers/documentation_helpers'
|
|
|
|
|
2011-09-19 02:42:31 -04:00
|
|
|
class Pry
|
2012-01-09 22:59:13 -05:00
|
|
|
class << self
|
|
|
|
# If the given object is a `Pry::Method`, return it unaltered. If it's
|
|
|
|
# anything else, return it wrapped in a `Pry::Method` instance.
|
|
|
|
def Method(obj)
|
|
|
|
if obj.is_a? Pry::Method
|
|
|
|
obj
|
|
|
|
else
|
|
|
|
Pry::Method.new(obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-17 07:26:45 -04:00
|
|
|
# This class wraps the normal `Method` and `UnboundMethod` classes
|
|
|
|
# to provide extra functionality useful to Pry.
|
2011-09-19 02:42:31 -04:00
|
|
|
class Method
|
2012-11-02 09:28:07 -04:00
|
|
|
extend Helpers::BaseHelpers
|
2012-12-20 11:28:00 -05:00
|
|
|
include Helpers::BaseHelpers
|
2011-10-02 23:13:10 -04:00
|
|
|
include RbxMethod if Helpers::BaseHelpers.rbx?
|
2012-04-12 10:17:47 -04:00
|
|
|
include Helpers::DocumentationHelpers
|
2012-12-25 07:47:33 -05:00
|
|
|
include CodeObject::Helpers
|
2011-09-19 02:42:31 -04:00
|
|
|
|
2011-09-25 01:13:52 -04:00
|
|
|
class << self
|
2011-10-02 04:25:49 -04:00
|
|
|
# Given a string representing a method name and optionally a binding to
|
|
|
|
# search in, find and return the requested method wrapped in a `Pry::Method`
|
|
|
|
# instance.
|
|
|
|
#
|
2011-10-02 00:14:32 -04:00
|
|
|
# @param [String, nil] name The name of the method to retrieve, or `nil` to
|
|
|
|
# delegate to `from_binding` instead.
|
|
|
|
# @param [Binding] target The context in which to search for the method.
|
|
|
|
# @param [Hash] options
|
|
|
|
# @option options [Boolean] :instance Look for an instance method if `name` doesn't
|
|
|
|
# contain any context.
|
|
|
|
# @option options [Boolean] :methods Look for a bound/singleton method if `name` doesn't
|
|
|
|
# contain any context.
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [Pry::Method, nil] A `Pry::Method` instance containing the requested
|
|
|
|
# method, or `nil` if no method could be located matching the parameters.
|
2011-10-02 00:14:32 -04:00
|
|
|
def from_str(name, target=TOPLEVEL_BINDING, options={})
|
|
|
|
if name.nil?
|
2011-09-25 01:13:52 -04:00
|
|
|
from_binding(target)
|
2011-10-15 03:51:56 -04:00
|
|
|
elsif name.to_s =~ /(.+)\#(\S+)\Z/
|
2011-09-25 01:13:52 -04:00
|
|
|
context, meth_name = $1, $2
|
2012-11-02 09:28:07 -04:00
|
|
|
from_module(target.eval(context), meth_name, target)
|
2012-11-13 23:32:31 -05:00
|
|
|
elsif name.to_s =~ /(.+)(\.|::)(\S+)\Z/
|
|
|
|
context, meth_name = $1, $3
|
2012-11-02 09:28:07 -04:00
|
|
|
from_obj(target.eval(context), meth_name, target)
|
2011-09-25 01:13:52 -04:00
|
|
|
elsif options[:instance]
|
2012-11-02 09:28:07 -04:00
|
|
|
from_module(target.eval("self"), name, target)
|
2011-09-25 01:13:52 -04:00
|
|
|
elsif options[:methods]
|
2012-11-02 09:28:07 -04:00
|
|
|
from_obj(target.eval("self"), name, target)
|
2011-09-25 01:13:52 -04:00
|
|
|
else
|
2011-10-02 00:14:32 -04:00
|
|
|
from_str(name, target, :instance => true) or
|
|
|
|
from_str(name, target, :methods => true)
|
2011-09-25 01:13:52 -04:00
|
|
|
end
|
2012-12-22 16:45:33 -05:00
|
|
|
|
|
|
|
rescue Pry::RescuableException
|
|
|
|
nil
|
2011-09-25 01:13:52 -04:00
|
|
|
end
|
2011-09-19 02:42:31 -04:00
|
|
|
|
2011-10-02 00:14:32 -04:00
|
|
|
# Given a `Binding`, try to extract the `::Method` it originated from and
|
|
|
|
# use it to instantiate a `Pry::Method`. Return `nil` if this isn't
|
|
|
|
# possible.
|
2011-10-02 04:25:49 -04:00
|
|
|
#
|
2011-10-02 00:14:32 -04:00
|
|
|
# @param [Binding] b
|
|
|
|
# @return [Pry::Method, nil]
|
2011-10-02 04:25:49 -04:00
|
|
|
#
|
2011-09-25 01:13:52 -04:00
|
|
|
def from_binding(b)
|
2012-09-23 23:54:35 -04:00
|
|
|
meth_name = b.eval('::Kernel.__method__')
|
2012-06-07 00:25:51 -04:00
|
|
|
if [:__script__, nil].include?(meth_name)
|
2011-09-25 01:13:52 -04:00
|
|
|
nil
|
|
|
|
else
|
2011-12-23 17:44:01 -05:00
|
|
|
method = begin
|
2012-09-23 23:54:35 -04:00
|
|
|
if Object === b.eval('self')
|
|
|
|
new(Kernel.instance_method(:method).bind(b.eval("self")).call(meth_name))
|
|
|
|
else
|
|
|
|
new(b.eval('class << self; self; end.instance_method(::Kernel.__method__).bind(self)'))
|
|
|
|
end
|
2011-12-23 17:44:01 -05:00
|
|
|
rescue NameError, NoMethodError
|
|
|
|
Disowned.new(b.eval('self'), meth_name.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
# it's possible in some cases that the method we find by this approach is a sub-method of
|
|
|
|
# the one we're currently in, consider:
|
|
|
|
#
|
|
|
|
# class A; def b; binding.pry; end; end
|
|
|
|
# class B < A; def b; super; end; end
|
|
|
|
#
|
|
|
|
# Given that we can normally find the source_range of methods, and that we know which
|
|
|
|
# __FILE__ and __LINE__ the binding is at, we can hope to disambiguate these cases.
|
|
|
|
#
|
|
|
|
# This obviously won't work if the source is unavaiable for some reason, or if both
|
|
|
|
# methods have the same __FILE__ and __LINE__, or if we're in rbx where b.eval('__LINE__')
|
|
|
|
# is broken.
|
|
|
|
#
|
|
|
|
guess = method
|
|
|
|
|
|
|
|
while guess
|
|
|
|
# needs rescue if this is a Disowned method or a C method or something...
|
|
|
|
# TODO: Fix up the exception handling so we don't need a bare rescue
|
|
|
|
if (guess.source_file && guess.source_range rescue false) &&
|
|
|
|
File.expand_path(guess.source_file) == File.expand_path(b.eval('__FILE__')) &&
|
|
|
|
guess.source_range.include?(b.eval('__LINE__'))
|
|
|
|
return guess
|
|
|
|
else
|
|
|
|
guess = guess.super
|
|
|
|
end
|
2011-12-03 23:21:02 -05:00
|
|
|
end
|
2011-12-23 17:44:01 -05:00
|
|
|
|
|
|
|
# Uhoh... none of the methods in the chain had the right __FILE__ and __LINE__
|
|
|
|
# This may be caused by rbx https://github.com/rubinius/rubinius/issues/953,
|
|
|
|
# or other unknown circumstances (TODO: we should warn the user when this happens)
|
|
|
|
method
|
2011-09-25 01:13:52 -04:00
|
|
|
end
|
|
|
|
end
|
2011-09-19 02:42:31 -04:00
|
|
|
|
2012-11-02 09:28:07 -04:00
|
|
|
# In order to support 2.0 Refinements we need to look up methods
|
|
|
|
# inside the relevant Binding.
|
|
|
|
# @param [Object] obj The owner/receiver of the method.
|
|
|
|
# @param [Symbol] method_name The name of the method.
|
|
|
|
# @param [Symbol] method_type The type of method: :method or :instance_method
|
|
|
|
# @param [Binding] target The binding where the method is looked up.
|
|
|
|
# @return [Method, UnboundMethod] The 'refined' method object.
|
|
|
|
def lookup_method_via_binding(obj, method_name, method_type, target=TOPLEVEL_BINDING)
|
2013-01-15 17:29:23 -05:00
|
|
|
Pry.current[:obj] = obj
|
|
|
|
Pry.current[:name] = method_name
|
2012-11-02 09:28:07 -04:00
|
|
|
receiver = obj.is_a?(Module) ? "Module" : "Kernel"
|
2013-01-15 17:29:23 -05:00
|
|
|
target.eval("::#{receiver}.instance_method(:#{method_type}).bind(Pry.current[:obj]).call(Pry.current[:name])")
|
2012-11-02 09:28:07 -04:00
|
|
|
ensure
|
2013-01-15 17:29:23 -05:00
|
|
|
Pry.current[:obj] = Pry.current[:name] = nil
|
2012-11-02 09:28:07 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 00:14:32 -04:00
|
|
|
# Given a `Class` or `Module` and the name of a method, try to
|
|
|
|
# instantiate a `Pry::Method` containing the instance method of
|
|
|
|
# that name. Return `nil` if no such method exists.
|
2011-10-02 04:25:49 -04:00
|
|
|
#
|
2011-10-02 00:14:32 -04:00
|
|
|
# @param [Class, Module] klass
|
|
|
|
# @param [String] name
|
2012-11-02 09:46:01 -04:00
|
|
|
# @param [Binding] target The binding where the method is looked up.
|
2011-10-02 00:14:32 -04:00
|
|
|
# @return [Pry::Method, nil]
|
2012-11-02 09:28:07 -04:00
|
|
|
def from_class(klass, name, target=TOPLEVEL_BINDING)
|
|
|
|
new(lookup_method_via_binding(klass, name, :instance_method, target)) rescue nil
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
2011-10-02 00:14:32 -04:00
|
|
|
alias from_module from_class
|
|
|
|
|
|
|
|
# Given an object and the name of a method, try to instantiate
|
|
|
|
# a `Pry::Method` containing the method of that name bound to
|
|
|
|
# that object. Return `nil` if no such method exists.
|
2011-10-02 04:25:49 -04:00
|
|
|
#
|
2011-10-02 00:14:32 -04:00
|
|
|
# @param [Object] obj
|
|
|
|
# @param [String] name
|
2012-11-02 09:46:01 -04:00
|
|
|
# @param [Binding] target The binding where the method is looked up.
|
2011-10-02 00:14:32 -04:00
|
|
|
# @return [Pry::Method, nil]
|
2012-11-02 09:28:07 -04:00
|
|
|
def from_obj(obj, name, target=TOPLEVEL_BINDING)
|
|
|
|
new(lookup_method_via_binding(obj, name, :method, target)) rescue nil
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
2012-11-28 07:49:39 -05:00
|
|
|
|
2011-10-02 02:30:33 -04:00
|
|
|
# Get all of the instance methods of a `Class` or `Module`
|
|
|
|
# @param [Class,Module] klass
|
2012-04-10 23:26:59 -04:00
|
|
|
# @param [Boolean] include_super Whether to include methods from ancestors.
|
2011-10-02 02:30:33 -04:00
|
|
|
# @return [Array[Pry::Method]]
|
2012-04-10 23:26:59 -04:00
|
|
|
def all_from_class(klass, include_super=true)
|
|
|
|
all_from_common(klass, :instance_method, include_super)
|
2011-10-02 02:30:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get all of the methods on an `Object`
|
|
|
|
# @param [Object] obj
|
2012-04-10 23:26:59 -04:00
|
|
|
# @param [Boolean] include_super Whether to include methods from ancestors.
|
2011-10-02 02:30:33 -04:00
|
|
|
# @return [Array[Pry::Method]]
|
2012-04-10 23:26:59 -04:00
|
|
|
def all_from_obj(obj, include_super=true)
|
|
|
|
all_from_common(obj, :method, include_super)
|
2011-10-02 02:30:33 -04:00
|
|
|
end
|
|
|
|
|
2011-10-05 04:08:17 -04:00
|
|
|
# Get every `Class` and `Module`, in order, that will be checked when looking
|
|
|
|
# for an instance method to call on this object.
|
|
|
|
# @param [Object] obj
|
|
|
|
# @return [Array[Class, Module]]
|
|
|
|
def resolution_order(obj)
|
2011-10-25 16:37:41 -04:00
|
|
|
if Class === obj
|
2011-10-05 04:08:17 -04:00
|
|
|
singleton_class_resolution_order(obj) + instance_resolution_order(Class)
|
|
|
|
else
|
|
|
|
klass = singleton_class(obj) rescue obj.class
|
|
|
|
instance_resolution_order(klass)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get every `Class` and `Module`, in order, that will be checked when looking
|
|
|
|
# for methods on instances of the given `Class` or `Module`.
|
|
|
|
# This does not treat singleton classes of classes specially.
|
|
|
|
# @param [Class, Module] klass
|
|
|
|
# @return [Array[Class, Module]]
|
|
|
|
def instance_resolution_order(klass)
|
|
|
|
# include klass in case it is a singleton class,
|
|
|
|
([klass] + klass.ancestors).uniq
|
|
|
|
end
|
|
|
|
|
2011-10-02 02:30:33 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# See all_from_class and all_from_obj.
|
|
|
|
# If method_type is :instance_method, obj must be a `Class` or a `Module`
|
|
|
|
# If method_type is :method, obj can be any `Object`
|
|
|
|
#
|
|
|
|
# N.B. we pre-cache the visibility here to avoid O(N²) behaviour in "ls".
|
2012-04-10 23:26:59 -04:00
|
|
|
def all_from_common(obj, method_type, include_super=true)
|
2011-10-02 02:30:33 -04:00
|
|
|
%w(public protected private).map do |visibility|
|
2012-04-10 23:26:59 -04:00
|
|
|
safe_send(obj, :"#{visibility}_#{method_type}s", include_super).map do |method_name|
|
2011-10-25 16:37:41 -04:00
|
|
|
new(safe_send(obj, method_type, method_name), :visibility => visibility.to_sym)
|
2011-10-02 02:30:33 -04:00
|
|
|
end
|
|
|
|
end.flatten(1)
|
|
|
|
end
|
2011-10-05 04:08:17 -04:00
|
|
|
|
|
|
|
# Get the singleton classes of superclasses that could define methods on
|
|
|
|
# the given class object, and any modules they include.
|
|
|
|
# If a module is included at multiple points in the ancestry, only
|
|
|
|
# the lowest copy will be returned.
|
|
|
|
def singleton_class_resolution_order(klass)
|
|
|
|
resolution_order = klass.ancestors.map do |anc|
|
|
|
|
[singleton_class(anc)] + singleton_class(anc).included_modules if anc.is_a?(Class)
|
|
|
|
end.compact.flatten(1)
|
|
|
|
|
|
|
|
resolution_order.reverse.uniq.reverse - Class.included_modules
|
|
|
|
end
|
|
|
|
|
|
|
|
def singleton_class(obj); class << obj; self; end end
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# A new instance of `Pry::Method` wrapping the given `::Method`, `UnboundMethod`, or `Proc`.
|
|
|
|
#
|
|
|
|
# @param [::Method, UnboundMethod, Proc] method
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [Hash] known_info Can be used to pre-cache expensive to compute stuff.
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [Pry::Method]
|
2011-10-02 02:30:33 -04:00
|
|
|
def initialize(method, known_info={})
|
2011-09-19 02:42:31 -04:00
|
|
|
@method = method
|
2011-10-02 02:30:33 -04:00
|
|
|
@visibility = known_info[:visibility]
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 03:02:59 -04:00
|
|
|
# Get the name of the method as a String, regardless of the underlying Method#name type.
|
|
|
|
# @return [String]
|
|
|
|
def name
|
|
|
|
@method.name.to_s
|
|
|
|
end
|
|
|
|
|
2011-12-02 03:02:14 -05:00
|
|
|
# Get the owner of the method as a Pry::Module
|
|
|
|
# @return [Pry::Module]
|
|
|
|
def wrapped_owner
|
2011-12-03 23:21:02 -05:00
|
|
|
@wrapped_owner ||= Pry::WrappedModule.new(owner)
|
2011-12-02 03:02:14 -05:00
|
|
|
end
|
|
|
|
|
2012-12-20 11:28:00 -05:00
|
|
|
# Get underlying object wrapped by this Pry::Method instance
|
|
|
|
# @return [Method, UnboundMethod, Proc]
|
|
|
|
def wrapped
|
|
|
|
@method
|
|
|
|
end
|
|
|
|
|
2011-12-20 10:24:35 -05:00
|
|
|
# Is the method undefined? (aka `Disowned`)
|
|
|
|
# @return [Boolean] false
|
|
|
|
def undefined?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2011-12-02 03:02:14 -05:00
|
|
|
# Get the name of the method including the class on which it was defined.
|
|
|
|
# @example
|
|
|
|
# method(:puts).method_name
|
|
|
|
# => "Kernel.puts"
|
|
|
|
# @return [String]
|
|
|
|
def name_with_owner
|
|
|
|
"#{wrapped_owner.method_prefix}#{name}"
|
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [String, nil] The source code of the method, or `nil` if it's unavailable.
|
2011-09-19 02:42:31 -04:00
|
|
|
def source
|
|
|
|
@source ||= case source_type
|
2012-04-17 01:14:35 -04:00
|
|
|
when :c
|
|
|
|
info = pry_doc_info
|
|
|
|
if info and info.source
|
|
|
|
code = strip_comments_from_c_code(info.source)
|
|
|
|
end
|
|
|
|
when :ruby
|
2012-06-03 04:55:07 -04:00
|
|
|
# clone of MethodSource.source_helper that knows to use our
|
|
|
|
# hacked version of source_location for rbx core methods, and
|
|
|
|
# our input buffer for methods defined in (pry)
|
|
|
|
file, line = *source_location
|
|
|
|
raise SourceNotFoundError, "Could not locate source for #{name_with_owner}!" unless file
|
|
|
|
|
|
|
|
begin
|
|
|
|
code = Pry::Code.from_file(file).expression_at(line)
|
|
|
|
rescue SyntaxError => e
|
|
|
|
raise MethodSource::SourceNotFoundError.new(e.message)
|
2012-04-17 01:14:35 -04:00
|
|
|
end
|
|
|
|
strip_leading_whitespace(code)
|
|
|
|
end
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
|
2012-06-03 23:09:00 -04:00
|
|
|
# Can we get the source code for this method?
|
|
|
|
# @return [Boolean]
|
|
|
|
def source?
|
|
|
|
!!source
|
|
|
|
rescue MethodSource::SourceNotFoundError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [String, nil] The documentation for the method, or `nil` if it's
|
|
|
|
# unavailable.
|
2011-10-02 00:14:32 -04:00
|
|
|
# @raise [CommandError] Raises when the method was defined in the REPL.
|
2011-09-19 02:42:31 -04:00
|
|
|
def doc
|
|
|
|
@doc ||= case source_type
|
|
|
|
when :c
|
|
|
|
info = pry_doc_info
|
|
|
|
info.docstring if info
|
|
|
|
when :ruby
|
2012-05-28 19:37:02 -04:00
|
|
|
if Helpers::BaseHelpers.rbx? && !pry_method?
|
|
|
|
strip_leading_hash_and_whitespace_from_ruby_comments(core_doc)
|
2011-11-06 01:02:40 -04:00
|
|
|
elsif pry_method?
|
2012-05-28 19:37:02 -04:00
|
|
|
strip_leading_hash_and_whitespace_from_ruby_comments(doc_for_pry_method)
|
2011-09-19 02:42:31 -04:00
|
|
|
else
|
|
|
|
strip_leading_hash_and_whitespace_from_ruby_comments(@method.comment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [Symbol] The source type of the method. The options are
|
2011-11-06 01:02:40 -04:00
|
|
|
# `:ruby` for Ruby methods or `:c` for methods written in C.
|
2011-09-19 02:42:31 -04:00
|
|
|
def source_type
|
2011-11-06 01:02:40 -04:00
|
|
|
source_location.nil? ? :c : :ruby
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
|
2012-04-13 02:36:24 -04:00
|
|
|
def source_location
|
2012-04-15 19:35:55 -04:00
|
|
|
if @method.source_location && Helpers::BaseHelpers.rbx?
|
2012-04-13 02:36:24 -04:00
|
|
|
file, line = @method.source_location
|
|
|
|
[RbxPath.convert_path_to_full(file), line]
|
|
|
|
else
|
|
|
|
@method.source_location
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [String, nil] The name of the file the method is defined in, or
|
|
|
|
# `nil` if the filename is unavailable.
|
2011-09-19 02:42:31 -04:00
|
|
|
def source_file
|
2011-10-02 23:13:10 -04:00
|
|
|
if source_location.nil?
|
|
|
|
if !Helpers::BaseHelpers.rbx? and source_type == :c
|
|
|
|
info = pry_doc_info
|
|
|
|
info.file if info
|
|
|
|
end
|
|
|
|
else
|
|
|
|
source_location.first
|
|
|
|
end
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [Fixnum, nil] The line of code in `source_file` which begins
|
|
|
|
# the method's definition, or `nil` if that information is unavailable.
|
2011-09-19 02:42:31 -04:00
|
|
|
def source_line
|
|
|
|
source_location.nil? ? nil : source_location.last
|
|
|
|
end
|
|
|
|
|
2011-12-23 17:44:01 -05:00
|
|
|
# @return [Range, nil] The range of lines in `source_file` which contain
|
|
|
|
# the method's definition, or `nil` if that information is unavailable.
|
|
|
|
def source_range
|
2012-04-14 01:26:18 -04:00
|
|
|
source_location.nil? ? nil : (source_line)..(source_line + source.lines.count - 1)
|
2011-12-23 17:44:01 -05:00
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [Symbol] The visibility of the method. May be `:public`,
|
|
|
|
# `:protected`, or `:private`.
|
2011-09-19 02:42:31 -04:00
|
|
|
def visibility
|
2011-10-26 00:39:35 -04:00
|
|
|
@visibility ||= if owner.public_instance_methods.any? { |m| m.to_s == name }
|
2011-10-02 02:30:33 -04:00
|
|
|
:public
|
2011-10-26 00:39:35 -04:00
|
|
|
elsif owner.protected_instance_methods.any? { |m| m.to_s == name }
|
2011-10-02 02:30:33 -04:00
|
|
|
:protected
|
2011-10-26 00:41:19 -04:00
|
|
|
elsif owner.private_instance_methods.any? { |m| m.to_s == name }
|
2011-10-02 02:30:33 -04:00
|
|
|
:private
|
|
|
|
else
|
|
|
|
:none
|
|
|
|
end
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [String] A representation of the method's signature, including its
|
|
|
|
# name and parameters. Optional and "rest" parameters are marked with `*`
|
|
|
|
# and block parameters with `&`. If the parameter names are unavailable,
|
|
|
|
# they're given numbered names instead.
|
|
|
|
# Paraphrased from `awesome_print` gem.
|
2011-09-19 02:42:31 -04:00
|
|
|
def signature
|
2011-09-25 01:51:06 -04:00
|
|
|
if respond_to?(:parameters)
|
|
|
|
args = parameters.inject([]) do |arr, (type, name)|
|
2011-09-19 02:42:31 -04:00
|
|
|
name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
|
|
|
|
arr << case type
|
2011-10-28 02:37:14 -04:00
|
|
|
when :req then name.to_s
|
|
|
|
when :opt then "#{name}=?"
|
|
|
|
when :rest then "*#{name}"
|
|
|
|
when :block then "&#{name}"
|
2011-09-19 02:42:31 -04:00
|
|
|
else '?'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
args = (1..arity.abs).map { |i| "arg#{i}" }
|
|
|
|
args[-1] = "*#{args[-1]}" if arity < 0
|
|
|
|
end
|
|
|
|
|
|
|
|
"#{name}(#{args.join(', ')})"
|
|
|
|
end
|
|
|
|
|
2011-10-16 01:07:52 -04:00
|
|
|
# @return [Pry::Method, nil] The wrapped method that is called when you
|
2011-10-14 03:02:55 -04:00
|
|
|
# use "super" in the body of this method.
|
|
|
|
def super(times=1)
|
2012-02-01 02:04:52 -05:00
|
|
|
if UnboundMethod === @method
|
|
|
|
sup = super_using_ancestors(Pry::Method.instance_resolution_order(owner), times)
|
|
|
|
else
|
2011-10-14 03:02:55 -04:00
|
|
|
sup = super_using_ancestors(Pry::Method.resolution_order(receiver), times)
|
|
|
|
sup &&= sup.bind(receiver)
|
|
|
|
end
|
|
|
|
Pry::Method.new(sup) if sup
|
|
|
|
end
|
|
|
|
|
2011-11-26 21:12:34 -05:00
|
|
|
# @return [String, nil] The original name the method was defined under,
|
2011-10-16 01:07:52 -04:00
|
|
|
# before any aliasing, or `nil` if it can't be determined.
|
|
|
|
def original_name
|
|
|
|
return nil if source_type != :ruby
|
2011-11-26 21:33:13 -05:00
|
|
|
method_name_from_first_line(source.lines.first)
|
2011-10-16 01:07:52 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [Boolean] Was the method defined outside a source file?
|
2011-09-19 02:42:31 -04:00
|
|
|
def dynamically_defined?
|
2011-10-02 04:25:49 -04:00
|
|
|
!!(source_file and source_file =~ /(\(.*\))|<.*>/)
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# @return [Boolean] Was the method defined within the Pry REPL?
|
2011-09-19 02:42:31 -04:00
|
|
|
def pry_method?
|
|
|
|
source_file == Pry.eval_path
|
|
|
|
end
|
|
|
|
|
Retouch `#aliases` so it returns Array<String>
Also, refactor tests for `Method#aliases` and use a neat trick with
Sets, when matching two arrays.
# Meh.
[:a, :b, :c] == [:c, :b, :a] # => false
# Yarr!
Set.new([:a, :b, :c]) == Set.new([:c, :b, :a]) # => true
Last but not least, this commit fixes build errors on different rubies
(well, I hope so!).
Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-08-27 19:08:05 -04:00
|
|
|
# @return [Array<String>] All known aliases for the method.
|
|
|
|
# @note On Ruby 1.8 this method always returns an empty Array for methods
|
|
|
|
# implemented in C.
|
2012-08-25 17:02:39 -04:00
|
|
|
def aliases
|
|
|
|
owner = @method.owner
|
|
|
|
# Avoid using `to_sym` on {Method#name}, which returns a `String`, because
|
|
|
|
# it won't be garbage collected.
|
|
|
|
name = @method.name
|
|
|
|
|
|
|
|
alias_list = owner.instance_methods.combination(2).select do |pair|
|
2012-08-29 03:04:40 -04:00
|
|
|
pair.include?(name) &&
|
|
|
|
owner.instance_method(pair.first) == owner.instance_method(pair.last)
|
2012-08-25 17:02:39 -04:00
|
|
|
end.flatten
|
|
|
|
alias_list.delete(name)
|
|
|
|
|
Retouch `#aliases` so it returns Array<String>
Also, refactor tests for `Method#aliases` and use a neat trick with
Sets, when matching two arrays.
# Meh.
[:a, :b, :c] == [:c, :b, :a] # => false
# Yarr!
Set.new([:a, :b, :c]) == Set.new([:c, :b, :a]) # => true
Last but not least, this commit fixes build errors on different rubies
(well, I hope so!).
Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-08-27 19:08:05 -04:00
|
|
|
alias_list.map(&:to_s)
|
2012-08-25 17:02:39 -04:00
|
|
|
end
|
|
|
|
|
2011-10-16 01:07:52 -04:00
|
|
|
# @return [Boolean] Is the method definitely an alias?
|
|
|
|
def alias?
|
|
|
|
name != original_name
|
|
|
|
end
|
|
|
|
|
2011-10-02 00:14:32 -04:00
|
|
|
# @return [Boolean]
|
2011-09-19 02:42:31 -04:00
|
|
|
def ==(obj)
|
|
|
|
if obj.is_a? Pry::Method
|
2012-04-01 22:24:23 -04:00
|
|
|
obj == @method
|
2011-09-19 02:42:31 -04:00
|
|
|
else
|
|
|
|
@method == obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-02 00:14:32 -04:00
|
|
|
# @param [Class] klass
|
|
|
|
# @return [Boolean]
|
2011-09-25 01:14:04 -04:00
|
|
|
def is_a?(klass)
|
|
|
|
klass == Pry::Method or @method.is_a?(klass)
|
|
|
|
end
|
|
|
|
alias kind_of? is_a?
|
|
|
|
|
2011-10-02 00:14:32 -04:00
|
|
|
# @param [String, Symbol] method_name
|
|
|
|
# @return [Boolean]
|
2011-09-25 01:51:06 -04:00
|
|
|
def respond_to?(method_name)
|
|
|
|
super or @method.respond_to?(method_name)
|
|
|
|
end
|
|
|
|
|
2011-10-02 04:25:49 -04:00
|
|
|
# Delegate any unknown calls to the wrapped method.
|
2011-09-19 02:42:31 -04:00
|
|
|
def method_missing(method_name, *args, &block)
|
|
|
|
@method.send(method_name, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2013-01-30 03:01:24 -05:00
|
|
|
|
|
|
|
# @return [YARD::CodeObjects::MethodObject]
|
|
|
|
# @raise [CommandError] Raises when the method can't be found or `pry-doc` isn't installed.
|
|
|
|
def pry_doc_info
|
|
|
|
if Pry.config.has_pry_doc
|
|
|
|
Pry::MethodInfo.info_for(@method) or raise CommandError, "Cannot locate this method: #{name}. (source_location returns nil)"
|
|
|
|
else
|
|
|
|
raise CommandError, "Cannot locate this method: #{name}. Try `gem install pry-doc` to get access to Ruby Core documentation."
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
2013-01-30 03:01:24 -05:00
|
|
|
end
|
2011-09-19 02:42:31 -04:00
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
# FIXME: a very similar method to this exists on WrappedModule: extract_doc_for_candidate
|
|
|
|
def doc_for_pry_method
|
|
|
|
_, line_num = source_location
|
2012-04-17 07:26:45 -04:00
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
buffer = ""
|
|
|
|
Pry.line_buffer[0..(line_num - 1)].each do |line|
|
|
|
|
# Add any line that is a valid ruby comment,
|
|
|
|
# but clear as soon as we hit a non comment line.
|
|
|
|
if (line =~ /^\s*#/) || (line =~ /^\s*$/)
|
|
|
|
buffer << line.lstrip
|
|
|
|
else
|
|
|
|
buffer.replace("")
|
2012-04-17 07:26:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
buffer
|
|
|
|
end
|
2012-12-20 11:28:00 -05:00
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
# @param [Class, Module] ancestors The ancestors to investigate
|
|
|
|
# @return [Method] The unwrapped super-method
|
|
|
|
def super_using_ancestors(ancestors, times=1)
|
|
|
|
next_owner = self.owner
|
|
|
|
times.times do
|
|
|
|
i = ancestors.index(next_owner) + 1
|
|
|
|
while ancestors[i] && !(ancestors[i].method_defined?(name) || ancestors[i].private_method_defined?(name))
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
next_owner = ancestors[i] or return nil
|
2011-10-14 03:02:55 -04:00
|
|
|
end
|
2011-11-26 21:33:13 -05:00
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
safe_send(next_owner, :instance_method, name) rescue nil
|
|
|
|
end
|
2011-11-26 21:33:13 -05:00
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
# @param [String] first_ln The first line of a method definition.
|
|
|
|
# @return [String, nil]
|
|
|
|
def method_name_from_first_line(first_ln)
|
|
|
|
return nil if first_ln.strip !~ /^def /
|
2011-11-26 21:33:13 -05:00
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
tokens = CodeRay.scan(first_ln, :ruby)
|
|
|
|
tokens = tokens.tokens.each_slice(2) if tokens.respond_to?(:tokens)
|
|
|
|
tokens.each_cons(2) do |t1, t2|
|
|
|
|
if t2.last == :method || t2.last == :ident && t1 == [".", :operator]
|
|
|
|
return t2.first
|
|
|
|
end
|
2011-11-26 21:33:13 -05:00
|
|
|
end
|
2011-12-03 23:21:02 -05:00
|
|
|
|
2013-01-30 03:01:24 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2011-12-03 23:21:02 -05:00
|
|
|
# A Disowned Method is one that's been removed from the class on which it was defined.
|
|
|
|
#
|
|
|
|
# e.g.
|
|
|
|
# class C
|
|
|
|
# def foo
|
|
|
|
# C.send(:undefine_method, :foo)
|
|
|
|
# Pry::Method.from_binding(binding)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# In this case we assume that the "owner" is the singleton class of the receiver.
|
|
|
|
#
|
|
|
|
# This occurs mainly in Sinatra applications.
|
|
|
|
class Disowned < Method
|
|
|
|
attr_reader :receiver, :name
|
|
|
|
|
|
|
|
# Create a new Disowned method.
|
|
|
|
#
|
|
|
|
# @param [Object] receiver
|
|
|
|
# @param [String] method_name
|
2012-06-27 01:30:00 -04:00
|
|
|
def initialize(receiver, method_name)
|
|
|
|
@receiver, @name = receiver, method_name
|
2011-12-03 23:21:02 -05:00
|
|
|
end
|
|
|
|
|
2011-12-20 10:24:35 -05:00
|
|
|
# Is the method undefined? (aka `Disowned`)
|
|
|
|
# @return [Boolean] true
|
|
|
|
def undefined?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-06-03 23:09:00 -04:00
|
|
|
# Can we get the source for this method?
|
|
|
|
# @return [Boolean] false
|
|
|
|
def source?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2011-12-03 23:21:02 -05:00
|
|
|
# Get the hypothesized owner of the method.
|
|
|
|
#
|
|
|
|
# @return [Object]
|
|
|
|
def owner
|
|
|
|
class << receiver; self; end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Raise a more useful error message instead of trying to forward to nil.
|
|
|
|
def method_missing(meth_name, *args, &block)
|
|
|
|
raise "Cannot call '#{meth_name}' on an undef'd method." if method(:name).respond_to?(meth_name)
|
|
|
|
Object.instance_method(:method_missing).bind(self).call(meth_name, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
2011-09-19 02:42:31 -04:00
|
|
|
end
|
|
|
|
end
|