class SimpleMarketplace::Checkout

Public Class Methods

new(*promotional_rules) click to toggle source
# File lib/simple_marketplace/checkout.rb, line 4
def initialize(*promotional_rules)
  @promotional_rules = promotional_rules
  @items = []
end

Public Instance Methods

clear_items() click to toggle source

Restart the cart

# File lib/simple_marketplace/checkout.rb, line 29
def clear_items
  @items = []
end
pretty_total() click to toggle source

Shows the formated total

# File lib/simple_marketplace/checkout.rb, line 24
def pretty_total
  "£#{'%.2f' % total}"
end
scan(product_code) click to toggle source

Adds an item to the cart

# File lib/simple_marketplace/checkout.rb, line 10
def scan(product_code)
  if (product = Product.get_by_code(product_code))
    @items << product
  end
end
total() click to toggle source

Calculates the total with promotions applied

# File lib/simple_marketplace/checkout.rb, line 17
def total
  @total = @items.map(&:price).reduce(:+)
  apply_promotional_rules
  @total
end

Private Instance Methods

apply_promotional_rules() click to toggle source

calls the promotional rules in order

# File lib/simple_marketplace/checkout.rb, line 36
def apply_promotional_rules
  @promotional_rules.each do |rule|
    @total = rule.call(@items, @total)
  end
end