Class: MudratProjector::MortgageAmortizer

Inherits:
CompoundInterestAmortizer show all
Defined in:
lib/mudrat_projector/amortizer.rb

Instance Attribute Summary collapse

Attributes inherited from CompoundInterestAmortizer

#schedule

Attributes inherited from Amortizer

#balance

Instance Method Summary collapse

Methods inherited from CompoundInterestAmortizer

#initial_value, #interest, #months_amortized, #next_transaction, #payment, #principal, #rate, #schedule_range

Constructor Details

#initialize(*args, extra_principal: 0) ⇒ MortgageAmortizer

Returns a new instance of MortgageAmortizer.



70
71
72
73
# File 'lib/mudrat_projector/amortizer.rb', line 70

def initialize *args, extra_principal: 0
  @extra_principal = extra_principal
  super
end

Instance Attribute Details

#extra_principalObject (readonly)

Returns the value of attribute extra_principal.



68
69
70
# File 'lib/mudrat_projector/amortizer.rb', line 68

def extra_principal
  @extra_principal
end

Instance Method Details

#amortizeObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mudrat_projector/amortizer.rb', line 86

def amortize
  interest_paid  = 0
  principal_paid = 0
  
  mp = monthly_payment

  new_balance = months_amortized.times.inject initial_value do |balance, _|
    interest    = balance * rate
    principal   = (mp - interest) + extra_principal
    interest_paid  += interest
    principal_paid += principal
    balance - principal
  end
  
  [new_balance, interest_paid, principal_paid]
end

#each_entry(&block) ⇒ Object



79
80
81
82
83
84
# File 'lib/mudrat_projector/amortizer.rb', line 79

def each_entry &block
  [[:credit, interest,  schedule.  ],
   [:credit, principal, schedule.  ],
   [:debit,  interest,  schedule. ],
   [:debit,  principal, schedule.]].each &block
end

#monthly_paymentObject



75
76
77
# File 'lib/mudrat_projector/amortizer.rb', line 75

def monthly_payment
  schedule.monthly_payment
end