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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b9270952b4db924510e32a7767e8938c0087bfb2808f5f91083f469fdc2f0f9
4
- data.tar.gz: 7f35803f477a96ce89afef7fe150c02e7b53de92c33dd2027521e67420d29206
3
+ metadata.gz: d8a106bfb77db774342387c9692d0db2ba9c0b3a39cd95c5157278d9af91a482
4
+ data.tar.gz: cc4dba477d4bed5803c1c3d22b1d95b4ee053469e45597f56993f53285e67a1f
5
5
  SHA512:
6
- metadata.gz: 7b433d79cc59d0d6a6368205d92d54fff8c877511693b9ce6e51cd230f0c2a04d074c4526d3a82b112ba0836d17c9b6be64d1c8f068c6c72fbe70c3c9a4f4812
7
- data.tar.gz: f09d1dc9b2b0290b281f43e707f42737fe364d57d2a979f428ad811f57324a2f9cf6c63e1c588877eec12f1bc5d77b08b321f05f8a17bfebf4dce297fbad616e
6
+ metadata.gz: ddc579bae548e775abddf463e29ed9c716d938c829e45563d67a5356718e2a04f95f5f430c11f4c89586fdcbe2dfc213623b55a3e246b72b242470ca9bb88054
7
+ data.tar.gz: 75d8cd4b256b5b45b43600ff589d6ddf7d6c741ffdaa12ebfe5095f7faec3d91496678cf978e660afc39ee70a4b07f5fef3007b944be865b1132a9a921f30adc
data/.gitignore CHANGED
@@ -7,6 +7,7 @@ test/dummy/tmp/
7
7
  test/dummy/.sass-cache
8
8
 
9
9
  *.sh
10
+ *.gem
10
11
  sample_data.rb
11
12
  gre.rb
12
13
  gpm.rb
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/**/*.rb']
6
+ #t.verbose = true
7
+ end
8
+
9
+ desc "Run tests"
10
+ task default: :test
@@ -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('ostruct')
21
- s.add_dependency('active_support')
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 OpenLineCalculator
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
- puts "#{self.class.name}##{__method__}: date:#{cdate} -- end_date:#{end_date}"
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
- puts "#{self.class.name}##{__method__}: interest rate: #{@interest_rate}"
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
- puts "#{self.class.name}##{__method__}: calculated interest: #{result}"
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
- puts "#{self.class.name}##{__method__}: days #{days}-- factor:#{factor}"
219
+ logger.debug "#{self.class.name}##{__method__}: days #{days}-- factor:#{factor}"
156
220
  factor
157
221
  end
158
222
  end
159
- end
223
+ end
@@ -1,3 +1,3 @@
1
1
  module InterestCalc
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/interest_calc.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  require_relative 'interest_calc/version'
2
- require_relative 'interest_calc/calculators/open_line_calculator'
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.1.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-06-22 00:00:00.000000000 Z
11
+ date: 2022-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ostruct
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
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: '0'
26
+ version: 7.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: active_support
28
+ name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
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: '0'
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/open_line_calculator.rb
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: