Class: MudratProjector::CompoundInterestAmortizer
- Defined in:
- lib/mudrat_projector/amortizer.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#schedule ⇒ Object
readonly
Returns the value of attribute schedule.
Attributes inherited from Amortizer
Instance Method Summary collapse
- #amortize ⇒ Object
- #each_entry(&block) ⇒ Object
- #initial_value ⇒ Object
-
#initialize(schedule, projection_range, **params) ⇒ CompoundInterestAmortizer
constructor
A new instance of CompoundInterestAmortizer.
- #interest ⇒ Object
- #months_amortized ⇒ Object
- #next_transaction(transaction, schedule) ⇒ Object
- #payment ⇒ Object
- #principal ⇒ Object
- #rate ⇒ Object
- #schedule_range ⇒ Object
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
#schedule ⇒ Object (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
#amortize ⇒ Object
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.interest_account ], [:credit, principal, schedule.principal_account], [:debit, payment, schedule.payment_account ]].each &block end |
#initial_value ⇒ Object
27 28 29 |
# File 'lib/mudrat_projector/amortizer.rb', line 27 def initial_value schedule.initial_value end |
#interest ⇒ Object
31 32 33 |
# File 'lib/mudrat_projector/amortizer.rb', line 31 def interest @interest.round 2 end |
#months_amortized ⇒ Object
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 |
#payment ⇒ Object
58 59 60 |
# File 'lib/mudrat_projector/amortizer.rb', line 58 def payment interest + principal end |
#principal ⇒ Object
54 55 56 |
# File 'lib/mudrat_projector/amortizer.rb', line 54 def principal @principal.round 2 end |
#rate ⇒ Object
50 51 52 |
# File 'lib/mudrat_projector/amortizer.rb', line 50 def rate schedule.rate end |
#schedule_range ⇒ Object
62 63 64 |
# File 'lib/mudrat_projector/amortizer.rb', line 62 def schedule_range schedule.range end |