mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
add Pry::WrappedModule#super
It's useful having Pry::WrappedModule and Pry::Method share as much interface as possible so that methods can be indifferent to which one they get (a la show-source / edit)
This commit is contained in:
parent
7449821657
commit
f2d6a926f9
2 changed files with 73 additions and 1 deletions
|
@ -204,6 +204,24 @@ class Pry
|
|||
!!(defined?(YARD) && YARD::Registry.at(name))
|
||||
end
|
||||
|
||||
# @param [Fixnum] times How far to travel up the ancestor chain.
|
||||
# @return [Pry::WrappedModule, nil] The wrapped module that is the
|
||||
# superclass.
|
||||
# When `self` is a `Module` then return the
|
||||
# nth ancestor, otherwise (in the case of classes) return the
|
||||
# nth ancestor that is a class.
|
||||
def super(times=1)
|
||||
return self if times.zero?
|
||||
|
||||
if wrapped.is_a?(Class)
|
||||
sup = ancestors.select { |v| v.is_a?(Class) }[times]
|
||||
else
|
||||
sup = ancestors[times]
|
||||
end
|
||||
|
||||
Pry::WrappedModule(sup) if sup
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# @return [Pry::WrappedModule::Candidate] The candidate of rank 0,
|
||||
|
|
|
@ -168,5 +168,59 @@ describe Pry::WrappedModule do
|
|||
Pry::WrappedModule.new(class << Object; self; end).singleton_instance.should.equal?(Object)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ".super" do
|
||||
describe "receiver is a class" do
|
||||
before do
|
||||
@a = Class.new
|
||||
@m = Module.new
|
||||
@b = Class.new(@a)
|
||||
@b.send(:include, @m)
|
||||
@c = Class.new(@b)
|
||||
end
|
||||
|
||||
it 'should return superclass for a wrapped class' do
|
||||
Pry::WrappedModule(@c).super.wrapped.should == @b
|
||||
end
|
||||
|
||||
it 'should return nth superclass for a wrapped class' do
|
||||
d = Class.new(@c)
|
||||
Pry::WrappedModule(d).super(2).wrapped.should == @b
|
||||
end
|
||||
|
||||
it 'should ignore modules when retrieving nth superclass' do
|
||||
Pry::WrappedModule(@c).super(2).wrapped.should == @a
|
||||
end
|
||||
|
||||
it 'should return nil when no nth superclass exists' do
|
||||
Pry::WrappedModule(@c).super(10).should == nil
|
||||
end
|
||||
|
||||
it 'should return self when .super(0) is used' do
|
||||
c = Pry::WrappedModule(@c)
|
||||
c.super(0).should == c
|
||||
end
|
||||
end
|
||||
|
||||
describe "receiver is a module" do
|
||||
before do
|
||||
@m1 = Module.new
|
||||
@m2 = Module.new.tap { |v| v.send(:include, @m1) }
|
||||
@m3 = Module.new.tap { |v| v.send(:include, @m2) }
|
||||
end
|
||||
|
||||
it 'should not ignore modules when retrieving supers' do
|
||||
Pry::WrappedModule(@m3).super.wrapped.should == @m2
|
||||
end
|
||||
|
||||
it 'should retrieve nth super' do
|
||||
Pry::WrappedModule(@m3).super(2).wrapped.should == @m1
|
||||
end
|
||||
|
||||
it 'should return self when .super(0) is used' do
|
||||
m = Pry::WrappedModule(@m1)
|
||||
m.super(0).should == m
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue