lib/finrb/amortization.rb in finrb-0.0.1 vs lib/finrb/amortization.rb in finrb-0.1.0

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative 'cashflows' -require_relative 'decimal' -require_relative 'transaction' +require_relative "cashflows" +require_relative "decimal" +require_relative "transaction" module Finrb # the Amortization class provides an interface for working with loan amortizations. # @note There are _two_ ways to create an amortization. The first # example uses the amortize method for the Numeric class. The second @@ -65,11 +65,11 @@ # rate = Rate.new(0.0375, :apr, :duration => (30 * 12)) # amt = 300000.amortize(rate){ |payment| payment.amount-100} # amt.additional_payments #=> [DecNum('-100.00'), DecNum('-100.00'), ... ] # @api public def additional_payments - @transactions.select(&:payment?).map(&:difference) + @transactions.filter_map { |trans| trans.difference if trans.payment? } end # amortize the balance of loan with the given interest rate # @return none # @param [Rate] rate the interest rate to use in the amortization @@ -153,11 +153,11 @@ # rate = Rate.new(0.0375, :apr, :duration => (30 * 12)) # amt = 300000.amortize(rate) # amt.interest[0,6].sum #=> DecNum('5603.74') # @api public def interest - @transactions.select(&:interest?).map(&:amount) + @transactions.filter_map { |trans| trans.amount if trans.interest? } end # @return [DecNum] the periodic payment due on a loan # @param [DecNum] principal the initial amount of the loan or investment # @param [Rate] rate the applicable interest rate (per period) @@ -165,11 +165,11 @@ # @note in most cases, you will probably want to use rate.monthly when calling this function outside of an Amortization instance. # @example # rate = Rate.new(0.0375, :apr, :duration => (30 * 12)) # rate.duration #=> 360 # Amortization.payment(200000, rate.monthly, rate.duration) #=> DecNum('-926.23') - # @see http://en.wikipedia.org/wiki/Amortization_calculator + # @see https://en.wikipedia.org/wiki/Amortization_calculator # @api public def self.payment(principal, rate, periods) if rate.zero? # simplified formula to avoid division-by-zero when interest rate is zero -(principal / periods).round(2) @@ -183,10 +183,10 @@ # rate = Rate.new(0.0375, :apr, :duration => (30 * 12)) # amt = 300000.amortize(rate) # amt.payments.sum #=> DecNum('-500163.94') # @api public def payments - @transactions.select(&:payment?).map(&:amount) + @transactions.filter_map { |trans| trans.amount if trans.payment? } end end end class Numeric