Skip to content

@orend /cream_and_sugar.rb forked from joshuaclayton/cream_and_sugar.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.