Class: Finrb::Amortization::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/finrb/amortization.rb,
sig/finrb.rbs

Overview

Immutable breakdown of one amortization period. Payments retain finrb's cashflow sign convention and are negative; the other monetary fields are non-negative.

Instance Method Summary collapse

Constructor Details

#initialize(period:, opening_balance:, payment:, interest:, principal:, additional_payment:, balloon_payment:, interest_only:, closing_balance:) ⇒ Entry

Returns a new instance of Entry.

Parameters:

  • period: (Integer)
  • opening_balance: (number)
  • payment: (number)
  • interest: (number)
  • principal: (number)
  • additional_payment: (number)
  • balloon_payment: (number)
  • interest_only: (Boolean)
  • closing_balance: (number)

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/finrb/amortization.rb', line 32

def initialize(period:, opening_balance:, payment:, interest:, principal:, additional_payment:, balloon_payment:, interest_only:, closing_balance:)
  raise(ArgumentError, 'period must be a non-negative integer.') unless period.is_a?(Integer) && !period.negative?
  raise(ArgumentError, 'interest_only must be true or false.') unless [true, false].include?(interest_only)

  @period = period
  @interest_only = interest_only
  MONETARY_ATTRIBUTES.each do |name|
    value = binding.local_variable_get(name)
    instance_variable_set("@#{name}", Validation.decimal(value, name: name.to_s.tr('_', ' ')))
  end
  freeze
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • (Object)

Returns:

  • (Boolean)


45
46
47
# File 'lib/finrb/amortization.rb', line 45

def ==(other)
  other.instance_of?(self.class) && ATTRIBUTES.all? { |name| public_send(name) == other.public_send(name) }
end

#additional_paymentdecimal

Returns:

  • (decimal)


104
# File 'sig/finrb.rbs', line 104

def additional_payment: () -> decimal

#balloon_paymentdecimal

Returns:

  • (decimal)


105
# File 'sig/finrb.rbs', line 105

def balloon_payment: () -> decimal

#closing_balancedecimal

Returns:

  • (decimal)


108
# File 'sig/finrb.rbs', line 108

def closing_balance: () -> decimal

#hashInteger

Returns:

  • (Integer)


50
51
52
53
# File 'lib/finrb/amortization.rb', line 50

def hash
  attributes = ATTRIBUTES.map { |name| public_send(name) }
  attributes.hash
end

#interestdecimal

Returns:

  • (decimal)


102
# File 'sig/finrb.rbs', line 102

def interest: () -> decimal

#interest_onlyBoolean Also known as: interest_only?

Returns:

  • (Boolean)


106
# File 'sig/finrb.rbs', line 106

def interest_only: () -> bool

#opening_balancedecimal

Returns:

  • (decimal)


100
# File 'sig/finrb.rbs', line 100

def opening_balance: () -> decimal

#paymentdecimal

Returns:

  • (decimal)


101
# File 'sig/finrb.rbs', line 101

def payment: () -> decimal

#periodInteger

Returns:

  • (Integer)


99
# File 'sig/finrb.rbs', line 99

def period: () -> Integer

#principaldecimal

Returns:

  • (decimal)


103
# File 'sig/finrb.rbs', line 103

def principal: () -> decimal

#to_hHash[Symbol, Integer | decimal]

Returns:

  • (Hash[Symbol, Integer | decimal])


55
56
57
# File 'lib/finrb/amortization.rb', line 55

def to_h
  ATTRIBUTES.to_h { |name| [name, public_send(name)] }
end