Class: MortgageBuddy::PaymentPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/mortgage_buddy/payment_plan.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ PaymentPlan

Returns a new instance of PaymentPlan.



9
10
11
12
13
14
15
16
# File 'lib/mortgage_buddy/payment_plan.rb', line 9

def initialize(params)
  @loan_amount                = params[:loan_amount]
  @monthly_payment            = params[:monthly_payment]
  @monthly_interest_rate      = params[:monthly_interest_rate]
  @interest_rounding_strategy = params[:interest_rounding_strategy] || MortgageBuddy::StandardRounding
  @period                     = params[:period]
  @remaining_loan_amount      = @loan_amount
end

Class Method Details

.build(params) ⇒ Object



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

def build(params)
  new(params).payments
end

Instance Method Details

#build_paymentsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mortgage_buddy/payment_plan.rb', line 22

def build_payments
  payments       = []
  payment_number = 1
  while @remaining_loan_amount > 0.0
    interest               = next_payment_interest
    principal              = next_payment_principal(interest, payments.length + 1)
    payment                = interest + principal
    @remaining_loan_amount = (@remaining_loan_amount - principal).round(2)
    payments << OpenStruct.new(payment:   payment.round(2),
                               interest:  interest,
                               principal: principal,
                               number:    payment_number,
                               balance:   @remaining_loan_amount)
    payment_number += 1
  end
  payments
end

#last_payment?(payment_number) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/mortgage_buddy/payment_plan.rb', line 52

def last_payment?(payment_number)
  @period == payment_number
end

#next_payment_interestObject



48
49
50
# File 'lib/mortgage_buddy/payment_plan.rb', line 48

def next_payment_interest
  @interest_rounding_strategy.round(@monthly_interest_rate * @remaining_loan_amount)
end

#next_payment_principal(interest, payment_number) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/mortgage_buddy/payment_plan.rb', line 40

def next_payment_principal(interest, payment_number)
  principal = @monthly_payment - interest
  if principal > @remaining_loan_amount || last_payment?(payment_number)
    principal = @remaining_loan_amount
  end
  principal.round(2)
end

#paymentsObject



18
19
20
# File 'lib/mortgage_buddy/payment_plan.rb', line 18

def payments
  @payments ||= build_payments
end