Class: Workarea::Pricing::Discount::AmountCalculator

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • the (Money)

    price to be discounted

Returns:



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.

Returns:

  • (Boolean)


28
29
30
# File 'app/models/workarea/pricing/discount/amount_calculator.rb', line 28

def flat?
  amount_type == :flat
end

#percentFloat

The percent representation of the amount off.

Returns:



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.

Returns:

  • (Boolean)


19
20
21
# File 'app/models/workarea/pricing/discount/amount_calculator.rb', line 19

def percent?
  amount_type == :percent
end