Class: Basket

Inherits:
Object
  • Object
show all
Defined in:
lib/wunder/basket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasket

Returns a new instance of Basket.



7
8
9
# File 'lib/wunder/basket.rb', line 7

def initialize
  @items = ItemCollection.new
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



5
6
7
# File 'lib/wunder/basket.rb', line 5

def items
  @items
end

Instance Method Details

#add_item(product) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/wunder/basket.rb', line 11

def add_item(product)
  item = items.find_product(product.product_code)
  if item.nil?
    items.push(BasketItem.new(product))
  else
    item.increment_item_quantity
  end
end

#items_in_basketObject



30
31
32
33
34
35
36
37
# File 'lib/wunder/basket.rb', line 30

def items_in_basket
  products = []
  items.each do |item|
    products << "#{item.product.product_code} - #{item.product.name}"
  end

  products.join(" , ")
end

#remove_item(product) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/wunder/basket.rb', line 20

def remove_item(product)
  item = items.find_product(product.product_code)
  return if item.nil?
  if item.quantity == 1
    items.delete(item)
  else
    item.decrement_item_quantity
  end
end