Class: FinancialCalculator::Ppmt

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

Overview

Calculates the principal portion of a loan or annuity for a particular period

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate, period, 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

  • period (Numeric)

    The amortization period

  • 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)



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/financial_calculator/ppmt.rb', line 44

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

  @rate             = Flt::DecNum(rate.to_s)
  @period           = Flt::DecNum(period)
  @num_periods      = Flt::DecNum(num_periods.to_s)
  @present_value    = Flt::DecNum(present_value.to_s)
  @future_value     = Flt::DecNum(future_value.to_s)
  @pay_at_beginning = pay_at_beginning
  @result           = solve(@rate, @period, @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



24
25
26
# File 'lib/financial_calculator/ppmt.rb', line 24

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



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

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)



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

def pay_at_beginning
  @pay_at_beginning
end

#periodNumeric (readonly)

Returns The amortization period.

Returns:

  • (Numeric)

    The amortization period



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

def period
  @period
end

#present_valueNumeric (readonly)

Returns The current value of the annuity.

Returns:

  • (Numeric)

    The current value of the annuity



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

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/ppmt.rb', line 8

def rate
  @rate
end

#resultDecNum (readonly)

Returns Result of the PMT calculation.

Returns:

  • (DecNum)

    Result of the PMT calculation



33
34
35
# File 'lib/financial_calculator/ppmt.rb', line 33

def result
  @result
end

Instance Method Details

#inspectObject



62
63
64
# File 'lib/financial_calculator/ppmt.rb', line 62

def inspect
  "PPMT(#{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



58
59
60
# File 'lib/financial_calculator/ppmt.rb', line 58

def pays_at_beginning?
  @pay_at_beginning
end