Class: FinancialCalculator::Pmt

Inherits:
Object
  • Object
show all
Includes:
Validator
Defined in:
lib/financial_calculator/pmt.rb

Overview

Calculates the payment of an ordinary annuity

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate, num_periods, present_value, future_value = 0, pay_at_beginning = false) ⇒ FinancialCalculator::Npv

Create a new object for calculating the periodic payment of an ordinary annuity

Parameters:

  • rate (Numeric)

    The discount (interest) rate

  • num_periods (Numeric)

    The number of payments to be made

  • present_value (Numeric)

    The current value of the annuity

  • future_value (Numeric) (defaults to: 0)

    The ending value of the annuity

  • pay_at_beginning. (Boolean)

    Whether the payment is made at the beginning of the period (true) or end of the period (false)



39
40
41
42
43
44
45
46
47
48
# File 'lib/financial_calculator/pmt.rb', line 39

def initialize(rate, num_periods, present_value, future_value = 0, pay_at_beginning = false)
  validate_numerics(rate: rate, num_periods: num_periods, present_value: present_value, future_value: future_value)

  @rate             = Flt::DecNum(rate.to_s)
  @num_periods      = Flt::DecNum(num_periods.to_s)
  @present_value    = Flt::DecNum(present_value.to_s)
  @future_value     = Flt::DecNum(future_value || "0")
  @pay_at_beginning = pay_at_beginning || false
  @result           = solve(@rate, @num_periods, @present_value, @future_value, @pay_at_beginning)
end

Instance Attribute Details

#future_valueNumeric (readonly)

Returns The ending value of the annuity. Defaults to 0.

Returns:

  • (Numeric)

    The ending value of the annuity. Defaults to 0



20
21
22
# File 'lib/financial_calculator/pmt.rb', line 20

def future_value
  @future_value
end

#num_periodsNumeric (readonly)

Returns The number of payments to be made.

Returns:

  • (Numeric)

    The number of payments to be made



12
13
14
# File 'lib/financial_calculator/pmt.rb', line 12

def num_periods
  @num_periods
end

#pay_at_beginningBoolean (readonly)

Returns Whether the payment is made at the beginning of the period (true) or end of the period (false).

Returns:

  • (Boolean)

    Whether the payment is made at the beginning of the period (true) or end of the period (false)



25
26
27
# File 'lib/financial_calculator/pmt.rb', line 25

def pay_at_beginning
  @pay_at_beginning
end

#present_valueNumeric (readonly)

Returns The current value of the annuity.

Returns:

  • (Numeric)

    The current value of the annuity



16
17
18
# File 'lib/financial_calculator/pmt.rb', line 16

def present_value
  @present_value
end

#rateNumeric (readonly)

Returns The rate used for calculating the payment amount.

Returns:

  • (Numeric)

    The rate used for calculating the payment amount



8
9
10
# File 'lib/financial_calculator/pmt.rb', line 8

def rate
  @rate
end

#resultDecNum (readonly)

Returns Result of the PMT calculation.

Returns:

  • (DecNum)

    Result of the PMT calculation



29
30
31
# File 'lib/financial_calculator/pmt.rb', line 29

def result
  @result
end

Instance Method Details

#inspectObject



56
57
58
# File 'lib/financial_calculator/pmt.rb', line 56

def inspect
  "PMT(#{result})"
end

#pays_at_beginning?Boolean

Returns Whether the payments are made at the beginning of each period.

Returns:

  • (Boolean)

    Whether the payments are made at the beginning of each period



52
53
54
# File 'lib/financial_calculator/pmt.rb', line 52

def pays_at_beginning?
  @pay_at_beginning
end