public
Last active — forked from joshuaclayton/cream_and_sugar.rb

  • Download Gist
cream_and_sugar.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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
cream_and_sugar_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.