๐Ÿ“ฆ AlanGabbianelli / Cake

๐Ÿ“„ multibuy_promotion.rb ยท 22 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class MultibuyPromotion # :nodoc:
  def initialize(item_code: 001, required_quantity: 2, discount_per_item: 0.75)
    @item_code = item_code
    @required_quantity = required_quantity
    @discount_per_item = discount_per_item.to_d
  end

  def apply(basket, _subtotal)
    promo_applicable?(basket) ? applicable_discount(basket) : 0
  end

  private

  def promo_applicable?(basket)
    basket[@item_code] >= @required_quantity
  end

  def applicable_discount(basket)
    basket[@item_code] * @discount_per_item
  end
end