Class: MudratProjector::CompoundInterestAmortizer

Inherits:
Amortizer
  • Object
show all
Defined in:
lib/mudrat_projector/amortizer.rb

Direct Known Subclasses

MortgageAmortizer

Instance Attribute Summary collapse

Attributes inherited from Amortizer

#balance

Instance Method Summary collapse

Constructor Details

#initialize(schedule, projection_range, **params) ⇒ CompoundInterestAmortizer

Returns a new instance of CompoundInterestAmortizer.



9
10
11
12
13
# File 'lib/mudrat_projector/amortizer.rb', line 9

def initialize schedule, projection_range, **params
  @schedule         = schedule
  @projection_range = projection_range
  @balance, @interest, @principal = amortize
end

Instance Attribute Details

#scheduleObject (readonly)

Returns the value of attribute schedule.



7
8
9
# File 'lib/mudrat_projector/amortizer.rb', line 7

def schedule
  @schedule
end

Instance Method Details

#amortizeObject



15
16
17
18
19
# File 'lib/mudrat_projector/amortizer.rb', line 15

def amortize
  balance  = initial_value * ((1 + rate) ** months_amortized)
  interest = balance - initial_value
  [balance, interest, 0]
end

#each_entry(&block) ⇒ Object



21
22
23
24
25
# File 'lib/mudrat_projector/amortizer.rb', line 21

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

#initial_valueObject



27
28
29
# File 'lib/mudrat_projector/amortizer.rb', line 27

def initial_value
  schedule.initial_value
end

#interestObject



31
32
33
# File 'lib/mudrat_projector/amortizer.rb', line 31

def interest
  @interest.round 2
end

#months_amortizedObject



35
36
37
38
39
# File 'lib/mudrat_projector/amortizer.rb', line 35

def months_amortized
  start  = [projection_range.begin, schedule_range.begin].max
  finish = [projection_range.end,   schedule_range.end].min
  DateDiff.date_diff(:month, start, finish).to_i
end

#next_transaction(transaction, schedule) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/mudrat_projector/amortizer.rb', line 41

def next_transaction transaction, schedule
  Transaction.new(
    date:     projection_range.end + 1,
    credits:  transaction.credits,
    debits:   transaction.debits,
    schedule: schedule,
  )
end

#paymentObject



58
59
60
# File 'lib/mudrat_projector/amortizer.rb', line 58

def payment
  interest + principal
end

#principalObject



54
55
56
# File 'lib/mudrat_projector/amortizer.rb', line 54

def principal
  @principal.round 2
end

#rateObject



50
51
52
# File 'lib/mudrat_projector/amortizer.rb', line 50

def rate
  schedule.rate
end

#schedule_rangeObject



62
63
64
# File 'lib/mudrat_projector/amortizer.rb', line 62

def schedule_range
  schedule.range
end