Checkout now stores its items using a quantity and a link back to the original product instance. It has a reference to the store to make sure that product IDs added are valid
21 lines
353 B
Ruby
21 lines
353 B
Ruby
class Shop
|
|
class DuplicateProductIdError < StandardError; end
|
|
def initialize
|
|
@products = []
|
|
end
|
|
|
|
def products
|
|
@products
|
|
end
|
|
|
|
def add_product(product)
|
|
raise DuplicateProductIdError if product_by_id(product.id)
|
|
|
|
@products.push(product)
|
|
end
|
|
|
|
def product_by_id(id)
|
|
@products.find { |product| product.id == id }
|
|
end
|
|
end
|