Class: Hola::Cart

Inherits:
Object
  • Object
show all
Defined in:
lib/hola/cart.rb,
lib/hola/cart/item.rb,
lib/hola/cart/renderer.rb,
lib/hola/cart/item/processor.rb

Defined Under Namespace

Classes: Item, Renderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCart

Returns a new instance of Cart.



12
13
14
# File 'lib/hola/cart.rb', line 12

def initialize
  @items = Hash.new { Cart::Item.new }
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

Instance Method Details

#add(product_id:, quantity:) ⇒ Object



16
17
18
19
20
21
# File 'lib/hola/cart.rb', line 16

def add(product_id:, quantity:)
  items[product_id] = Cart::Item::Processor.new(
    product_id: product_id,
    quantity: items[product_id].quantity + quantity
  ).perform
end

#outputObject



27
28
29
# File 'lib/hola/cart.rb', line 27

def output
  items.map { |_k, item| item.output }
end

#totalObject



23
24
25
# File 'lib/hola/cart.rb', line 23

def total
  items.sum { |_k, item| item.subtotal }
end