mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
added ability to extract an implicit object from binding when no string is given
This commit is contained in:
parent
bbd4df87d4
commit
405aacf76d
2 changed files with 69 additions and 4 deletions
|
@ -1,10 +1,13 @@
|
|||
class Pry
|
||||
class CodeObject
|
||||
include Helpers::CommandHelpers
|
||||
|
||||
class << self
|
||||
def lookup(str, target, _pry_, options={})
|
||||
co = new(str, target, _pry_, options)
|
||||
co.other_object || co.method_or_class || co.command
|
||||
|
||||
co.default_lookup || co.method_or_class_lookup ||
|
||||
co.command_lookup || co.binding_lookup
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -24,11 +27,24 @@ class Pry
|
|||
@super_level = options[:super]
|
||||
end
|
||||
|
||||
def command
|
||||
def command_lookup
|
||||
pry.commands[str]
|
||||
end
|
||||
|
||||
def other_object
|
||||
# extract the object from the binding
|
||||
def binding_lookup
|
||||
return nil if str && !str.empty?
|
||||
|
||||
if internal_binding?(target)
|
||||
mod = target_self.is_a?(Module) ? target_self : target_self.class
|
||||
Pry::WrappedModule(mod)
|
||||
else
|
||||
Pry::Method.from_binding(target)
|
||||
end
|
||||
end
|
||||
|
||||
# lookup variables and constants that are not modules
|
||||
def default_lookup
|
||||
if target.eval("defined? #{str} ") =~ /variable|constant/
|
||||
obj = target.eval(str)
|
||||
|
||||
|
@ -45,7 +61,13 @@ class Pry
|
|||
nil
|
||||
end
|
||||
|
||||
def method_or_class
|
||||
def method_or_class_lookup
|
||||
# we need this here because stupid Pry::Method.from_str() does a
|
||||
# Pry::Method.from_binding when str is nil.
|
||||
# Once we refactor Pry::Method.from_str() so it doesnt lookup
|
||||
# from bindings, we can get rid of this check
|
||||
return nil if !str || str.empty?
|
||||
|
||||
obj = if str =~ /::(?:\S+)\Z/
|
||||
Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
|
||||
else
|
||||
|
@ -59,5 +81,10 @@ class Pry
|
|||
sup
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def target_self
|
||||
target.eval('self')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,6 +10,7 @@ describe Pry::CodeObject do
|
|||
|
||||
class ClassyWassy
|
||||
def piggy
|
||||
binding
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -53,6 +54,43 @@ describe Pry::CodeObject do
|
|||
m.is_a?(Pry::WrappedModule).should == true
|
||||
m.source.should =~ /piggy/
|
||||
end
|
||||
|
||||
describe "inferring object from binding when lookup str is empty/nil" do
|
||||
before do
|
||||
@b1 = Pry.binding_for(ClassyWassy)
|
||||
@b2 = Pry.binding_for(ClassyWassy.new)
|
||||
end
|
||||
|
||||
describe "infer module objects" do
|
||||
it 'should infer module object when binding self is a module' do
|
||||
["", nil].each do |v|
|
||||
m = Pry::CodeObject.lookup(v, @b1, Pry.new)
|
||||
m.is_a?(Pry::WrappedModule).should == true
|
||||
m.name.should =~ /ClassyWassy/
|
||||
end
|
||||
end
|
||||
|
||||
it 'should infer module object when binding self is an instance' do
|
||||
["", nil].each do |v|
|
||||
m = Pry::CodeObject.lookup(v, @b2, Pry.new)
|
||||
m.is_a?(Pry::WrappedModule).should == true
|
||||
m.name.should =~ /ClassyWassy/
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "infer method objects" do
|
||||
it 'should infer method object from binding when inside method context' do
|
||||
b = ClassyWassy.new.piggy
|
||||
|
||||
["", nil].each do |v|
|
||||
m = Pry::CodeObject.lookup(v, b, Pry.new)
|
||||
m.is_a?(Pry::Method).should == true
|
||||
m.name.should =~ /piggy/
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "lookups with :super" do
|
||||
|
|
Loading…
Add table
Reference in a new issue