Class: MortgageBuddy::Amoratizer

Inherits:
Object
  • Object
show all
Includes:
Monthly, SafeNum
Defined in:
lib/mortgage_buddy/amoratizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SafeNum

#safe_float, #safe_int

Methods included from Monthly

#monthly_interest_rate

Constructor Details

#initialize(params = {}) ⇒ Amoratizer

Parameters

:loan_amount

Loan amount. Price of home minus down payment

:interest_rate

The actual interest rate of the loan

:period

Number of months of the loan. 30 yr is 360. 15 yr is 189

:extra_monthly_payment

This is the extra monthly principal payment. Optional and defaults to 0



12
13
14
15
16
17
18
# File 'lib/mortgage_buddy/amoratizer.rb', line 12

def initialize(params={})
  @interest_rate              = safe_float params[:interest_rate]
  @loan_amount                = safe_float params[:loan_amount]
  @period                     = safe_int params[:period]
  @extra_monthly_payment      = safe_float(params.fetch(:extra_monthly_payment, 0))
  @interest_rounding_strategy = params[:interest_rounding_strategy]
end

Instance Attribute Details

#extra_monthly_paymentObject (readonly)

optional



5
6
7
# File 'lib/mortgage_buddy/amoratizer.rb', line 5

def extra_monthly_payment
  @extra_monthly_payment
end

#interest_rateObject (readonly)

Returns the value of attribute interest_rate.



4
5
6
# File 'lib/mortgage_buddy/amoratizer.rb', line 4

def interest_rate
  @interest_rate
end

#loan_amountObject (readonly)

Returns the value of attribute loan_amount.



4
5
6
# File 'lib/mortgage_buddy/amoratizer.rb', line 4

def loan_amount
  @loan_amount
end

#periodObject (readonly)

Returns the value of attribute period.



4
5
6
# File 'lib/mortgage_buddy/amoratizer.rb', line 4

def period
  @period
end

Instance Method Details

#actual_monthly_paymentObject



33
34
35
# File 'lib/mortgage_buddy/amoratizer.rb', line 33

def actual_monthly_payment
  (minimum_monthly_payment + @extra_monthly_payment).round(2)
end

#last_monthly_paymentObject



41
42
43
# File 'lib/mortgage_buddy/amoratizer.rb', line 41

def last_monthly_payment
  payments.last.payment
end

#minimum_monthly_paymentObject

A = [i * P * (1 + i)^n] / [(1 + i)^n - 1] A = minimum_monthly_payment P = loan amount i = monthly interest rate n = period (number of payments)



29
30
31
# File 'lib/mortgage_buddy/amoratizer.rb', line 29

def minimum_monthly_payment
  @minimum_monthly_payment ||= calculate_minimum_monthly_payment
end

#paymentsObject



45
46
47
48
49
50
51
# File 'lib/mortgage_buddy/amoratizer.rb', line 45

def payments
  @payments ||= MortgageBuddy::PaymentPlan.build(loan_amount:                self.loan_amount,
                                                 period:                     self.period,
                                                 monthly_payment:            actual_monthly_payment,
                                                 monthly_interest_rate:      monthly_interest_rate,
                                                 interest_rounding_strategy: @interest_rounding_strategy)
end

#total_interestObject



20
21
22
# File 'lib/mortgage_buddy/amoratizer.rb', line 20

def total_interest
  payments.inject(0) { |sum, pay| sum + pay.interest }.round(2)
end

#total_num_paymentsObject



37
38
39
# File 'lib/mortgage_buddy/amoratizer.rb', line 37

def total_num_payments
  payments.length
end