Class: Refinery::Cart

Inherits:
Object
  • Object
show all
Defined in:
lib/refinery/cart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = "Your") ⇒ Cart

Returns a new instance of Cart.



9
10
11
12
# File 'lib/refinery/cart.rb', line 9

def initialize( store="Your" )
  @items = []
  @store_name = store
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/refinery/cart.rb', line 7

def items
  @items
end

#store_nameObject (readonly)

Returns the value of attribute store_name.



7
8
9
# File 'lib/refinery/cart.rb', line 7

def store_name
  @store_name
end

Instance Method Details

#add_product(product) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/refinery/cart.rb', line 14

def add_product(product)
  current_item = @items.find {|item| item.product == product}
  if current_item
    current_item.increment_quantity
  else
    current_item = ::Refinery::CartItem.new(product)
    @items << current_item
  end
  current_item
end

#total_itemsObject



26
27
28
# File 'lib/refinery/cart.rb', line 26

def total_items
  @items.sum { |item| item.quantity }
end

#total_priceObject



31
32
33
# File 'lib/refinery/cart.rb', line 31

def total_price
  @items.sum { |item| item.price }
end