interest_calc 0.1.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Rakefile +10 -0
- data/interest_calc.gemspec +3 -3
- data/lib/interest_calc/calculators/{open_line_calculator.rb → line_of_credit_calculator.rb} +71 -7
- data/lib/interest_calc/version.rb +1 -1
- data/lib/interest_calc.rb +1 -1
- metadata +15 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8a106bfb77db774342387c9692d0db2ba9c0b3a39cd95c5157278d9af91a482
|
4
|
+
data.tar.gz: cc4dba477d4bed5803c1c3d22b1d95b4ee053469e45597f56993f53285e67a1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddc579bae548e775abddf463e29ed9c716d938c829e45563d67a5356718e2a04f95f5f430c11f4c89586fdcbe2dfc213623b55a3e246b72b242470ca9bb88054
|
7
|
+
data.tar.gz: 75d8cd4b256b5b45b43600ff589d6ddf7d6c741ffdaa12ebfe5095f7faec3d91496678cf978e660afc39ee70a4b07f5fef3007b944be865b1132a9a921f30adc
|
data/.gitignore
CHANGED
data/Rakefile
ADDED
data/interest_calc.gemspec
CHANGED
@@ -17,9 +17,9 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.homepage = 'https://rubygems.org/gems/interest_calc'
|
18
18
|
s.license = 'MIT'
|
19
19
|
|
20
|
-
s.add_dependency('
|
21
|
-
s.
|
20
|
+
s.add_dependency('activesupport', '~> 7.0.0')
|
21
|
+
s.add_development_dependency('minitest', '~> 5.16')
|
22
22
|
|
23
|
-
s.add_dependency('interest_days')
|
23
|
+
#s.add_dependency('interest_days', '~> 0.4.1')
|
24
24
|
|
25
25
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'logger'
|
1
2
|
require 'ostruct'
|
2
3
|
require 'active_support'
|
3
4
|
require 'active_support/core_ext'
|
@@ -6,16 +7,74 @@ require 'date'
|
|
6
7
|
|
7
8
|
|
8
9
|
module InterestCalc
|
9
|
-
class
|
10
|
+
class LineOfCreditCalculator
|
10
11
|
|
11
12
|
attr_accessor :interest_rate
|
13
|
+
attr_accessor :logger
|
12
14
|
|
13
15
|
def initialize(interest_rate = 0.05)
|
14
16
|
@interest_rate = interest_rate
|
17
|
+
@logger = Logger.new(STDOUT)
|
15
18
|
end
|
16
19
|
|
17
|
-
# calculates the interest per month for an open line of credit
|
18
20
|
def calculate(data)
|
21
|
+
return_values = []
|
22
|
+
nitem = nil
|
23
|
+
changes = data[:changes]
|
24
|
+
end_date = data[:end_date]
|
25
|
+
|
26
|
+
if changes.nil? || changes.count < 1
|
27
|
+
raise Exception.new "No changes to the line of credit provided. Please provide an array of changes data[:changes]"
|
28
|
+
end
|
29
|
+
|
30
|
+
changes.reverse!
|
31
|
+
citem = changes.pop
|
32
|
+
cdate = citem[:date]
|
33
|
+
camount = citem[:amount]
|
34
|
+
|
35
|
+
|
36
|
+
interest = 0
|
37
|
+
nitem = changes.pop
|
38
|
+
|
39
|
+
logger.debug "#{self.class.name}##{__method__}:calculation starts: #{cdate.at_beginning_of_month}"
|
40
|
+
|
41
|
+
while(cdate <= cdate.at_end_of_month) do
|
42
|
+
|
43
|
+
# amount has changed
|
44
|
+
if(!nitem.nil? && nitem[:date] == cdate)
|
45
|
+
logger.debug "#{self.class.name}##{__method__}: amount changing. OLD:#{camount} -- change:#{nitem[:amount]} -- NEW:#{camount + nitem[:amount]}"
|
46
|
+
camount += nitem[:amount]
|
47
|
+
nitem = changes.pop # pops nil if changes.empty?
|
48
|
+
end
|
49
|
+
|
50
|
+
interest += camount * interest_rate / 365.0
|
51
|
+
|
52
|
+
if(cdate == cdate.at_end_of_month)
|
53
|
+
return_values << {
|
54
|
+
interest: interest,
|
55
|
+
year: cdate.year,
|
56
|
+
month: cdate.month
|
57
|
+
}
|
58
|
+
|
59
|
+
interest = 0
|
60
|
+
logger.debug "#{self.class.name}##{__method__}: stored:#{return_values.last}"
|
61
|
+
|
62
|
+
# if there are no further changes and this month is over, abort
|
63
|
+
break if nitem.nil? && (end_date.nil? || cdate >= end_date)
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
cdate = cdate + 1.day
|
69
|
+
end
|
70
|
+
|
71
|
+
return return_values
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
# calculates the interest per month for an open line of credit
|
77
|
+
def calculate_old(data)
|
19
78
|
|
20
79
|
return_values = []
|
21
80
|
nitem = nil
|
@@ -108,6 +167,8 @@ module InterestCalc
|
|
108
167
|
def print(data)
|
109
168
|
values = calculate data
|
110
169
|
|
170
|
+
total_interest = 0.0
|
171
|
+
|
111
172
|
puts "--------------------------------------"
|
112
173
|
puts "| year-month | interest |"
|
113
174
|
puts "--------------------------------------"
|
@@ -115,7 +176,10 @@ module InterestCalc
|
|
115
176
|
ym = "#{item[:year]}-#{item[:month]}".ljust(16)
|
116
177
|
i = item[:interest].to_f.round(2).to_s.rjust(19)
|
117
178
|
puts "|#{ym}|#{i}|"
|
179
|
+
|
180
|
+
total_interest += item[:interest].to_f
|
118
181
|
end
|
182
|
+
puts "|#{"2022".ljust(16)}|#{total_interest.round(2).to_s.rjust(19)}|"
|
119
183
|
puts "--------------------------------------"
|
120
184
|
return values
|
121
185
|
end
|
@@ -128,16 +192,16 @@ module InterestCalc
|
|
128
192
|
raise "amount can't be nil"
|
129
193
|
end
|
130
194
|
|
131
|
-
|
195
|
+
logger.debug "#{self.class.name}##{__method__}: date:#{cdate} -- end_date:#{end_date}"
|
132
196
|
# calculate until end of month if no end_date given.
|
133
197
|
# Add one day to include last day in calculations
|
134
198
|
end_date = end_date || (cdate.at_end_of_month)
|
135
199
|
|
136
|
-
|
200
|
+
logger.debug "#{self.class.name}##{__method__}: interest rate: #{@interest_rate}"
|
137
201
|
|
138
202
|
result = interest_days_counter_factor(cdate, end_date) * @interest_rate * camount
|
139
203
|
|
140
|
-
|
204
|
+
logger.debug "#{self.class.name}##{__method__}: calculated interest: #{result}"
|
141
205
|
return result
|
142
206
|
end
|
143
207
|
|
@@ -152,8 +216,8 @@ module InterestCalc
|
|
152
216
|
# add 1 to count the first day as well
|
153
217
|
days = ((end_date - date).to_i + 1)
|
154
218
|
factor = days / 360.0
|
155
|
-
|
219
|
+
logger.debug "#{self.class.name}##{__method__}: days #{days}-- factor:#{factor}"
|
156
220
|
factor
|
157
221
|
end
|
158
222
|
end
|
159
|
-
end
|
223
|
+
end
|
data/lib/interest_calc.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require_relative 'interest_calc/version'
|
2
|
-
require_relative 'interest_calc/calculators/
|
2
|
+
require_relative 'interest_calc/calculators/line_of_credit_calculator'
|
metadata
CHANGED
@@ -1,57 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interest_calc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christoph Engelhardt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 7.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 7.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
34
|
-
type: :
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: interest_days
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :runtime
|
33
|
+
version: '5.16'
|
34
|
+
type: :development
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
|
-
- - "
|
38
|
+
- - "~>"
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
40
|
+
version: '5.16'
|
55
41
|
description: Helps you calculate interest for open lines of credit
|
56
42
|
email: christoph@christophengelhardt.com
|
57
43
|
executables: []
|
@@ -61,9 +47,10 @@ files:
|
|
61
47
|
- ".gitignore"
|
62
48
|
- LICENSE
|
63
49
|
- README.md
|
50
|
+
- Rakefile
|
64
51
|
- interest_calc.gemspec
|
65
52
|
- lib/interest_calc.rb
|
66
|
-
- lib/interest_calc/calculators/
|
53
|
+
- lib/interest_calc/calculators/line_of_credit_calculator.rb
|
67
54
|
- lib/interest_calc/version.rb
|
68
55
|
homepage: https://rubygems.org/gems/interest_calc
|
69
56
|
licenses:
|