shopify-money 0.14.6 → 0.14.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/money/helpers.rb +4 -0
- data/lib/money/money.rb +21 -4
- data/lib/money/version.rb +1 -1
- data/spec/money_spec.rb +35 -5
- 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: e845ad45cb261ed350e3baff7d3eade222009665caaaa52b033cdf66da011477
|
4
|
+
data.tar.gz: 460242834342b799f8e6a1d4475bcd9e24a251240471bf8c47c9d899b346ab51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 949a72b84927dfa363f433d62ebd54cdc898417cf222697eb04ab87e00d61a36321d777bb7ba01f7a500ae2f95f3b5480b4d0080245616e69424ebbe5a6700ed
|
7
|
+
data.tar.gz: f3ba37b79af054aa738c1ff562eea5d2a43c9e8b3856ee8be487320a59f704cf64f560bf1186e0ebfe2b7b3b4065d638c5d218f6a1ae4efdfbc7fa4521ff057c
|
data/lib/money/helpers.rb
CHANGED
data/lib/money/money.rb
CHANGED
@@ -39,9 +39,18 @@ class Money
|
|
39
39
|
new(cents.round.to_f / 100, currency)
|
40
40
|
end
|
41
41
|
|
42
|
-
def from_subunits(subunits, currency_iso)
|
42
|
+
def from_subunits(subunits, currency_iso, format: :iso4217)
|
43
43
|
currency = Helpers.value_to_currency(currency_iso)
|
44
|
-
|
44
|
+
|
45
|
+
subunit_to_unit_value = if format == :iso4217
|
46
|
+
currency.subunit_to_unit
|
47
|
+
elsif format == :stripe
|
48
|
+
Helpers::STRIPE_SUBUNIT_OVERRIDE.fetch(currency.iso_code, currency.subunit_to_unit)
|
49
|
+
else
|
50
|
+
raise ArgumentError, "unknown format #{format}"
|
51
|
+
end
|
52
|
+
|
53
|
+
value = Helpers.value_to_decimal(subunits) / subunit_to_unit_value
|
45
54
|
new(value, currency)
|
46
55
|
end
|
47
56
|
|
@@ -102,8 +111,16 @@ class Money
|
|
102
111
|
(value * 100).to_i
|
103
112
|
end
|
104
113
|
|
105
|
-
def subunits
|
106
|
-
|
114
|
+
def subunits(format: :iso4217)
|
115
|
+
subunit_to_unit_value = if format == :iso4217
|
116
|
+
@currency.subunit_to_unit
|
117
|
+
elsif format == :stripe
|
118
|
+
Helpers::STRIPE_SUBUNIT_OVERRIDE.fetch(@currency.iso_code, @currency.subunit_to_unit)
|
119
|
+
else
|
120
|
+
raise ArgumentError, "unknown format #{format}"
|
121
|
+
end
|
122
|
+
|
123
|
+
(@value * subunit_to_unit_value).to_i
|
107
124
|
end
|
108
125
|
|
109
126
|
def no_currency?
|
data/lib/money/version.rb
CHANGED
data/spec/money_spec.rb
CHANGED
@@ -342,12 +342,28 @@ RSpec.describe "Money" do
|
|
342
342
|
expect(Money.from_cents(1950.5)).to eq(Money.new(19.51))
|
343
343
|
end
|
344
344
|
|
345
|
-
|
346
|
-
|
347
|
-
|
345
|
+
describe '#from_subunits' do
|
346
|
+
it "creates Money object from an integer value in cents and currency" do
|
347
|
+
expect(Money.from_subunits(1950, 'CAD')).to eq(Money.new(19.50))
|
348
|
+
end
|
348
349
|
|
349
|
-
|
350
|
-
|
350
|
+
it "creates Money object from an integer value in dollars and currency with no cents" do
|
351
|
+
expect(Money.from_subunits(1950, 'JPY')).to eq(Money.new(1950, 'JPY'))
|
352
|
+
end
|
353
|
+
|
354
|
+
describe 'with format specified' do
|
355
|
+
it 'overrides the subunit_to_unit amount' do
|
356
|
+
expect(Money.from_subunits(100, 'ISK', format: :stripe)).to eq(Money.new(1, 'ISK'))
|
357
|
+
end
|
358
|
+
|
359
|
+
it 'fallbacks to the default subunit_to_unit amount if no override is specified' do
|
360
|
+
expect(Money.from_subunits(100, 'USD', format: :stripe)).to eq(Money.new(1, 'USD'))
|
361
|
+
end
|
362
|
+
|
363
|
+
it 'raises if the format is not found' do
|
364
|
+
expect { Money.from_subunits(100, 'ISK', format: :unknown) }.to(raise_error(ArgumentError))
|
365
|
+
end
|
366
|
+
end
|
351
367
|
end
|
352
368
|
|
353
369
|
it "raises when constructed with a NaN value" do
|
@@ -544,6 +560,20 @@ RSpec.describe "Money" do
|
|
544
560
|
expect(Money.new(1, 'IQD').subunits).to eq(1000)
|
545
561
|
expect(Money.new(1).subunits).to eq(100)
|
546
562
|
end
|
563
|
+
|
564
|
+
describe 'with format specified' do
|
565
|
+
it 'overrides the subunit_to_unit amount' do
|
566
|
+
expect(Money.new(1, 'ISK').subunits(format: :stripe)).to eq(100)
|
567
|
+
end
|
568
|
+
|
569
|
+
it 'fallbacks to the default subunit_to_unit amount if no override is specified' do
|
570
|
+
expect(Money.new(1, 'USD').subunits(format: :stripe)).to eq(100)
|
571
|
+
end
|
572
|
+
|
573
|
+
it 'raises if the format is not found' do
|
574
|
+
expect { Money.new(1, 'ISK').subunits(format: :unknown) }.to(raise_error(ArgumentError))
|
575
|
+
end
|
576
|
+
end
|
547
577
|
end
|
548
578
|
|
549
579
|
describe "value" 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.14.
|
4
|
+
version: 0.14.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: 2020-
|
11
|
+
date: 2020-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|