Skip to content

Instantly share code, notes, and snippets.

@orend
Forked from joshuaclayton/cream_and_sugar.rb
Created December 27, 2011 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orend/1523849 to your computer and use it in GitHub Desktop.
Save orend/1523849 to your computer and use it in GitHub Desktop.
require "money"
class Decorator < BasicObject
def initialize(component)
@component = component
end
def method_missing(name, *args, &block)
@component.send(name, *args, &block)
end
def send(symbol, *args)
__send__(symbol, *args)
end
def ==(other)
@component == other
end
end
class Cream < Decorator
def cost
@component.cost + ::Money.new(50, "USD")
end
end
class Sugar < Decorator
def cost
@component.cost + ::Money.new(20, "USD")
end
end
class Coffee
def cost
::Money.new(200, "USD")
end
def origin
"Columbia"
end
end
describe Coffee do
its(:cost) { should == 2 }
its(:origin) { should == "Columbia" }
it { should be_an_instance_of(Coffee) }
end
describe "Coffee with Sugar" do
let(:coffee) { Coffee.new }
subject { Sugar.new(coffee) }
it { (subject == coffee).should be_true }
its(:cost) { should == 2.2 }
its(:origin) { should == "Columbia" }
its(:object_id) { should == coffee.object_id }
its(:class) { should == coffee.class }
it { should be_an_instance_of(Coffee) }
end
describe "Coffee with two Sugar" do
let(:coffee) { Coffee.new }
subject { Sugar.new(Sugar.new(coffee)) }
it { (subject == coffee).should be_true }
its(:cost) { should == 2.4 }
its(:origin) { should == "Columbia" }
its(:object_id) { should == coffee.object_id }
its(:class) { should == coffee.class }
it { should be_an_instance_of(Coffee) }
end
describe "Coffee with Sugar and Cream" do
let(:coffee) { Coffee.new }
subject { Sugar.new(Cream.new(coffee)) }
it { (subject == coffee).should be_true }
its(:cost) { should == 2.7 }
its(:origin) { should == "Columbia" }
its(:object_id) { should == coffee.object_id }
its(:class) { should == coffee.class }
it { should be_an_instance_of(Coffee) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment