Class: MortgageBuddy::MortgageCost

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

Constant Summary collapse

DEFAULT_PERIOD =
360
DEFAULT_FEES =
0
DEFAULT_POINTS =
0

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 = {}) ⇒ MortgageCost

Parameters

:loan_amount

Loan amount. Price of home minus down payment

:interest_rate

The interest rate of the loan

:period

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

:fees

Closing cost fees. Optional and defaults to 0

:points

Points. Optional and defaults to 0



15
16
17
18
19
20
21
22
23
# File 'lib/mortgage_buddy/mortgage_cost.rb', line 15

def initialize(params={})
  raise '[:loan_amount] required' if params[:loan_amount].blank?
  raise '[:interest_rate] required' if params[:interest_rate].blank?
  @loan_amount   = safe_float params[:loan_amount]
  @interest_rate = safe_float params[:interest_rate]
  @period        = safe_int params.fetch(:period, DEFAULT_PERIOD)
  @fees          = safe_int params.fetch(:fees, DEFAULT_FEES)
  @points        = safe_float params.fetch(:points, DEFAULT_POINTS)
end

Instance Attribute Details

#feesObject (readonly)

Returns the value of attribute fees.



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

def fees
  @fees
end

#interest_rateObject (readonly)

Returns the value of attribute interest_rate.



4
5
6
# File 'lib/mortgage_buddy/mortgage_cost.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/mortgage_cost.rb', line 4

def loan_amount
  @loan_amount
end

#periodObject (readonly)

Returns the value of attribute period.



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

def period
  @period
end

#pointsObject (readonly)

Returns the value of attribute points.



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

def points
  @points
end

Instance Method Details

#aprObject



25
26
27
28
29
30
# File 'lib/mortgage_buddy/mortgage_cost.rb', line 25

def apr
  @apr ||= MortgageBuddy::AprCalculator.new(loan_amount:               @loan_amount,
                                            monthly_payment_with_fees: monthly_payment_with_fees,
                                            period:                    period,
                                            monthly_interest_rate:     monthly_interest_rate).apr
end

#monthly_paymentObject



32
33
34
# File 'lib/mortgage_buddy/mortgage_cost.rb', line 32

def monthly_payment
  @monthly_payment ||= calculate_monthly_payment(@loan_amount, monthly_interest_rate, @period)
end

#monthly_payment_with_feesObject



36
37
38
# File 'lib/mortgage_buddy/mortgage_cost.rb', line 36

def monthly_payment_with_fees
  @monthly_payment_with_fees ||= calculate_monthly_payment(@loan_amount + total_fees, monthly_interest_rate, @period)
end

#total_fees(negative_allowed = false) ⇒ Object



40
41
42
43
44
# File 'lib/mortgage_buddy/mortgage_cost.rb', line 40

def total_fees(negative_allowed = false)
  #fees may not be negative (borrower is not paid)
  total_fees = calculate_total_fees
  !negative_allowed && total_fees < 0 ? 0 : total_fees
end