Class: Workarea::Pricing::Discount::AmountCalculator
- Inherits:
-
Object
- Object
- Workarea::Pricing::Discount::AmountCalculator
- Defined in:
- app/models/workarea/pricing/discount/amount_calculator.rb
Overview
This class coordinates with a FlatOrPercentOff discount to calculate how much should be given against a certin amount off.
Instance Method Summary collapse
-
#calculate(price, quantity = 1) ⇒ Money
Calculate the amount off of the passed price.
-
#flat? ⇒ Boolean
Whether this calculator is based on a flat amount off the price.
-
#initialize(discount) ⇒ AmountCalculator
constructor
A new instance of AmountCalculator.
-
#percent ⇒ Float
The percent representation of the amount off.
-
#percent? ⇒ Boolean
Whether this calculator is based on percent off the price.
Constructor Details
#initialize(discount) ⇒ AmountCalculator
Returns a new instance of AmountCalculator.
10 11 12 |
# File 'app/models/workarea/pricing/discount/amount_calculator.rb', line 10 def initialize(discount) @discount = discount end |
Instance Method Details
#calculate(price, quantity = 1) ⇒ Money
Calculate the amount off of the passed price.
45 46 47 48 49 50 51 52 53 |
# File 'app/models/workarea/pricing/discount/amount_calculator.rb', line 45 def calculate(price, quantity = 1) if percent? price * quantity * percent elsif price.to_m < amount.to_m price * quantity else amount.to_m * quantity end end |
#flat? ⇒ Boolean
Whether this calculator is based on a flat amount off the price.
28 29 30 |
# File 'app/models/workarea/pricing/discount/amount_calculator.rb', line 28 def flat? amount_type == :flat end |
#percent ⇒ Float
The percent representation of the amount off.
36 37 38 |
# File 'app/models/workarea/pricing/discount/amount_calculator.rb', line 36 def percent amount / 100 end |
#percent? ⇒ Boolean
Whether this calculator is based on percent off the price.
19 20 21 |
# File 'app/models/workarea/pricing/discount/amount_calculator.rb', line 19 def percent? amount_type == :percent end |