shopify-money 0.11.6 → 0.11.7

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: c122266eee7c568beac30f07b25eb000fec13b3b516f64d86b2a2a7fe0e062cb
4
- data.tar.gz: 9462466de4c1d0d1fbaadbc4ad84bb26a53bbc2559369442c54c50aba6287fe6
3
+ metadata.gz: 47f4b881badba413ac46f65f7aff7457c2ec502f4ab743467393fe8b9e51b5d7
4
+ data.tar.gz: 5333ce81e4360e10349a2cb22966794a21cfca24ab3158c0376af374105062fe
5
5
  SHA512:
6
- metadata.gz: a2972d4815fb48705773b50a0561e2cd4c662c53e42f05627220def37d01a25de850234c965c4b25fd47a8d3980b173c5ebc17724671bbb95c278bf1cfe52b14
7
- data.tar.gz: 00d5a604adf5bd570378e424238a3977f3602b276a3c1d84bff2670853bd59eb27d9db2cac5e39b0208fb91e11f163b18e2ab2d40a90b86d656511e1ce9421d8
6
+ metadata.gz: 19d044e6aaa05b55fd3067eb7a51ba2fb140baf1eb221fcd2e3b7eb594e9025f45e0ff4bd0a490452b168e0211da85171493f11664a62ececb30f73b80850c73
7
+ data.tar.gz: df0d59334c90dd06236873fa42def1bb92b2657418a137a3d8a501e15b88d8c9ca325cdb92f0cd74d1af81eefb114993c5f6f0cab93d3b9ed20afa8a8ee4c412
data/dev.yml CHANGED
@@ -3,7 +3,7 @@
3
3
  ---
4
4
  name: money
5
5
  up:
6
- - ruby: 2.3.3
6
+ - ruby: 2.5.3
7
7
  - bundler
8
8
  commands:
9
9
  test: bundle exec rspec
data/lib/money/money.rb CHANGED
@@ -340,14 +340,32 @@ class Money
340
340
  # @example
341
341
  # Money.new(100, "USD").split(3) #=> [Money.new(34), Money.new(33), Money.new(33)]
342
342
  def split(num)
343
+ calculate_splits(num).sum { |value, count| Array.new(count, value) }
344
+ end
345
+
346
+ # Calculate the splits evenly without losing pennies.
347
+ # Returns the number of high and low splits and the value of the high and low splits.
348
+ # Where high represents the Money value with the extra penny
349
+ # and low a Money without the extra penny.
350
+ #
351
+ # @param [2] number of parties.
352
+ #
353
+ # @return [Hash<Money, Integer>]
354
+ #
355
+ # @example
356
+ # Money.new(100, "USD").calculate_splits(3) #=> {Money.new(34) => 1, Money.new(33) => 2}
357
+ def calculate_splits(num)
343
358
  raise ArgumentError, "need at least one party" if num < 1
344
359
  subunits = self.subunits
345
360
  low = Money.from_subunits(subunits / num, currency)
346
361
  high = Money.from_subunits(low.subunits + 1, currency)
347
362
 
348
- remainder = subunits % num
363
+ num_high = subunits % num
349
364
 
350
- return Array.new(remainder, high) + Array.new(num - remainder, low)
365
+ {}.tap do |result|
366
+ result[high] = num_high if num_high > 0
367
+ result[low] = num - num_high
368
+ end
351
369
  end
352
370
 
353
371
  # Clamps the value to be within the specified minimum and maximum. Returns
data/lib/money/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Money
2
- VERSION = "0.11.6"
2
+ VERSION = "0.11.7"
3
3
  end
data/money.gemspec CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.add_development_dependency("rails", "~> 5.0")
18
18
  s.add_development_dependency("rspec", "~> 3.2")
19
19
  s.add_development_dependency("database_cleaner", "~> 1.6")
20
- s.add_development_dependency("sqlite3", "~> 1.3")
21
- s.add_development_dependency("bigdecimal", ">= 1.3.2")
20
+ s.add_development_dependency("sqlite3", "~> 1.3.6")
21
+ s.add_development_dependency("bigdecimal", "~> 1.3.2")
22
22
 
23
23
  s.files = `git ls-files`.split($/)
24
24
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
data/spec/money_spec.rb CHANGED
@@ -618,6 +618,25 @@ RSpec.describe "Money" do
618
618
  end
619
619
  end
620
620
 
621
+ describe "calculate_splits" do
622
+ specify "#calculate_splits gives 1 cent to both people if we start with 2" do
623
+ actual = Money.new(0.02, 'CAD').calculate_splits(2)
624
+
625
+ expect(actual).to eq({
626
+ Money.new(0.01, 'CAD') => 2,
627
+ })
628
+ end
629
+
630
+ specify "#calculate_splits gives an extra penny to one" do
631
+ actual = Money.new(0.04, 'CAD').calculate_splits(3)
632
+
633
+ expect(actual).to eq({
634
+ Money.new(0.02, 'CAD') => 1,
635
+ Money.new(0.01, 'CAD') => 2,
636
+ })
637
+ end
638
+ end
639
+
621
640
  describe "fraction" do
622
641
  specify "#fraction needs a positive rate" do
623
642
  expect {Money.new(1).fraction(-0.5)}.to raise_error(ArgumentError)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.6
4
+ version: 0.11.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-20 00:00:00.000000000 Z
11
+ date: 2019-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,26 +86,26 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '1.3'
89
+ version: 1.3.6
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '1.3'
96
+ version: 1.3.6
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: bigdecimal
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
103
  version: 1.3.2
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 1.3.2
111
111
  description: Manage money in Shopify with a class that wont lose pennies during division!