Permalink
Browse files

Adds basic functionality for DumbDelegator

  • Loading branch information...
1 parent a68ba16 commit e09832a3a4563da0febffa1b157d3ea87bf6ae09 @alindeman alindeman committed Mar 20, 2012
Showing with 76 additions and 3 deletions.
  1. +1 −0 .rspec
  2. +1 −0 dumb_delegator.gemspec
  3. +20 −2 lib/dumb_delegator.rb
  4. +1 −1 lib/dumb_delegator/version.rb
  5. +51 −0 spec/dumb_delegator_spec.rb
  6. +2 −0 spec/spec_helper.rb
View
1 .rspec
@@ -1,2 +1,3 @@
--color
--format progress
+--order rand
View
@@ -18,4 +18,5 @@ Gem::Specification.new do |gem|
gem.version = DumbDelegator::VERSION
gem.add_development_dependency 'rspec', '~>2.9.0'
+ gem.add_development_dependency 'pry'
end
View
@@ -1,5 +1,23 @@
require "dumb_delegator/version"
-module DumbDelegator
- # Your code goes here...
+class DumbDelegator
+ def initialize(target)
+ __setobj__(target)
+ end
+
+ def method_missing(method, *args, &block)
+ if @__dumb_target__.respond_to?(method)
+ @__dumb_target__.__send__(method, *args, &block)
+ else
+ super
+ end
+ end
+
+ def __getobj__
+ @__dumb_target__
+ end
+
+ def __setobj__(obj)
+ @__dumb_target__ = obj
+ end
end
@@ -1,3 +1,3 @@
-module DumbDelegator
+class DumbDelegator
VERSION = "0.0.1"
end
@@ -0,0 +1,51 @@
+require "spec_helper"
+
+describe DumbDelegator do
+ let(:target) { double }
+ subject { described_class.new(target) }
+
+ it "delegates to the target object" do
+ target.should_receive(:foo)
+ subject.foo
+ end
+
+ it "delegates to the target object with arguments" do
+ target.should_receive(:foo).with(:bar)
+
+ subject.foo(:bar)
+ end
+
+ it "delegates to the target object with a block" do
+ bar_block = proc { "bar" }
+ target.should_receive(:foo) { |&block| block.should == bar_block }
+
+ subject.foo(&bar_block)
+ end
+
+ it "does not delegate if the target does not respond_to? the message" do
+ target.stub(:foo)
+ target.stub(:respond_to?).with(:foo).and_return(false)
+
+ expect {
+ subject.foo
+ }.to raise_error(NoMethodError)
+ end
+
+ describe "#__getobj__" do
+ it "returns the target object" do
+ subject.__getobj__.should equal target
+ end
+ end
+
+ describe "#__setobj__" do
+ it "resets the target object to a different object" do
+ target.should_receive(:foo).never
+
+ new_target = double
+ new_target.should_receive(:foo)
+
+ subject.__setobj__(new_target)
+ subject.foo
+ end
+ end
+end
View
@@ -7,6 +7,8 @@
require File.expand_path('../lib/dumb_delegator', File.dirname(__FILE__))
+require 'pry'
+
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true

0 comments on commit e09832a

Please sign in to comment.