shopify-money 4.1.1 → 4.2.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/Gemfile.lock +1 -1
- data/lib/money/currency.rb +15 -3
- data/lib/money/helpers.rb +14 -0
- data/lib/money/money.rb +19 -13
- data/lib/money/version.rb +1 -1
- data/sig/money/helpers.rbs +3 -0
- data/sig/money.rbs +2 -1
- data/spec/currency_spec.rb +22 -0
- data/spec/money_column_spec.rb +4 -0
- data/spec/money_spec.rb +92 -13
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 916394c2e17429314246e0d6052b1d03421d2069663d011e89489ded29b7c43b
|
|
4
|
+
data.tar.gz: 77c3aa7b48879983be28135da3d3910956504869e1cf8bbe5655cec2ac20084f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 25d36cd3cb93a98fc4e25bed927878a14ae6bc1b7d5c15135b654690409dd6ed3d077180abc8402dfba535e605702446fa3f3dd9b8b1461f8a224572dbe206a5
|
|
7
|
+
data.tar.gz: af819e6cf489243439af8aa448d97853799b37dc7f770d0044bd3c1a8ba1e5aa04e0fd55b353c2155e88726f52454dbe3496240ec995a090c543eaacd0e22df5
|
data/Gemfile.lock
CHANGED
data/lib/money/currency.rb
CHANGED
|
@@ -11,9 +11,21 @@ class Money
|
|
|
11
11
|
|
|
12
12
|
class << self
|
|
13
13
|
def new(currency_iso)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
currency = @@loaded_currencies[currency_iso]
|
|
15
|
+
return currency if currency
|
|
16
|
+
|
|
17
|
+
downcase_iso = currency_iso.to_s.downcase
|
|
18
|
+
raise UnknownCurrency, "Currency can't be blank" if downcase_iso.empty?
|
|
19
|
+
|
|
20
|
+
@@loaded_currencies[downcase_iso] || @@mutex.synchronize do
|
|
21
|
+
currency = super(downcase_iso)
|
|
22
|
+
# NOTE: camelcase currency spelling (ex "uSd") is never memoized and will pay the downcase tax on every call.
|
|
23
|
+
# This is intentional, as it is not a valid currency spelling. User input should be normalized in production code.
|
|
24
|
+
[downcase_iso.upcase, downcase_iso, downcase_iso.upcase.to_sym, downcase_iso.to_sym].each do |spelling|
|
|
25
|
+
@@loaded_currencies[spelling] = currency
|
|
26
|
+
end
|
|
27
|
+
currency
|
|
28
|
+
end
|
|
17
29
|
end
|
|
18
30
|
alias_method :find!, :new
|
|
19
31
|
|
data/lib/money/helpers.rb
CHANGED
|
@@ -8,6 +8,10 @@ class Money
|
|
|
8
8
|
|
|
9
9
|
DECIMAL_ZERO = BigDecimal(0).freeze
|
|
10
10
|
MAX_DECIMAL = 21
|
|
11
|
+
# Largest supported BigDecimal#exponent (number of integer digits). Rejects
|
|
12
|
+
# values like "1e1000000000" that are cheap to store but expand into huge
|
|
13
|
+
# Integers or Strings in #subunits or #to_s.
|
|
14
|
+
MAX_INTEGER_DIGITS = 1000
|
|
11
15
|
|
|
12
16
|
def value_to_decimal(num)
|
|
13
17
|
value =
|
|
@@ -30,9 +34,19 @@ class Money
|
|
|
30
34
|
raise ArgumentError, "could not parse as decimal #{num.inspect}"
|
|
31
35
|
end
|
|
32
36
|
return DECIMAL_ZERO if value.sign == BigDecimal::SIGN_NEGATIVE_ZERO
|
|
37
|
+
# Check at parse time, before callers such as Converter#from_subunits do
|
|
38
|
+
# arithmetic on the value: BigDecimal division with a huge exponent can
|
|
39
|
+
# hang the process before Money#initialize gets to reject the result.
|
|
40
|
+
check_magnitude!(value)
|
|
33
41
|
value
|
|
34
42
|
end
|
|
35
43
|
|
|
44
|
+
def check_magnitude!(value)
|
|
45
|
+
return if value.exponent <= MAX_INTEGER_DIGITS
|
|
46
|
+
|
|
47
|
+
raise ArgumentError, "value exceeds the supported range of #{MAX_INTEGER_DIGITS} integer digits"
|
|
48
|
+
end
|
|
49
|
+
|
|
36
50
|
def value_to_currency(currency)
|
|
37
51
|
case currency
|
|
38
52
|
when Money::Currency, Money::NullCurrency
|
data/lib/money/money.rb
CHANGED
|
@@ -5,14 +5,11 @@ require 'json'
|
|
|
5
5
|
|
|
6
6
|
class Money
|
|
7
7
|
include Comparable
|
|
8
|
-
extend Forwardable
|
|
9
8
|
|
|
10
9
|
NULL_CURRENCY = NullCurrency.new.freeze
|
|
11
10
|
|
|
12
11
|
attr_reader :value, :currency
|
|
13
12
|
|
|
14
|
-
def_delegators :@value, :zero?, :nonzero?, :positive?, :negative?, :to_i, :to_f, :hash
|
|
15
|
-
|
|
16
13
|
class ReverseOperationProxy
|
|
17
14
|
include Comparable
|
|
18
15
|
|
|
@@ -127,6 +124,7 @@ class Money
|
|
|
127
124
|
def initialize(value, currency)
|
|
128
125
|
raise ArgumentError if value.nan?
|
|
129
126
|
raise ArgumentError if value.infinite?
|
|
127
|
+
Helpers.check_magnitude!(value)
|
|
130
128
|
|
|
131
129
|
@currency = currency
|
|
132
130
|
@value = BigDecimal(value.round(@currency.minor_units))
|
|
@@ -209,6 +207,14 @@ class Money
|
|
|
209
207
|
value == other.value
|
|
210
208
|
end
|
|
211
209
|
|
|
210
|
+
def zero? = @value.zero?
|
|
211
|
+
def nonzero? = @value.nonzero?
|
|
212
|
+
def positive? = @value.positive?
|
|
213
|
+
def negative? = @value.negative?
|
|
214
|
+
def to_i = @value.to_i
|
|
215
|
+
def to_f = @value.to_f
|
|
216
|
+
def hash = @value.hash
|
|
217
|
+
|
|
212
218
|
def coerce(other)
|
|
213
219
|
raise TypeError, "Money can't be coerced into #{other.class}" unless other.is_a?(Numeric)
|
|
214
220
|
[ReverseOperationProxy.new(other), self]
|
|
@@ -343,21 +349,21 @@ class Money
|
|
|
343
349
|
Splitter.new(self, num).split.dup
|
|
344
350
|
end
|
|
345
351
|
|
|
346
|
-
# Clamps the value to be within the specified minimum and maximum
|
|
347
|
-
#
|
|
348
|
-
#
|
|
352
|
+
# Clamps the value to be within the specified minimum and maximum, or within
|
|
353
|
+
# an inclusive range. A nil minimum or maximum leaves that bound open.
|
|
354
|
+
# Returns self if the value is within bounds, otherwise a new Money object
|
|
355
|
+
# with the closest minimum or maximum value.
|
|
349
356
|
#
|
|
350
357
|
# @example
|
|
351
358
|
# Money.new(50, "CAD").clamp(1, 100) #=> Money.new(50, "CAD")
|
|
352
|
-
#
|
|
359
|
+
# Money.new(50, "CAD").clamp(1..100) #=> Money.new(50, "CAD")
|
|
360
|
+
# Money.new(50, "CAD").clamp(1, nil) #=> Money.new(50, "CAD")
|
|
361
|
+
# Money.new(50, "CAD").clamp(nil, 40) #=> Money.new(40, "CAD")
|
|
353
362
|
# Money.new(120, "CAD").clamp(0, 100) #=> Money.new(100, "CAD")
|
|
354
|
-
def clamp(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
clamped_value = min if value < min
|
|
358
|
-
clamped_value = max if value > max
|
|
363
|
+
def clamp(...)
|
|
364
|
+
clamped_value = value.clamp(...)
|
|
359
365
|
|
|
360
|
-
if clamped_value
|
|
366
|
+
if clamped_value == value
|
|
361
367
|
self
|
|
362
368
|
else
|
|
363
369
|
Money.new(clamped_value, currency)
|
data/lib/money/version.rb
CHANGED
data/sig/money/helpers.rbs
CHANGED
|
@@ -4,12 +4,15 @@ class Money
|
|
|
4
4
|
module Helpers
|
|
5
5
|
DECIMAL_ZERO: BigDecimal
|
|
6
6
|
MAX_DECIMAL: Integer
|
|
7
|
+
MAX_INTEGER_DIGITS: Integer
|
|
7
8
|
|
|
8
9
|
def self.value_to_decimal: (Numeric | String | Money | nil num) -> BigDecimal
|
|
9
10
|
def self.value_to_currency: (String | Currency | NullCurrency | nil currency) -> (Currency | NullCurrency)
|
|
11
|
+
def self.check_magnitude!: (BigDecimal value) -> void
|
|
10
12
|
|
|
11
13
|
# Instance methods (via extend self)
|
|
12
14
|
def value_to_decimal: (Numeric | String | Money | nil num) -> BigDecimal
|
|
13
15
|
def value_to_currency: (String | Currency | NullCurrency | nil currency) -> (Currency | NullCurrency)
|
|
16
|
+
def check_magnitude!: (BigDecimal value) -> void
|
|
14
17
|
end
|
|
15
18
|
end
|
data/sig/money.rbs
CHANGED
|
@@ -75,7 +75,8 @@ class Money
|
|
|
75
75
|
def floor: () -> Money
|
|
76
76
|
def round: (?Integer ndigits) -> Money
|
|
77
77
|
def fraction: (Numeric rate) -> Money
|
|
78
|
-
def clamp: (Numeric min, Numeric max) -> Money
|
|
78
|
+
def clamp: (Numeric? min, Numeric? max) -> Money
|
|
79
|
+
| (Range[Numeric] range) -> Money
|
|
79
80
|
|
|
80
81
|
def allocate: (Array[Numeric] splits, ?Symbol strategy) -> Array[Money]
|
|
81
82
|
def allocate_max_amounts: (Array[Money] maximums) -> Array[Money]
|
data/spec/currency_spec.rb
CHANGED
|
@@ -58,6 +58,28 @@ RSpec.describe "Currency" do
|
|
|
58
58
|
expect(Money::Currency.new('usd').iso_code).to eq('USD')
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
+
it "is constructable with a mixed-case string" do
|
|
62
|
+
expect(Money::Currency.new('UsD').iso_code).to eq('USD')
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "returns the same cached instance regardless of spelling" do
|
|
66
|
+
expect(Money::Currency.new('USD')).to be(Money::Currency.new('usd'))
|
|
67
|
+
expect(Money::Currency.new(:usd)).to be(Money::Currency.new('usd'))
|
|
68
|
+
expect(Money::Currency.new(:USD)).to be(Money::Currency.new('usd'))
|
|
69
|
+
expect(Money::Currency.new('UsD')).to be(Money::Currency.new('usd'))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "memoizes only canonical spellings, keeping the cache bounded" do
|
|
73
|
+
Money::Currency.reset_loaded_currencies
|
|
74
|
+
Money::Currency.new('usd')
|
|
75
|
+
Money::Currency.new('USD')
|
|
76
|
+
Money::Currency.new(:usd)
|
|
77
|
+
Money::Currency.new('UsD')
|
|
78
|
+
|
|
79
|
+
cached_keys = Money::Currency.class_variable_get(:@@loaded_currencies).keys
|
|
80
|
+
expect(cached_keys).to contain_exactly('usd', 'USD', :usd, :USD)
|
|
81
|
+
end
|
|
82
|
+
|
|
61
83
|
it "raises when the currency is invalid" do
|
|
62
84
|
expect { Money::Currency.new('yyy') }.to raise_error(Money::Currency::UnknownCurrency)
|
|
63
85
|
end
|
data/spec/money_column_spec.rb
CHANGED
|
@@ -88,6 +88,10 @@ RSpec.describe 'MoneyColumn' do
|
|
|
88
88
|
expect(record.price.currency.to_s).to eq('JPY')
|
|
89
89
|
end
|
|
90
90
|
|
|
91
|
+
it 'raises when assigned an amount beyond the supported magnitude' do
|
|
92
|
+
expect { MoneyRecord.new(price: "1e1000000000") }.to raise_error(ArgumentError, /supported range/)
|
|
93
|
+
end
|
|
94
|
+
|
|
91
95
|
it 'duplicating the record keeps the money values' do
|
|
92
96
|
expect(MoneyRecord.new(price: money).clone.price).to eq(money)
|
|
93
97
|
expect(MoneyRecord.new(price: money).dup.price).to eq(money)
|
data/spec/money_spec.rb
CHANGED
|
@@ -465,6 +465,54 @@ RSpec.describe "Money" do
|
|
|
465
465
|
expect { Money.new(-Float::INFINITY) }.to raise_error(ArgumentError)
|
|
466
466
|
end
|
|
467
467
|
|
|
468
|
+
describe "magnitude bound" do
|
|
469
|
+
it "raises when constructed with an exponent-form string beyond the supported range" do
|
|
470
|
+
expect { Money.new("1e1000000000", "USD") }.to raise_error(ArgumentError, /supported range/)
|
|
471
|
+
expect { Money.new("-1e1000000000", "USD") }.to raise_error(ArgumentError, /supported range/)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
it "raises when constructed with an out-of-range BigDecimal" do
|
|
475
|
+
expect { Money.new(BigDecimal("1e1000"), "USD") }.to raise_error(ArgumentError, /supported range/)
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
it "raises when converting out-of-range subunits back to money" do
|
|
479
|
+
expect { Money.from_subunits("1e1000000000", "USD") }.to raise_error(ArgumentError, /supported range/)
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
it "raises before dividing when out-of-range subunits have a very large exponent" do
|
|
483
|
+
# BigDecimal division on this value does not return in reasonable time,
|
|
484
|
+
# so the bound must apply before Converter#from_subunits divides.
|
|
485
|
+
expect { Money.from_subunits("1e100000000000", "USD") }.to raise_error(ArgumentError, /supported range/)
|
|
486
|
+
expect { Money.from_subunits(BigDecimal("1e100000000000"), "USD") }.to raise_error(ArgumentError, /supported range/)
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
it "raises when arithmetic leaves the supported range" do
|
|
490
|
+
expect { Money.new("1e999", "USD") * 10 }.to raise_error(ArgumentError, /supported range/)
|
|
491
|
+
end
|
|
492
|
+
|
|
493
|
+
it "raises when loaded from YAML with an out-of-range value" do
|
|
494
|
+
yaml = "--- !ruby/object:Money\nvalue: '1e1000000000'\ncurrency: USD\n"
|
|
495
|
+
expect { yaml_load(yaml) }.to raise_error(ArgumentError, /supported range/)
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
it "accepts the largest in-range value and converts it to subunits unchanged" do
|
|
499
|
+
max = "9" * Money::Helpers::MAX_INTEGER_DIGITS
|
|
500
|
+
expect(Money.new(max, "USD").value).to eq(BigDecimal(max))
|
|
501
|
+
expect(Money.new(max, "USD").subunits).to eq(Integer(max) * 100)
|
|
502
|
+
expect(Money.new("-#{max}", "USD").subunits).to eq(-Integer(max) * 100)
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
it "accepts values larger than a DECIMAL(21,3) column" do
|
|
506
|
+
expect(Money.new("1e25", "USD") * 10**25).to eq(Money.new("1e50", "USD"))
|
|
507
|
+
expect(Money.new("1e25", "USD").subunits).to eq(10**27)
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
it "still accepts ordinary exponent-form strings" do
|
|
511
|
+
expect(Money.new("1.5e3", "USD")).to eq(Money.new(1500, "USD"))
|
|
512
|
+
expect(Money.new("1e-10", "USD")).to eq(Money.new(0, "USD"))
|
|
513
|
+
end
|
|
514
|
+
end
|
|
515
|
+
|
|
468
516
|
it "is comparable with non-money objects" do
|
|
469
517
|
expect(money).not_to eq(nil)
|
|
470
518
|
end
|
|
@@ -1137,26 +1185,57 @@ RSpec.describe "Money" do
|
|
|
1137
1185
|
end
|
|
1138
1186
|
end
|
|
1139
1187
|
|
|
1140
|
-
describe '
|
|
1188
|
+
describe '#clamp' do
|
|
1141
1189
|
let(:max) { 9000 }
|
|
1142
1190
|
let(:min) { -max }
|
|
1191
|
+
let(:money) { Money.new(5000, 'EUR') }
|
|
1192
|
+
|
|
1193
|
+
it 'returns itself if its value is between the minimum and maximum' do
|
|
1194
|
+
expect(money.clamp(min, max)).to equal(money)
|
|
1195
|
+
end
|
|
1196
|
+
|
|
1197
|
+
it 'returns the maximum if its value is larger' do
|
|
1198
|
+
expect(Money.new(max + 1, 'EUR').clamp(min, max)).to eq(Money.new(max, 'EUR'))
|
|
1199
|
+
end
|
|
1200
|
+
|
|
1201
|
+
it 'returns the minimum if its value is smaller' do
|
|
1202
|
+
expect(Money.new(min - 1, 'EUR').clamp(min, max)).to eq(Money.new(min, 'EUR'))
|
|
1203
|
+
end
|
|
1204
|
+
|
|
1205
|
+
it 'accepts a bounded inclusive range' do
|
|
1206
|
+
expect(money.clamp(min..max)).to equal(money)
|
|
1207
|
+
expect(Money.new(min - 1, 'EUR').clamp(min..max)).to eq(Money.new(min, 'EUR'))
|
|
1208
|
+
expect(Money.new(max + 1, 'EUR').clamp(min..max)).to eq(Money.new(max, 'EUR'))
|
|
1209
|
+
end
|
|
1210
|
+
|
|
1211
|
+
it 'accepts an endless range' do
|
|
1212
|
+
expect(Money.new(min - 1, 'EUR').clamp(min..)).to eq(Money.new(min, 'EUR'))
|
|
1213
|
+
end
|
|
1214
|
+
|
|
1215
|
+
it 'accepts a beginless range' do
|
|
1216
|
+
expect(Money.new(max + 1, 'EUR').clamp(..max)).to eq(Money.new(max, 'EUR'))
|
|
1217
|
+
end
|
|
1218
|
+
|
|
1219
|
+
it 'accepts a nil maximum' do
|
|
1220
|
+
expect(money.clamp(min, nil)).to equal(money)
|
|
1221
|
+
expect(Money.new(min - 1, 'EUR').clamp(min, nil)).to eq(Money.new(min, 'EUR'))
|
|
1222
|
+
end
|
|
1223
|
+
|
|
1224
|
+
it 'accepts a nil minimum' do
|
|
1225
|
+
expect(money.clamp(nil, max)).to equal(money)
|
|
1226
|
+
expect(Money.new(max + 1, 'EUR').clamp(nil, max)).to eq(Money.new(max, 'EUR'))
|
|
1227
|
+
end
|
|
1143
1228
|
|
|
1144
|
-
it '
|
|
1145
|
-
money
|
|
1146
|
-
expect(money.value).to eq(5000)
|
|
1147
|
-
expect(money.currency.iso_code).to eq('EUR')
|
|
1229
|
+
it 'accepts two nil bounds' do
|
|
1230
|
+
expect(money.clamp(nil, nil)).to equal(money)
|
|
1148
1231
|
end
|
|
1149
1232
|
|
|
1150
|
-
it '
|
|
1151
|
-
|
|
1152
|
-
expect(money.clamp(min, max).value).to eq(9000)
|
|
1153
|
-
expect(money.clamp(min, max).currency.iso_code).to eq('EUR')
|
|
1233
|
+
it 'rejects an exclusive range' do
|
|
1234
|
+
expect { money.clamp(min...max) }.to raise_error(ArgumentError)
|
|
1154
1235
|
end
|
|
1155
1236
|
|
|
1156
|
-
it '
|
|
1157
|
-
|
|
1158
|
-
expect(money.value).to eq(-9000)
|
|
1159
|
-
expect(money.currency.iso_code).to eq('EUR')
|
|
1237
|
+
it 'raises an ArgumentError if the minimum is greater than the maximum' do
|
|
1238
|
+
expect { money.clamp(max, min) }.to raise_error(ArgumentError)
|
|
1160
1239
|
end
|
|
1161
1240
|
end
|
|
1162
1241
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: shopify-money
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.
|
|
4
|
+
version: 4.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shopify Inc
|
|
@@ -252,7 +252,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
252
252
|
- !ruby/object:Gem::Version
|
|
253
253
|
version: '0'
|
|
254
254
|
requirements: []
|
|
255
|
-
rubygems_version: 4.0.
|
|
255
|
+
rubygems_version: 4.0.23
|
|
256
256
|
specification_version: 4
|
|
257
257
|
summary: Shopify's money gem
|
|
258
258
|
test_files: []
|