Class: SimpleMarketplace::Checkout

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_marketplace/checkout.rb

Instance Method Summary collapse

Constructor Details

#initialize(*promotional_rules) ⇒ Checkout

Returns a new instance of Checkout.



4
5
6
7
# File 'lib/simple_marketplace/checkout.rb', line 4

def initialize(*promotional_rules)
  @promotional_rules = promotional_rules
  @items = []
end

Instance Method Details

#clear_itemsObject

Restart the cart



29
30
31
# File 'lib/simple_marketplace/checkout.rb', line 29

def clear_items
  @items = []
end

#pretty_totalObject

Shows the formated total



24
25
26
# File 'lib/simple_marketplace/checkout.rb', line 24

def pretty_total
  "£#{'%.2f' % total}"
end

#scan(product_code) ⇒ Object

Adds an item to the cart



10
11
12
13
14
# File 'lib/simple_marketplace/checkout.rb', line 10

def scan(product_code)
  if (product = Product.get_by_code(product_code))
    @items << product
  end
end

#totalObject

Calculates the total with promotions applied



17
18
19
20
21
# File 'lib/simple_marketplace/checkout.rb', line 17

def total
  @total = @items.map(&:price).reduce(:+)
  apply_promotional_rules
  @total
end