lare_round 0.0.3 → 1.0.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/.github/workflows/ci.yml +37 -0
- data/.rubocop.yml +4 -0
- data/.rubocop_todo.yml +16 -47
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/README.md +24 -4
- data/bin/console +15 -0
- data/lare_round.gemspec +7 -9
- data/lib/lare_round/version.rb +1 -1
- data/lib/lare_round.rb +10 -16
- data/test/lare_round_test.rb +10 -14
- data/test/test_helper.rb +0 -1
- metadata +13 -85
- data/.travis.yml +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75cbed7590d38dbbddd0ddb51341cc2fd9b3b55c614ebc347cad64ac8b675d6c
|
4
|
+
data.tar.gz: 99c9fa456289a7c6e384b90922b770532ce937874a226dedbae6a1e83cc32b49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5d356f37278ac48cad5f49cba5a88587cc35c57e68a650126c9d066f86d101442cd97c17522c0fd2f0db88d9127cfe6dcb2aa755403883fe4006e1c9c98dba4
|
7
|
+
data.tar.gz: f1f3193af4b5acd7a09a43b9e3c5aee28ceb460730e0ab19a4d6d45d2d20cc0649a6616e6dd77ceee117b1a4e4903c60db909f74e4087fca0372003d1b46368b
|
@@ -0,0 +1,37 @@
|
|
1
|
+
name: Continuous Integration
|
2
|
+
on:
|
3
|
+
push:
|
4
|
+
branches: [ master ]
|
5
|
+
pull_request:
|
6
|
+
branches: [ master ]
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
test:
|
10
|
+
name: Rspec and Rubocop
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby-version: ['3.0.4', '3.1.2','3.2.0']
|
16
|
+
|
17
|
+
steps:
|
18
|
+
- name: Check out code
|
19
|
+
uses: actions/checkout@v4
|
20
|
+
|
21
|
+
- name: Set up Ruby
|
22
|
+
uses: ruby/setup-ruby@v1.215.0
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.ruby-version }}
|
25
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
26
|
+
|
27
|
+
- name: Rubocop
|
28
|
+
env:
|
29
|
+
RAILS_ENV: test
|
30
|
+
run: |
|
31
|
+
bundle exec rubocop
|
32
|
+
|
33
|
+
- name: Minitest
|
34
|
+
env:
|
35
|
+
RAILS_ENV: test
|
36
|
+
run: |
|
37
|
+
bundle exec rake test
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -1,69 +1,38 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2023-09-11 13:29:52 UTC using RuboCop version 1.56.3.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
9
|
# Offense count: 3
|
10
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
|
10
11
|
Metrics/AbcSize:
|
11
|
-
Max:
|
12
|
+
Max: 25
|
12
13
|
|
13
14
|
# Offense count: 3
|
14
|
-
# Configuration parameters: CountComments,
|
15
|
-
#
|
15
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
16
|
+
# AllowedMethods: refine
|
16
17
|
Metrics/BlockLength:
|
17
|
-
Max:
|
18
|
+
Max: 78
|
18
19
|
|
19
20
|
# Offense count: 1
|
20
|
-
# Configuration parameters: CountComments.
|
21
|
+
# Configuration parameters: CountComments, CountAsOne.
|
21
22
|
Metrics/ClassLength:
|
22
23
|
Max: 130
|
23
24
|
|
24
|
-
# Offense count: 2
|
25
|
-
# Configuration parameters: CountComments, ExcludedMethods.
|
26
|
-
Metrics/MethodLength:
|
27
|
-
Max: 18
|
28
|
-
|
29
|
-
# Offense count: 1
|
30
|
-
Style/Documentation:
|
31
|
-
Exclude:
|
32
|
-
- 'spec/**/*'
|
33
|
-
- 'test/**/*'
|
34
|
-
- 'lib/lare_round.rb'
|
35
|
-
|
36
25
|
# Offense count: 1
|
37
|
-
#
|
38
|
-
|
39
|
-
|
40
|
-
- 'lare_round.gemspec'
|
26
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns.
|
27
|
+
Metrics/CyclomaticComplexity:
|
28
|
+
Max: 10
|
41
29
|
|
42
30
|
# Offense count: 2
|
43
|
-
# Configuration parameters:
|
44
|
-
|
45
|
-
|
46
|
-
- 'lib/lare_round.rb'
|
47
|
-
|
48
|
-
# Offense count: 1
|
49
|
-
# Cop supports --auto-correct.
|
50
|
-
# Configuration parameters: EnforcedStyle.
|
51
|
-
# SupportedStyles: use_perl_names, use_english_names
|
52
|
-
Style/SpecialGlobalVars:
|
53
|
-
Exclude:
|
54
|
-
- 'lare_round.gemspec'
|
55
|
-
|
56
|
-
# Offense count: 1
|
57
|
-
# Cop supports --auto-correct.
|
58
|
-
# Configuration parameters: EnforcedStyle, AllowSafeAssignment.
|
59
|
-
# SupportedStyles: require_parentheses, require_no_parentheses, require_parentheses_when_complex
|
60
|
-
Style/TernaryParentheses:
|
61
|
-
Exclude:
|
62
|
-
- 'lib/lare_round.rb'
|
31
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
32
|
+
Metrics/MethodLength:
|
33
|
+
Max: 18
|
63
34
|
|
64
35
|
# Offense count: 1
|
65
|
-
#
|
66
|
-
|
67
|
-
|
68
|
-
Metrics/LineLength:
|
69
|
-
Max: 82
|
36
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns.
|
37
|
+
Metrics/PerceivedComplexity:
|
38
|
+
Max: 10
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.2.0
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,8 +3,9 @@
|
|
3
3
|
A collection of BigDecimal items e.g. invoice items can be rounded for displaying them in views. Rounding may apply a rounding error to the items such as the summed up rounded items will show deviation towards an invoice total with summed unrounded items. Which might cause confusion for customers and finance departments alike. Application of the largest remainder method can help to preserve the total sum for rounded parts thus eliminating this confusion.
|
4
4
|
|
5
5
|
## Build status
|
6
|
-
[](https://github.com/jethroo/lare_round/actions/workflows/ci.yml)
|
7
7
|
[](http://badge.fury.io/rb/lare_round)
|
8
|
+
<a href="https://codeclimate.com/github/jethroo/lare_round/maintainability"><img src="https://api.codeclimate.com/v1/badges/9d97f5a7c54707277efc/maintainability" /></a>
|
8
9
|
|
9
10
|
## Used in production
|
10
11
|
|
@@ -97,11 +98,30 @@ Item (unrounded)| Price (unrounded) | LareRound | Financial
|
|
97
98
|
|
98
99
|
Add this line to your application's Gemfile:
|
99
100
|
|
100
|
-
|
101
|
-
|
101
|
+
```bash
|
102
|
+
gem 'lare_round', '~> 1.0.0'
|
103
|
+
```
|
102
104
|
And then execute:
|
103
105
|
|
104
|
-
|
106
|
+
```bash
|
107
|
+
bundle install
|
108
|
+
```
|
109
|
+
|
110
|
+
## Test
|
111
|
+
|
112
|
+
The tests can be run after bundling with:
|
113
|
+
|
114
|
+
```bash
|
115
|
+
rake test
|
116
|
+
```
|
117
|
+
|
118
|
+
## Console
|
119
|
+
|
120
|
+
To start the interactive ruby shell (irb) you can run:
|
121
|
+
|
122
|
+
```bash
|
123
|
+
bin/console
|
124
|
+
```
|
105
125
|
|
106
126
|
## Contributing
|
107
127
|
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'lare_round'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/lare_round.gemspec
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'English'
|
4
|
+
lib = File.expand_path('lib', __dir__)
|
4
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
6
|
require 'lare_round/version'
|
6
7
|
|
7
8
|
Gem::Specification.new do |spec|
|
9
|
+
spec.required_ruby_version = '>= 3.0.4'
|
10
|
+
|
8
11
|
spec.name = 'lare_round'
|
9
12
|
spec.version = LareRound::VERSION
|
10
13
|
spec.authors = ['Carsten Wirth']
|
@@ -25,14 +28,9 @@ Gem::Specification.new do |spec|
|
|
25
28
|
spec.homepage = ''
|
26
29
|
spec.license = 'MIT'
|
27
30
|
|
28
|
-
spec.
|
31
|
+
spec.metadata = { 'rubygems_mfa_required' => 'true' }
|
32
|
+
|
33
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
29
34
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
30
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
31
35
|
spec.require_paths = ['lib']
|
32
|
-
|
33
|
-
spec.add_development_dependency 'bundler'
|
34
|
-
spec.add_development_dependency 'minitest'
|
35
|
-
spec.add_development_dependency 'rake'
|
36
|
-
spec.add_development_dependency 'rubocop'
|
37
|
-
spec.add_development_dependency 'simplecov'
|
38
36
|
end
|
data/lib/lare_round/version.rb
CHANGED
data/lib/lare_round.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'bigdecimal'
|
4
4
|
|
5
|
+
# module providing the entry point to the rounding logic
|
5
6
|
module LareRound
|
6
7
|
def self.round(values, precision)
|
7
8
|
# although it is the senders responsibility to ensure that correct messages
|
@@ -51,25 +52,18 @@ module LareRound
|
|
51
52
|
raise LareRoundError, error
|
52
53
|
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
55
|
+
return if values.map { |i| i.is_a? BigDecimal }.reject { |i| i == true }.empty?
|
56
|
+
|
57
|
+
warn <<-WARNING.strip.gsub(/\s+/, ' ')
|
58
|
+
values contains non decimal values,
|
59
|
+
you might loose precision or even get wrong rounding results
|
60
|
+
WARNING
|
61
61
|
end
|
62
62
|
|
63
63
|
def handle_precision_errors(precision)
|
64
64
|
raise LareRoundError, 'precision must not be nil' if precision.nil?
|
65
|
-
|
66
|
-
|
67
|
-
raise LareRoundError, 'precision must be a number'
|
68
|
-
end
|
69
|
-
|
70
|
-
if precision.negative?
|
71
|
-
raise LareRoundError, 'precision must be greater or equal to 0'
|
72
|
-
end
|
65
|
+
raise LareRoundError, 'precision must be a number' unless precision.is_a?(Numeric)
|
66
|
+
raise LareRoundError, 'precision must be greater or equal to 0' if precision.negative?
|
73
67
|
end
|
74
68
|
|
75
69
|
Struct.new(
|
@@ -89,7 +83,7 @@ module LareRound
|
|
89
83
|
mrc.rounded_total = array_of_values.reduce(:+)
|
90
84
|
.round(precision) * mrc.decimal_shift
|
91
85
|
mrc.array_of_values = array_of_values.map do |v|
|
92
|
-
|
86
|
+
v.is_a?(BigDecimal) ? v : BigDecimal(v.to_s)
|
93
87
|
end
|
94
88
|
mrc.unrounded_values = array_of_values.map { |v| v * mrc.decimal_shift }
|
95
89
|
|
data/test/lare_round_test.rb
CHANGED
@@ -4,13 +4,13 @@ require_relative 'test_helper'
|
|
4
4
|
require 'bigdecimal'
|
5
5
|
require 'securerandom'
|
6
6
|
|
7
|
-
class LareRoundTest <
|
7
|
+
class LareRoundTest < Minitest::Spec
|
8
8
|
def test_has_static_method_round
|
9
9
|
assert_equal(true, LareRound.respond_to?(:round))
|
10
10
|
end
|
11
11
|
|
12
12
|
def create_big_decimal(precision, digit)
|
13
|
-
BigDecimal(
|
13
|
+
BigDecimal("0.#{'3' * precision}#{digit}")
|
14
14
|
end
|
15
15
|
|
16
16
|
(1..9).each do |digit|
|
@@ -36,11 +36,9 @@ class LareRoundTest < MiniTest::Spec
|
|
36
36
|
with precision of #{precision} if passed as hash
|
37
37
|
TESTMETHOD
|
38
38
|
define_method method_name do
|
39
|
-
hash =
|
40
|
-
(
|
41
|
-
|
42
|
-
end
|
43
|
-
]
|
39
|
+
hash = (1..items).to_h do |x|
|
40
|
+
[x, create_big_decimal(precision, digit)]
|
41
|
+
end
|
44
42
|
rounded_total = hash.values.reduce(:+).round(precision)
|
45
43
|
assert_equal(
|
46
44
|
rounded_total,
|
@@ -54,14 +52,12 @@ class LareRoundTest < MiniTest::Spec
|
|
54
52
|
if passed as hash should not change order
|
55
53
|
TESTMETHOD
|
56
54
|
define_method method_name do
|
57
|
-
hash =
|
58
|
-
(
|
59
|
-
|
60
|
-
end
|
61
|
-
]
|
55
|
+
hash = (1..items).map.with_index do |x, i|
|
56
|
+
[x, create_big_decimal(precision, digit) + BigDecimal(i)]
|
57
|
+
end.to_h
|
62
58
|
rounded_hash = LareRound.round(hash.clone, precision)
|
63
|
-
hash.
|
64
|
-
assert((((hash[key] - rounded_hash[key]) * 10**precision).abs < 1))
|
59
|
+
hash.each_key do |key|
|
60
|
+
assert((((hash[key] - rounded_hash[key]) * (10**precision)).abs < 1))
|
65
61
|
end
|
66
62
|
end
|
67
63
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,85 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lare_round
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carsten Wirth
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: minitest
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
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: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rubocop
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: simplecov
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
10
|
+
date: 2025-01-31 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
83
12
|
description: |2
|
84
13
|
A collection of BigDecimal items e.g. invoice items can be rounded for
|
85
14
|
displaying them in views. Rounding may apply a rounding error to the
|
@@ -91,18 +20,21 @@ description: |2
|
|
91
20
|
total sum for fractionated parts thus eliminating this confusion.
|
92
21
|
email:
|
93
22
|
- cwirth79@web.de
|
94
|
-
executables:
|
23
|
+
executables:
|
24
|
+
- console
|
95
25
|
extensions: []
|
96
26
|
extra_rdoc_files: []
|
97
27
|
files:
|
28
|
+
- ".github/workflows/ci.yml"
|
98
29
|
- ".gitignore"
|
99
30
|
- ".rubocop.yml"
|
100
31
|
- ".rubocop_todo.yml"
|
101
|
-
- ".
|
32
|
+
- ".ruby-version"
|
102
33
|
- Gemfile
|
103
34
|
- LICENSE.txt
|
104
35
|
- README.md
|
105
36
|
- Rakefile
|
37
|
+
- bin/console
|
106
38
|
- lare_round.gemspec
|
107
39
|
- lib/lare_round.rb
|
108
40
|
- lib/lare_round/version.rb
|
@@ -111,8 +43,8 @@ files:
|
|
111
43
|
homepage: ''
|
112
44
|
licenses:
|
113
45
|
- MIT
|
114
|
-
metadata:
|
115
|
-
|
46
|
+
metadata:
|
47
|
+
rubygems_mfa_required: 'true'
|
116
48
|
rdoc_options: []
|
117
49
|
require_paths:
|
118
50
|
- lib
|
@@ -120,18 +52,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
52
|
requirements:
|
121
53
|
- - ">="
|
122
54
|
- !ruby/object:Gem::Version
|
123
|
-
version:
|
55
|
+
version: 3.0.4
|
124
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
57
|
requirements:
|
126
58
|
- - ">="
|
127
59
|
- !ruby/object:Gem::Version
|
128
60
|
version: '0'
|
129
61
|
requirements: []
|
130
|
-
|
131
|
-
rubygems_version: 2.7.8
|
132
|
-
signing_key:
|
62
|
+
rubygems_version: 3.6.3
|
133
63
|
specification_version: 4
|
134
64
|
summary: gem for rounding BigDecimal items by preserving its sum
|
135
|
-
test_files:
|
136
|
-
- test/lare_round_test.rb
|
137
|
-
- test/test_helper.rb
|
65
|
+
test_files: []
|