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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1059640a2227392fc00f7292905bd4d4de2a71d075e8ffc1fb4d02944c5c8c2
4
- data.tar.gz: 80db4d470ef80c27c09937b7fa146827d4183c519418a8382934e9a1875fd5f7
3
+ metadata.gz: e845ad45cb261ed350e3baff7d3eade222009665caaaa52b033cdf66da011477
4
+ data.tar.gz: 460242834342b799f8e6a1d4475bcd9e24a251240471bf8c47c9d899b346ab51
5
5
  SHA512:
6
- metadata.gz: 2f8cf9b8ac4fafc4a372176ebe0e46d3b29c8fc82caf020f16732eef39d80ee3f35005faea7c9a96253791c9ce2c836776ca91dc31d55713bc577af55407eed2
7
- data.tar.gz: d484b008600973688c52d232797e9ebb1efa6e782b33b8fa408853c08d35f96d8fde50c353ea53c9d9350378fd4f0390d579b619bef45b0f28d0fbe2d7e7c8b6
6
+ metadata.gz: 949a72b84927dfa363f433d62ebd54cdc898417cf222697eb04ab87e00d61a36321d777bb7ba01f7a500ae2f95f3b5480b4d0080245616e69424ebbe5a6700ed
7
+ data.tar.gz: f3ba37b79af054aa738c1ff562eea5d2a43c9e8b3856ee8be487320a59f704cf64f560bf1186e0ebfe2b7b3b4065d638c5d218f6a1ae4efdfbc7fa4521ff057c
@@ -8,6 +8,10 @@ class Money
8
8
  DECIMAL_ZERO = BigDecimal(0).freeze
9
9
  MAX_DECIMAL = 21
10
10
 
11
+ STRIPE_SUBUNIT_OVERRIDE = {
12
+ 'ISK' => 100,
13
+ }.freeze
14
+
11
15
  def value_to_decimal(num)
12
16
  value =
13
17
  case num
@@ -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
- value = Helpers.value_to_decimal(subunits) / currency.subunit_to_unit
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
- (@value * @currency.subunit_to_unit).to_i
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?
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class Money
3
- VERSION = "0.14.6"
3
+ VERSION = "0.14.7"
4
4
  end
@@ -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
- it "is creatable from an integer value in cents and currency" do
346
- expect(Money.from_subunits(1950, 'CAD')).to eq(Money.new(19.50))
347
- end
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
- it "is creatable from an integer value in dollars and currency with no cents" do
350
- expect(Money.from_subunits(1950, 'JPY')).to eq(Money.new(1950, 'JPY'))
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.6
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-10-19 00:00:00.000000000 Z
11
+ date: 2020-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler