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 +4 -4
- data/dev.yml +1 -1
- data/lib/money/money.rb +20 -2
- data/lib/money/version.rb +1 -1
- data/money.gemspec +2 -2
- data/spec/money_spec.rb +19 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47f4b881badba413ac46f65f7aff7457c2ec502f4ab743467393fe8b9e51b5d7
|
4
|
+
data.tar.gz: 5333ce81e4360e10349a2cb22966794a21cfca24ab3158c0376af374105062fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19d044e6aaa05b55fd3067eb7a51ba2fb140baf1eb221fcd2e3b7eb594e9025f45e0ff4bd0a490452b168e0211da85171493f11664a62ececb30f73b80850c73
|
7
|
+
data.tar.gz: df0d59334c90dd06236873fa42def1bb92b2657418a137a3d8a501e15b88d8c9ca325cdb92f0cd74d1af81eefb114993c5f6f0cab93d3b9ed20afa8a8ee4c412
|
data/dev.yml
CHANGED
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
|
-
|
363
|
+
num_high = subunits % num
|
349
364
|
|
350
|
-
|
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
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", "
|
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.
|
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:
|
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:
|
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:
|
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!
|