Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial delegate (stdlib) implementation
- Loading branch information
1 parent
a74ba83
commit fc58796
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require File.expand_path('../../../../spec_helper', __FILE__) | ||
require File.expand_path('../../fixtures/classes', __FILE__) | ||
|
||
describe "SimpleDelegator.new" do | ||
before :all do | ||
@simple = DelegateSpecs::Simple.new | ||
@delegate = SimpleDelegator.new(@simple) | ||
end | ||
|
||
it "forwards public method calls" do | ||
@delegate.pub.should == :foo | ||
end | ||
|
||
ruby_version_is ""..."2.0" do | ||
it "forwards protected method calls" do | ||
@delegate.prot.should == :protected | ||
end | ||
end | ||
|
||
ruby_version_is "2.0" do | ||
it "forwards protected method calls" do | ||
lambda{ @delegate.prot }.should raise_error( NoMethodError ) | ||
end | ||
end | ||
|
||
it "doesn't forward private method calls" do | ||
lambda{ @delegate.priv }.should raise_error( NoMethodError ) | ||
end | ||
|
||
it "doesn't forward private method calls even via send or __send__" do | ||
lambda{ @delegate.send(:priv, 42) }.should raise_error( NoMethodError ) | ||
lambda{ @delegate.__send__(:priv, 42) }.should raise_error( NoMethodError ) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'delegate' | ||
module DelegateSpecs | ||
class Simple | ||
def pub | ||
:foo | ||
end | ||
|
||
def respond_to_missing?(method, priv=false) | ||
method == :pub_too || | ||
(priv && method == :priv_too) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Delegator < BasicObject | ||
def initialize(obj) | ||
__setobj__(obj) | ||
end | ||
|
||
def method_missing(m, *args, &block) | ||
target = self.__getobj__ | ||
|
||
if target.respond_to?(m) | ||
target.__send__(m, *args, &block) | ||
else | ||
super(m, *args, &block) | ||
end | ||
end | ||
end | ||
|
||
class SimpleDelegator < Delegator | ||
def __getobj__ | ||
@delegate_sd_obj | ||
end | ||
|
||
def __setobj__(obj) | ||
@delegate_sd_obj = obj | ||
end | ||
end |