Add ProductProxy which wraps the product's variants in VariantProxys

This commit is contained in:
Rohan Mitchell
2014-11-13 17:34:31 +11:00
parent f3fa5edb9d
commit 2b0f6b7865
2 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
module OpenFoodNetwork
class ProductProxy
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
def initialize(product, hub)
@product = product
@hub = hub
end
def variants
@product.variants.map { |v| VariantProxy.new(v, @hub) }
end
def method_missing(name, *args, &block)
@product.send(name, *args, &block)
end
end
end

View File

@@ -0,0 +1,27 @@
require 'open_food_network/product_proxy'
require 'open_food_network/variant_proxy'
module OpenFoodNetwork
describe ProductProxy do
let(:hub) { double(:hub) }
let(:p) { double(:product, name: 'name') }
let(:pp) { ProductProxy.new(p, hub) }
describe "delegating calls to proxied product" do
it "delegates name" do
pp.name.should == 'name'
end
end
describe "fetching the variants" do
let(:v1) { double(:variant) }
let(:v2) { double(:variant) }
let(:p) { double(:product, variants: [v1, v2]) }
it "returns variants wrapped in VariantProxy" do
# #class is proxied too, so we test that it worked by #object_id
pp.variants.map(&:object_id).sort.should_not == [v1.object_id, v2.object_id].sort
end
end
end
end