Add simple variant proxy

This commit is contained in:
Rohan Mitchell
2014-11-13 17:14:29 +11:00
parent f9b4c07219
commit f3fa5edb9d
2 changed files with 46 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
module OpenFoodNetwork
class VariantProxy
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
def initialize(variant, hub)
@variant = variant
@hub = hub
end
def price
VariantOverride.price_for(@variant, @hub) || @variant.price
end
def method_missing(name, *args, &block)
@variant.send(name, *args, &block)
end
end
end

View File

@@ -0,0 +1,27 @@
require 'open_food_network/variant_proxy'
module OpenFoodNetwork
describe VariantProxy do
let(:hub) { double(:hub) }
let(:v) { double(:variant, sku: 'sku123', price: 'global price') }
let(:vp) { VariantProxy.new(v, hub) }
describe "delegating calls to proxied variant" do
it "delegates sku" do
vp.sku.should == 'sku123'
end
end
describe "looking up the price" do
it "returns the override price when there is one" do
VariantOverride.stub(:price_for) { 'override price' }
vp.price.should == 'override price'
end
it "returns the global price otherwise" do
VariantOverride.stub(:price_for) { nil }
vp.price.should == 'global price'
end
end
end
end