Class: SimpleMarketplace::Offer::ProductDiscountByAmount

Inherits:
Object
  • Object
show all
Includes:
SimpleMarketplace::Offerable
Defined in:
lib/simple_marketplace/offer/product_discount_by_amount.rb

Instance Method Summary collapse

Constructor Details

#initialize(product_code, initial_amount, new_value) ⇒ ProductDiscountByAmount

Returns a new instance of ProductDiscountByAmount.



5
6
7
# File 'lib/simple_marketplace/offer/product_discount_by_amount.rb', line 5

def initialize(product_code, initial_amount, new_value)
  @product_code, @initial_amount, @new_value = product_code, initial_amount, new_value
end

Instance Method Details

#call(items, total) ⇒ Object

Verify if the product amount is the desired to change the product value



10
11
12
13
14
15
16
# File 'lib/simple_marketplace/offer/product_discount_by_amount.rb', line 10

def call(items, total)
  if items.count{ |product| product.code == @product_code } >= @initial_amount
    items.collect{|product| product.code == @product_code ? @new_value : product.price }.reduce(:+)
  else
    total
  end
end