money_extensions 1.3.0 → 1.5.0

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: b02ce974d0847bc55984f19c7b7052cac5178e8ca17ed4c17778b7cab20b918d
4
- data.tar.gz: 888c92464c6ba7510c855ac6ee8670c802d41f6e408f2e3cbf21ae793e1fdd7d
3
+ metadata.gz: 44dd97a83c21781a83f266f6ee1249fc2849e6ae4867a02e6666c15afc9cc018
4
+ data.tar.gz: d3d928e7816db0241eb60314f411bfff47f76b4466b34aab9fb5f9493a18d8f4
5
5
  SHA512:
6
- metadata.gz: fbef0196b7903684d86d191ea69dbc1f007949647f5135bbafcaea0ae6f9ee097fa46eee3892e4b9a799909345cabbb6f8b2fbe9681e994198d9e7220dd76991
7
- data.tar.gz: 52f7c134ad23cc2db1d1d040be03abfbf7607bf48731f838004609cf114045c4efdf09f708f3ae8864ff242a38b20e5e79f1a4cfd6f8d4d73cbcd3299784b845
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
@@ -6,7 +6,7 @@ jobs:
6
6
  fail-fast: false
7
7
  matrix:
8
8
  gemfile: [rails60, rails61, rails70]
9
- ruby: ["2.7", "3.0", "3.1"]
9
+ ruby: ["3.0", "3.1", "3.2"]
10
10
  runs-on: ubuntu-latest
11
11
  env:
12
12
  BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.1.0
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
- * [TT-5845] Update to support money gems >= 6
25
+ - [TT-5845] Update to support money gems >= 6
18
26
 
19
27
  ## 0.1.0
20
28
 
21
- * Using Fixnum is deprecated
22
- * Drop rails 2 and old ruby support
23
- * Use coverage kit to enforce maximum coverage
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
- divisor = params
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoneyExtensions
4
- VERSION = '1.3.0'
4
+ VERSION = '1.5.0'
5
5
  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 [-34,-33,-33].map{ |i| Money.new(i)}
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
 
@@ -1,3 +1,3 @@
1
1
  require 'coverage/kit'
2
2
 
3
- Coverage::Kit.setup(minimum_coverage: 86.5)
3
+ Coverage::Kit.setup(minimum_coverage: 87.0)
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.3.0
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: 2022-07-02 00:00:00.000000000 Z
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.3.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.