financial_calculator 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +1 -1
- data/lib/financial_calculator.rb +1 -0
- data/lib/financial_calculator/nper.rb +70 -0
- data/lib/financial_calculator/version.rb +1 -1
- data/spec/nper_spec.rb +53 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbbac869b96fed665ef26160601fa702b89bfa8753f9a36d02b4ad4b37e3054c
|
4
|
+
data.tar.gz: 19b4235429baa31c6ef21ed758af7d6355825d5d0ba09a406c800bde628f37e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d8981744d1ffef65c9c2e932c7ad5577a4ec731e138e51cd87596f650176f97cdf6896e5015bf18a3591c38d83c7303b1e9f83891e431956da8db3ff8df5133
|
7
|
+
data.tar.gz: 42ef34bd41ea46bb8bfcab9cd56d2bab2221fbef0c8bef74a47e22bf0d4b8cb60b9d544cebba986ed5bbd8cf83ba0e2948121de9159f04ae5705be9107320bbd
|
data/CHANGELOG.md
CHANGED
@@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
-
## [
|
7
|
+
## [3.3.0] - 2018-07-13
|
8
|
+
### Added
|
9
|
+
- New class for NPER
|
10
|
+
|
11
|
+
### Removed
|
12
|
+
|
13
|
+
### Changed
|
8
14
|
|
9
15
|
## [3.2.0] - 2018-06-09
|
10
16
|
### Added
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# FINANCIAL CALCULATOR
|
2
2
|
[](https://travis-ci.org/mfalzone/financial_calculator)
|
3
3
|
[](https://coveralls.io/github/mfalzone/financial_calculator?branch=master)
|
4
4
|
|
data/lib/financial_calculator.rb
CHANGED
@@ -16,6 +16,7 @@ module FinancialCalculator
|
|
16
16
|
require 'financial_calculator/irr'
|
17
17
|
require 'financial_calculator/ppmt'
|
18
18
|
require 'financial_calculator/pv'
|
19
|
+
require 'financial_calculator/nper'
|
19
20
|
require 'financial_calculator/npv'
|
20
21
|
require 'financial_calculator/xnpv'
|
21
22
|
require 'financial_calculator/xirr'
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module FinancialCalculator
|
2
|
+
# Calculates the number of periods in an annuity
|
3
|
+
class Nper
|
4
|
+
include ::Validator
|
5
|
+
|
6
|
+
# @return [Numeric] The rate used for calculating the number of payments
|
7
|
+
# @api public
|
8
|
+
attr_reader :rate
|
9
|
+
|
10
|
+
# @return [Numeric] The amount of each payment made
|
11
|
+
# @api public
|
12
|
+
attr_reader :payment
|
13
|
+
|
14
|
+
# @return [Numeric] The current value of the annuity
|
15
|
+
# @api public
|
16
|
+
attr_reader :present_value
|
17
|
+
|
18
|
+
# @return [Numeric] The ending value of the annuity. Defaults to 0
|
19
|
+
# @api public
|
20
|
+
attr_reader :future_value
|
21
|
+
|
22
|
+
# @return [Boolean] Whether the payment is made at the beginning of the
|
23
|
+
# period (true) or end of the period (false)
|
24
|
+
# @api public
|
25
|
+
attr_reader :pay_at_beginning
|
26
|
+
|
27
|
+
# @return [DecNum] Result of the PMT calculation
|
28
|
+
# @api public
|
29
|
+
attr_reader :result
|
30
|
+
|
31
|
+
# Create a new object for calculating the periodic payment of an ordinary annuity
|
32
|
+
# @param [Numeric] rate The discount (interest) rate
|
33
|
+
# @param [Numeric] payment The amount of each payment made
|
34
|
+
# @param [Numeric] present_value The current value of the annuity
|
35
|
+
# @param [Numeric] future_value The ending value of the annuity
|
36
|
+
# @param [Boolean] pay_at_beginning. Whether the payment is made at the beginning
|
37
|
+
# of the period (true) or end of the period (false)
|
38
|
+
# @return [FinancialCalculator::Nper] An instance of NPER calculation
|
39
|
+
def initialize(rate, payment, present_value, future_value = 0, pay_at_beginning = false)
|
40
|
+
validate_numerics(rate: rate, payment: payment, present_value: present_value, future_value: future_value)
|
41
|
+
|
42
|
+
@rate = Flt::DecNum(rate.to_s)
|
43
|
+
@payment = Flt::DecNum(payment.to_s)
|
44
|
+
@present_value = Flt::DecNum(present_value.to_s)
|
45
|
+
@future_value = Flt::DecNum(future_value || "0")
|
46
|
+
@pay_at_beginning = pay_at_beginning || false
|
47
|
+
@result = solve(@rate, @payment, @present_value, @future_value, @pay_at_beginning)
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [Boolean] Whether the payments are made at the beginning of each period
|
51
|
+
# @api public
|
52
|
+
def pays_at_beginning?
|
53
|
+
@pay_at_beginning
|
54
|
+
end
|
55
|
+
|
56
|
+
def inspect
|
57
|
+
"NPER(#{result})"
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def solve(rate, pmt, pv, fv, pay_at_beginning)
|
63
|
+
type = pay_at_beginning ? 1 : 0
|
64
|
+
initial = pmt * (1 + rate * type)
|
65
|
+
|
66
|
+
# TODO: Insert better error handling if either argument is negative.
|
67
|
+
@result = Math.log((initial - fv * rate) / (initial + pv * rate)) / Math.log(1.0 + rate)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/spec/nper_spec.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe "Nper" do
|
4
|
+
let(:rate) { 0.1 }
|
5
|
+
let(:payment) { 4500 }
|
6
|
+
let(:present_value) { 0 }
|
7
|
+
let(:future_value) { 0 }
|
8
|
+
let(:nper) { Nper.new(rate, payment, present_value, future_value) }
|
9
|
+
|
10
|
+
subject { Nper.new(rate, payment, present_value, future_value) }
|
11
|
+
|
12
|
+
it 'has a rate attribute' do
|
13
|
+
expect(subject).to have_attributes(rate: rate)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'has a payment attribute' do
|
17
|
+
expect(subject).to have_attributes(payment: payment)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'has a present_value attribute' do
|
21
|
+
expect(subject).to have_attributes(present_value: present_value)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'has a future_value attribute' do
|
25
|
+
expect(subject).to have_attributes(future_value: future_value)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'has a pays_at_beginning? attribute that defaults to false' do
|
29
|
+
expect(subject).to have_attributes(pays_at_beginning?: false)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'has a result attribute' do
|
33
|
+
expect(subject.result).to be_a Numeric
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when provided a non-numeric rate' do
|
37
|
+
let(:rate) { 'string' }
|
38
|
+
|
39
|
+
it 'raises an ArgumentError' do
|
40
|
+
expect { subject }.to raise_error ArgumentError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#inspect' do
|
45
|
+
subject { nper.inspect }
|
46
|
+
|
47
|
+
it { is_expected.to be_a String }
|
48
|
+
it { is_expected.to include 'NPER' }
|
49
|
+
it 'includes the result of the Nper calculation' do
|
50
|
+
expect(subject).to include nper.result.to_s
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: financial_calculator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Falzone
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: flt
|
@@ -163,6 +163,7 @@ files:
|
|
163
163
|
- lib/financial_calculator/fv.rb
|
164
164
|
- lib/financial_calculator/ipmt.rb
|
165
165
|
- lib/financial_calculator/irr.rb
|
166
|
+
- lib/financial_calculator/nper.rb
|
166
167
|
- lib/financial_calculator/npv.rb
|
167
168
|
- lib/financial_calculator/pmt.rb
|
168
169
|
- lib/financial_calculator/ppmt.rb
|
@@ -177,6 +178,7 @@ files:
|
|
177
178
|
- spec/fv_spec.rb
|
178
179
|
- spec/ipmt_spec.rb
|
179
180
|
- spec/irr_spec.rb
|
181
|
+
- spec/nper_spec.rb
|
180
182
|
- spec/npv_spec.rb
|
181
183
|
- spec/pmt_spec.rb
|
182
184
|
- spec/ppmt_spec.rb
|
@@ -217,6 +219,7 @@ test_files:
|
|
217
219
|
- spec/fv_spec.rb
|
218
220
|
- spec/ipmt_spec.rb
|
219
221
|
- spec/irr_spec.rb
|
222
|
+
- spec/nper_spec.rb
|
220
223
|
- spec/npv_spec.rb
|
221
224
|
- spec/pmt_spec.rb
|
222
225
|
- spec/ppmt_spec.rb
|