Class: Monetico::Loan
- Inherits:
-
Object
- Object
- Monetico::Loan
- Defined in:
- lib/monetico/loan.rb
Constant Summary collapse
- CADENCE =
{ monthly: 12, weekly: 52, }
Instance Method Summary collapse
- #capital ⇒ Object
-
#initialize(amount, interest_rate, no_installments, cadence = :monthly, kind = :desc) ⇒ Loan
constructor
A new instance of Loan.
- #interests(idx) ⇒ Object
- #payback(range) ⇒ Object
- #total_interests ⇒ Object
Constructor Details
#initialize(amount, interest_rate, no_installments, cadence = :monthly, kind = :desc) ⇒ Loan
Returns a new instance of Loan.
8 9 10 11 12 13 |
# File 'lib/monetico/loan.rb', line 8 def initialize(amount, interest_rate, no_installments, cadence=:monthly, kind=:desc) @amount = amount.big @interest_rate = interest_rate.big / CADENCE[cadence] @no_installments = no_installments @kind = kind end |
Instance Method Details
#capital ⇒ Object
15 16 17 |
# File 'lib/monetico/loan.rb', line 15 def capital @amount / @no_installments end |
#interests(idx) ⇒ Object
30 31 32 |
# File 'lib/monetico/loan.rb', line 30 def interests(idx) (@amount - (idx - 1) * capital) * @interest_rate end |
#payback(range) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/monetico/loan.rb', line 34 def payback(range) from = range.begin to = range.end if const? par = (1 + @interest_rate) ** @no_installments range.map do |n| { no: n, interests: interests(n), amount: @amount * @interest_rate * par / (par - 1) } end else range.map do |n| { no: n, interests: interests(n), amount: capital + interests(n) } end end end |
#total_interests ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/monetico/loan.rb', line 19 def total_interests if const? par = (1 + @interest_rate) ** @no_installments payback_amount = @amount * @interest_rate * par / (par - 1) payback_amount * @no_installments - @amount else 0.5.big * @interest_rate * @no_installments * (@amount + capital) end end |