money_extensions 1.3.0 → 1.5.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/.git-blame-ignore-revs +12 -0
- data/.github/workflows/ruby.yml +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +12 -4
- data/lib/money_extensions/split_between.rb +20 -9
- data/lib/money_extensions/version.rb +1 -1
- data/spec/money_spec.rb +21 -1
- data/spec/support/coverage_loader.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44dd97a83c21781a83f266f6ee1249fc2849e6ae4867a02e6666c15afc9cc018
|
4
|
+
data.tar.gz: d3d928e7816db0241eb60314f411bfff47f76b4466b34aab9fb5f9493a18d8f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f972a2dd5e9b4947e8c996bb920a5618c8058d795e10ccde6bddb28b1e7f82d9c5c82a9dd261cd6ea25bf61649cce5ab8043bda47522fceb59d8683810971ed
|
7
|
+
data.tar.gz: 7cb882d57518a6ce6f76df8aa7cc9127985ea081532c16e2eb6b5e7dc777908bd53ffecd705975bcd28bdd5ddc115aa35a2ff98a72b45fd824c7d38cfc71e55a
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# The commits that did automated reformatting. You can ignore them
|
2
|
+
# during git-blame with `--ignore-rev` or `--ignore-revs-file`.
|
3
|
+
# You can also globally configure GIT with the following command
|
4
|
+
#
|
5
|
+
# $ git config --add 'blame.ignoreRevsFile' '.git-blame-ignore-revs'
|
6
|
+
#
|
7
|
+
# Example entries:
|
8
|
+
#
|
9
|
+
# <full commit hash> # initial black-format
|
10
|
+
# <full commit hash> # rename something internal
|
11
|
+
|
12
|
+
0cdd3ad2866c4ebc989284f265bb968484547a08 # Pretty CHANGELOG.md
|
data/.github/workflows/ruby.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.
|
1
|
+
3.2.0
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.5.0
|
4
|
+
|
5
|
+
- [PLAT-1175] Update to Ruby 3.2
|
6
|
+
|
7
|
+
## 1.4.0
|
8
|
+
|
9
|
+
- [PLAT-401] Optimise split_between when we are doing so with numbers
|
10
|
+
|
3
11
|
## 1.3.0
|
4
12
|
|
5
13
|
- [PLAT-377] Improve bundler require time for the Gem
|
@@ -14,10 +22,10 @@
|
|
14
22
|
|
15
23
|
## 1.0.0
|
16
24
|
|
17
|
-
|
25
|
+
- [TT-5845] Update to support money gems >= 6
|
18
26
|
|
19
27
|
## 0.1.0
|
20
28
|
|
21
|
-
|
22
|
-
|
23
|
-
|
29
|
+
- Using Fixnum is deprecated
|
30
|
+
- Drop rails 2 and old ruby support
|
31
|
+
- Use coverage kit to enforce maximum coverage
|
@@ -1,11 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module SplitBetween
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
4
6
|
# Override division -- should not do it, but use split_between
|
5
7
|
def /(*_params)
|
6
8
|
raise "Division of money NOT allowed - use 'split_between' to avoid rounding errors"
|
7
9
|
end
|
8
10
|
|
11
|
+
module ClassMethods
|
12
|
+
def split_evenly_between(cents, number)
|
13
|
+
Money::Allocation.generate(cents, number).map { |num| Money.from_cents(num) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def split_evenly_between(number)
|
18
|
+
Money.split_evenly_between(cents, number)
|
19
|
+
end
|
20
|
+
|
9
21
|
# Split the money between the specified number - and return an array of money
|
10
22
|
# Remainder in splits are given to the highest value e.g.
|
11
23
|
# $2.00 splits [40, 81, 40] into [49, 102, 49]
|
@@ -13,11 +25,7 @@ module SplitBetween
|
|
13
25
|
def split_between(params)
|
14
26
|
# if just a number is passed in, then money is split equally
|
15
27
|
if params.is_a?(Integer)
|
16
|
-
|
17
|
-
raise ArgumentError, 'Can only split up over a positive number' if divisor < 1
|
18
|
-
|
19
|
-
rounded_split = lossy_divide(divisor)
|
20
|
-
results = Array.new(divisor, rounded_split) # Create with 'divisor' num elements
|
28
|
+
results = split_evenly_between(params)
|
21
29
|
|
22
30
|
# if an array of monies is passed in, then split in proportions
|
23
31
|
elsif params.is_a?(Array)
|
@@ -36,14 +44,17 @@ module SplitBetween
|
|
36
44
|
results.map! do |ratio|
|
37
45
|
::Money.new((cents * (ratio.to_f / total)).round)
|
38
46
|
end
|
47
|
+
|
48
|
+
# Distribute rounding to max absolute to avoid a $0 amount getting the rounding
|
49
|
+
remainder = self - results.total_money
|
50
|
+
if !remainder.zero?
|
51
|
+
biggest_value_index = results.index(results.max_by(&:abs))
|
52
|
+
results[biggest_value_index] += remainder
|
53
|
+
end
|
39
54
|
else
|
40
55
|
raise 'Either a Integer or array has to be passed in for splitting money'
|
41
56
|
end
|
42
57
|
|
43
|
-
# Distribute rounding to max absolute to avoid a $0 amount getting the rounding
|
44
|
-
biggest_value_index = results.index(results.max_by(&:abs))
|
45
|
-
results[biggest_value_index] += self - results.total_money
|
46
|
-
|
47
58
|
results
|
48
59
|
end
|
49
60
|
end
|
data/spec/money_spec.rb
CHANGED
@@ -5,6 +5,26 @@ describe Money do
|
|
5
5
|
expect { Money.new(50)/10 }.to raise_error(RuntimeError)
|
6
6
|
end
|
7
7
|
|
8
|
+
it 'should split evenly between' do
|
9
|
+
money = Money.new(100)
|
10
|
+
|
11
|
+
expect(money.split_evenly_between(3)).to eq [34,33,33].map{ |i| Money.new(i) }
|
12
|
+
expect(money.split_evenly_between(3).sum(Money.zero)).to eq money
|
13
|
+
|
14
|
+
expect(money.split_evenly_between(6)).to eq [17,17,17,17,16,16].map{ |i| Money.new(i) }
|
15
|
+
expect(money.split_evenly_between(6).sum(Money.zero)).to eq money
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should split evenly between negative numbers' do
|
19
|
+
money = Money.new(-100)
|
20
|
+
|
21
|
+
expect(money.split_evenly_between(3)).to eq [-33,-33,-34].map{ |i| Money.new(i) }
|
22
|
+
expect(money.split_evenly_between(3).sum(Money.zero)).to eq money
|
23
|
+
|
24
|
+
expect(money.split_evenly_between(6)).to eq [-16,-16,-17,-17,-17,-17].map{ |i| Money.new(i) }
|
25
|
+
expect(money.split_evenly_between(6).sum(Money.zero)).to eq money
|
26
|
+
end
|
27
|
+
|
8
28
|
it "should split money correctly" do
|
9
29
|
money = Money.new(100)
|
10
30
|
|
@@ -21,7 +41,7 @@ describe Money do
|
|
21
41
|
expect(money.split_between([1,2])).to eq [33,67].map{ |i| Money.new(i)}
|
22
42
|
|
23
43
|
money_negative = Money.new(-100)
|
24
|
-
expect(money_negative.split_between(3)).to eq [-
|
44
|
+
expect(money_negative.split_between(3)).to eq [-33,-33,-34].map{ |i| Money.new(i)}
|
25
45
|
expect(money_negative.split_between([1,2,2,5])).to eq [-10,-20,-20,-50].map{ |i| Money.new(i)}
|
26
46
|
expect(money_negative.split_between([1,2])).to eq [-33,-67].map{ |i| Money.new(i)}
|
27
47
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Noack
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: money
|
@@ -143,6 +143,7 @@ executables: []
|
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
|
+
- ".git-blame-ignore-revs"
|
146
147
|
- ".github/dependabot.yml"
|
147
148
|
- ".github/workflows/release.yml"
|
148
149
|
- ".github/workflows/ruby.yml"
|
@@ -195,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
196
|
- !ruby/object:Gem::Version
|
196
197
|
version: '0'
|
197
198
|
requirements: []
|
198
|
-
rubygems_version: 3.
|
199
|
+
rubygems_version: 3.4.1
|
199
200
|
signing_key:
|
200
201
|
specification_version: 4
|
201
202
|
summary: Set of extensions to the money gem used by TravelLink Technology.
|