shopify-money 0.13.1 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/lib/money/allocator.rb +7 -11
- data/lib/money/version.rb +1 -1
- data/money.gemspec +1 -1
- data/spec/allocator_spec.rb +12 -0
- data/spec/core_extensions_spec.rb +1 -1
- data/spec/helpers_spec.rb +2 -2
- data/spec/money_spec.rb +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b02e24a104e1494877174e804e31e40e09b65a274def627d659990efc1aee7de
|
4
|
+
data.tar.gz: e7938ab991812e180b648b0d300da322f4b231168242e645ea665481f5975d31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e327e9a9a15df9319a137763fea1e959c029a8f41d7791509798b3fbdce1d105ce2eeabbfff97aee2e798df55dff55de7d2800e57f83f649efb9580c15db5a8a
|
7
|
+
data.tar.gz: 41ad146ac22f2803728c66547caddc3a108d997c1a010c35d748b264b8f9c90d9130a5e921ab9ac3fff489fb85d830c75a05b020b87a55a0fb8ac7587b40a8a1
|
data/.travis.yml
CHANGED
data/lib/money/allocator.rb
CHANGED
@@ -37,11 +37,8 @@ class Money
|
|
37
37
|
# Money.new(10.01, "USD").allocate([0.5, 0.5], :roundrobin_reverse)
|
38
38
|
# #=> [#<Money value:5.00 currency:USD>, #<Money value:5.01 currency:USD>]
|
39
39
|
def allocate(splits, strategy = :roundrobin)
|
40
|
-
|
41
|
-
|
42
|
-
else
|
43
|
-
allocations = splits.inject(0) { |sum, n| sum + Helpers.value_to_decimal(n) }
|
44
|
-
end
|
40
|
+
splits.map!(&:to_r)
|
41
|
+
allocations = splits.inject(0, :+)
|
45
42
|
|
46
43
|
if (allocations - BigDecimal("1")) > Float::EPSILON
|
47
44
|
raise ArgumentError, "splits add to more than 100%"
|
@@ -81,14 +78,11 @@ class Money
|
|
81
78
|
maximums_total = maximums.reduce(Money.new(0, allocation_currency), :+)
|
82
79
|
|
83
80
|
splits = maximums.map do |max_amount|
|
84
|
-
next(0) if maximums_total.zero?
|
81
|
+
next(Rational(0)) if maximums_total.zero?
|
85
82
|
Money.rational(max_amount, maximums_total)
|
86
83
|
end
|
87
84
|
|
88
|
-
total_allocatable = [
|
89
|
-
value * allocation_currency.subunit_to_unit,
|
90
|
-
maximums_total.value * allocation_currency.subunit_to_unit
|
91
|
-
].min
|
85
|
+
total_allocatable = [maximums_total.subunits, self.subunits].min
|
92
86
|
|
93
87
|
subunits_amounts, left_over = amounts_from_splits(1, splits, total_allocatable)
|
94
88
|
|
@@ -116,10 +110,12 @@ class Money
|
|
116
110
|
end
|
117
111
|
|
118
112
|
def amounts_from_splits(allocations, splits, subunits_to_split = subunits)
|
113
|
+
raise ArgumentError, "All splits values must be of type Rational." unless all_rational?(splits)
|
114
|
+
|
119
115
|
left_over = subunits_to_split
|
120
116
|
|
121
117
|
amounts = splits.collect do |ratio|
|
122
|
-
frac = (
|
118
|
+
frac = (subunits_to_split * ratio / allocations.to_r).floor
|
123
119
|
left_over -= frac
|
124
120
|
frac
|
125
121
|
end
|
data/lib/money/version.rb
CHANGED
data/money.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.add_development_dependency("rspec", "~> 3.2")
|
19
19
|
s.add_development_dependency("database_cleaner", "~> 1.6")
|
20
20
|
s.add_development_dependency("sqlite3", "~> 1.3.6")
|
21
|
-
s.add_development_dependency("bigdecimal", "~> 1.
|
21
|
+
s.add_development_dependency("bigdecimal", "~> 1.4.4")
|
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/allocator_spec.rb
CHANGED
@@ -67,6 +67,12 @@ RSpec.describe "Allocator" do
|
|
67
67
|
specify "#allocate raise ArgumentError when invalid strategy is provided" do
|
68
68
|
expect { new_allocator(0.03).allocate([0.5, 0.5], :bad_strategy_name) }.to raise_error(ArgumentError, "Invalid strategy. Valid options: :roundrobin, :roundrobin_reverse")
|
69
69
|
end
|
70
|
+
|
71
|
+
specify "#allocate does not raise ArgumentError when invalid splits types are provided" do
|
72
|
+
moneys = new_allocator(0.03).allocate([0.5, 0.5], :roundrobin)
|
73
|
+
expect(moneys[0]).to eq(Money.new(0.02))
|
74
|
+
expect(moneys[1]).to eq(Money.new(0.01))
|
75
|
+
end
|
70
76
|
end
|
71
77
|
|
72
78
|
describe 'allocate_max_amounts' do
|
@@ -127,6 +133,12 @@ RSpec.describe "Allocator" do
|
|
127
133
|
new_allocator(3).allocate_max_amounts([Money.empty, Money.empty, Money.empty]),
|
128
134
|
).to eq([Money.empty, Money.empty, Money.empty])
|
129
135
|
end
|
136
|
+
|
137
|
+
specify "#allocate_max_amounts allocates the right amount without rounding error" do
|
138
|
+
expect(
|
139
|
+
new_allocator(24.2).allocate_max_amounts([Money.new(46), Money.new(46), Money.new(50), Money.new(50),Money.new(50)]),
|
140
|
+
).to eq([Money.new(4.6), Money.new(4.6), Money.new(5), Money.new(5), Money.new(5)])
|
141
|
+
end
|
130
142
|
end
|
131
143
|
|
132
144
|
def new_allocator(amount, currency = nil)
|
@@ -56,6 +56,6 @@ RSpec.describe BigDecimal do
|
|
56
56
|
it_should_behave_like "an object supporting to_money"
|
57
57
|
|
58
58
|
it "parses a zero BigDecimal to Money.zero" do
|
59
|
-
expect(BigDecimal
|
59
|
+
expect(BigDecimal("-0.000").to_money).to eq(Money.zero)
|
60
60
|
end
|
61
61
|
end
|
data/spec/helpers_spec.rb
CHANGED
@@ -51,8 +51,8 @@ RSpec.describe Money::Helpers do
|
|
51
51
|
|
52
52
|
it 'returns the bigdecimal representation of numbers while they are deprecated' do
|
53
53
|
expect(Money).to receive(:deprecate).exactly(2).times
|
54
|
-
expect(subject.value_to_decimal('
|
55
|
-
expect(subject.value_to_decimal("
|
54
|
+
expect(subject.value_to_decimal('0.00123e3')).to eq(amount)
|
55
|
+
expect(subject.value_to_decimal("0.123e1")).to eq(amount)
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'raises on invalid object' do
|
data/spec/money_spec.rb
CHANGED
@@ -744,8 +744,8 @@ RSpec.describe "Money" do
|
|
744
744
|
end
|
745
745
|
|
746
746
|
it "accepts Rational number" do
|
747
|
-
expect(Money.from_amount(Rational("999999999999999999.999")).value).to eql(BigDecimal
|
748
|
-
expect(Money.from_amount(Rational("999999999999999999.99")).value).to eql(BigDecimal
|
747
|
+
expect(Money.from_amount(Rational("999999999999999999.999")).value).to eql(BigDecimal("1000000000000000000", Money::Helpers::MAX_DECIMAL))
|
748
|
+
expect(Money.from_amount(Rational("999999999999999999.99")).value).to eql(BigDecimal("999999999999999999.99", Money::Helpers::MAX_DECIMAL))
|
749
749
|
end
|
750
750
|
|
751
751
|
it "raises ArgumentError with unsupported argument" do
|
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.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shopify Inc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 1.
|
103
|
+
version: 1.4.4
|
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
|
-
version: 1.
|
110
|
+
version: 1.4.4
|
111
111
|
description: Manage money in Shopify with a class that wont lose pennies during division!
|
112
112
|
email: gems@shopify.com
|
113
113
|
executables:
|