shopify-money 0.11.9 → 0.12.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/README.md +2 -2
- data/lib/money/money.rb +2 -0
- data/lib/money/version.rb +1 -1
- data/spec/money_spec.rb +117 -6
- 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: 5e95441c6aa9e01d7ac17c8e76f8008cf94dba8d4af2bd75f101d743a333311e
|
4
|
+
data.tar.gz: 07d99a0656016292183ef0a782879f018d4a8fdb814ab6e97470675003280805
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e18f193d30427adb99f4ebb28e1125a5819ec489801c25698069f1c48448edb492ea3effbd9117bac45d77a613f59688a5ff5ed3327d4b20d03b6dd0a51231d7
|
7
|
+
data.tar.gz: b5cc2766b02db02ebf0df626cc531d0996444f78e7939c7fee7e3caa050cb2fb06c767e88e7256ad12131f225436c68b7ebf776c6e8ea1995e18525004f34896
|
data/README.md
CHANGED
@@ -114,8 +114,8 @@ generate methods for use with ActiveRecord:
|
|
114
114
|
|
115
115
|
```ruby
|
116
116
|
create_table :orders do |t|
|
117
|
-
t.decimal :sub_total, precision:
|
118
|
-
t.decimal :tax, precision:
|
117
|
+
t.decimal :sub_total, precision: 21, scale: 3
|
118
|
+
t.decimal :tax, precision: 21, scale: 3
|
119
119
|
t.string :currency, limit: 3
|
120
120
|
end
|
121
121
|
|
data/lib/money/money.rb
CHANGED
@@ -114,6 +114,7 @@ class Money
|
|
114
114
|
end
|
115
115
|
|
116
116
|
def <=>(other)
|
117
|
+
return unless other.respond_to?(:to_money)
|
117
118
|
arithmetic(other) do |money|
|
118
119
|
value <=> money.value
|
119
120
|
end
|
@@ -150,6 +151,7 @@ class Money
|
|
150
151
|
eql?(other)
|
151
152
|
end
|
152
153
|
|
154
|
+
# TODO: Remove once cross-currency mathematical operations are no longer allowed
|
153
155
|
def eql?(other)
|
154
156
|
return false unless other.is_a?(Money)
|
155
157
|
return false unless currency.compatible?(other.currency)
|
data/lib/money/version.rb
CHANGED
data/spec/money_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
RSpec.describe "Money" do
|
5
5
|
|
6
|
-
let (:money) {Money.new(1)}
|
6
|
+
let (:money) { Money.new(1) }
|
7
7
|
let (:amount_money) { Money.new(1.23, 'USD') }
|
8
8
|
let (:non_fractional_money) { Money.new(1, 'JPY') }
|
9
9
|
let (:zero_money) { Money.new(0) }
|
@@ -374,17 +374,23 @@ RSpec.describe "Money" do
|
|
374
374
|
expect((0.5 <=> money)).to eq(-1)
|
375
375
|
end
|
376
376
|
|
377
|
-
it "raises error if compared other is not compatible" do
|
378
|
-
expect{ Money.new(5.00) <=> nil }.to raise_error(TypeError)
|
379
|
-
end
|
380
|
-
|
381
377
|
it "have the same hash value as $1" do
|
382
378
|
expect(money.hash).to eq(Money.new(1.00).hash)
|
383
379
|
end
|
384
380
|
|
385
381
|
it "does not have the same hash value as $2" do
|
386
|
-
expect(money.hash).
|
382
|
+
expect(money.hash).to_not eq(Money.new(2.00).hash)
|
387
383
|
end
|
384
|
+
end
|
385
|
+
|
386
|
+
describe('Comparable') do
|
387
|
+
let (:cad_05) { Money.new(5.00, 'CAD') }
|
388
|
+
let (:cad_10) { Money.new(10.00, 'CAD') }
|
389
|
+
let (:cad_20) { Money.new(20.00, 'CAD') }
|
390
|
+
let (:nil_05) { Money.new(5.00, Money::NULL_CURRENCY) }
|
391
|
+
let (:nil_10) { Money.new(10.00, Money::NULL_CURRENCY) }
|
392
|
+
let (:nil_20) { Money.new(20.00, Money::NULL_CURRENCY) }
|
393
|
+
let (:usd_10) { Money.new(10.00, 'USD') }
|
388
394
|
|
389
395
|
it "<=> can compare with and without currency" do
|
390
396
|
expect(Money.new(1000, Money::NULL_CURRENCY) <=> Money.new(2000, 'JPY')).to eq(-1)
|
@@ -396,6 +402,111 @@ RSpec.describe "Money" do
|
|
396
402
|
expect(Money.new(1000, 'USD') <=> Money.new(2000, 'JPY')).to eq(-1)
|
397
403
|
expect(Money.new(2000, 'JPY') <=> Money.new(1000, 'USD')).to eq(1)
|
398
404
|
end
|
405
|
+
|
406
|
+
describe('same values') do
|
407
|
+
describe('same currencies') do
|
408
|
+
it { expect(cad_10 <=> cad_10).to(eq(0)) }
|
409
|
+
it { expect(cad_10 > cad_10).to(eq(false)) }
|
410
|
+
it { expect(cad_10 >= cad_10).to(eq(true)) }
|
411
|
+
it { expect(cad_10 == cad_10).to(eq(true)) }
|
412
|
+
it { expect(cad_10 <= cad_10).to(eq(true)) }
|
413
|
+
it { expect(cad_10 < cad_10).to(eq(false)) }
|
414
|
+
end
|
415
|
+
|
416
|
+
describe('null currency') do
|
417
|
+
it { expect(cad_10 <=> nil_10).to(eq(0)) }
|
418
|
+
it { expect(cad_10 > nil_10).to(eq(false)) }
|
419
|
+
it { expect(cad_10 >= nil_10).to(eq(true)) }
|
420
|
+
it { expect(cad_10 == nil_10).to(eq(true)) }
|
421
|
+
it { expect(cad_10 <= nil_10).to(eq(true)) }
|
422
|
+
it { expect(cad_10 < nil_10).to(eq(false)) }
|
423
|
+
end
|
424
|
+
|
425
|
+
describe('different currencies') do
|
426
|
+
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <=> usd_10).to(eq(0)) }
|
427
|
+
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 > usd_10).to(eq(false)) }
|
428
|
+
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 >= usd_10).to(eq(true)) }
|
429
|
+
it { expect(cad_10 == usd_10).to(eq(false)) }
|
430
|
+
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <= usd_10).to(eq(true)) }
|
431
|
+
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 < usd_10).to(eq(false)) }
|
432
|
+
end
|
433
|
+
|
434
|
+
describe('to_money types') do
|
435
|
+
it { expect(cad_10 <=> 10.00).to(eq(0)) }
|
436
|
+
it { expect(cad_10 > 10.00).to(eq(false)) }
|
437
|
+
it { expect(cad_10 >= 10.00).to(eq(true)) }
|
438
|
+
it { expect(cad_10 == 10.00).to(eq(false)) }
|
439
|
+
it { expect(cad_10 <= 10.00).to(eq(true)) }
|
440
|
+
it { expect(cad_10 < 10.00).to(eq(false)) }
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
describe('left lower than right') do
|
445
|
+
describe('same currencies') do
|
446
|
+
it { expect(cad_10 <=> cad_20).to(eq(-1)) }
|
447
|
+
it { expect(cad_10 > cad_20).to(eq(false)) }
|
448
|
+
it { expect(cad_10 >= cad_20).to(eq(false)) }
|
449
|
+
it { expect(cad_10 == cad_20).to(eq(false)) }
|
450
|
+
it { expect(cad_10 <= cad_20).to(eq(true)) }
|
451
|
+
it { expect(cad_10 < cad_20).to(eq(true)) }
|
452
|
+
end
|
453
|
+
|
454
|
+
describe('null currency') do
|
455
|
+
it { expect(cad_10 <=> nil_20).to(eq(-1)) }
|
456
|
+
it { expect(cad_10 > nil_20).to(eq(false)) }
|
457
|
+
it { expect(cad_10 >= nil_20).to(eq(false)) }
|
458
|
+
it { expect(cad_10 == nil_20).to(eq(false)) }
|
459
|
+
it { expect(cad_10 <= nil_20).to(eq(true)) }
|
460
|
+
it { expect(cad_10 < nil_20).to(eq(true)) }
|
461
|
+
end
|
462
|
+
|
463
|
+
describe('to_money types') do
|
464
|
+
it { expect(cad_10 <=> 20.00).to(eq(-1)) }
|
465
|
+
it { expect(cad_10 > 20.00).to(eq(false)) }
|
466
|
+
it { expect(cad_10 >= 20.00).to(eq(false)) }
|
467
|
+
it { expect(cad_10 == 20.00).to(eq(false)) }
|
468
|
+
it { expect(cad_10 <= 20.00).to(eq(true)) }
|
469
|
+
it { expect(cad_10 < 20.00).to(eq(true)) }
|
470
|
+
end
|
471
|
+
end
|
472
|
+
|
473
|
+
describe('left greater than right') do
|
474
|
+
describe('same currencies') do
|
475
|
+
it { expect(cad_10 <=> cad_05).to(eq(1)) }
|
476
|
+
it { expect(cad_10 > cad_05).to(eq(true)) }
|
477
|
+
it { expect(cad_10 >= cad_05).to(eq(true)) }
|
478
|
+
it { expect(cad_10 == cad_05).to(eq(false)) }
|
479
|
+
it { expect(cad_10 <= cad_05).to(eq(false)) }
|
480
|
+
it { expect(cad_10 < cad_05).to(eq(false)) }
|
481
|
+
end
|
482
|
+
|
483
|
+
describe('null currency') do
|
484
|
+
it { expect(cad_10 <=> nil_05).to(eq(1)) }
|
485
|
+
it { expect(cad_10 > nil_05).to(eq(true)) }
|
486
|
+
it { expect(cad_10 >= nil_05).to(eq(true)) }
|
487
|
+
it { expect(cad_10 == nil_05).to(eq(false)) }
|
488
|
+
it { expect(cad_10 <= nil_05).to(eq(false)) }
|
489
|
+
it { expect(cad_10 < nil_05).to(eq(false)) }
|
490
|
+
end
|
491
|
+
|
492
|
+
describe('to_money types') do
|
493
|
+
it { expect(cad_10 <=> 5.00).to(eq(1)) }
|
494
|
+
it { expect(cad_10 > 5.00).to(eq(true)) }
|
495
|
+
it { expect(cad_10 >= 5.00).to(eq(true)) }
|
496
|
+
it { expect(cad_10 == 5.00).to(eq(false)) }
|
497
|
+
it { expect(cad_10 <= 5.00).to(eq(false)) }
|
498
|
+
it { expect(cad_10 < 5.00).to(eq(false)) }
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
describe('any values, non-to_money types') do
|
503
|
+
it { expect(cad_10 <=> nil).to(eq(nil)) }
|
504
|
+
it { expect { cad_10 > nil }.to(raise_error(ArgumentError)) }
|
505
|
+
it { expect { cad_10 >= nil }.to(raise_error(ArgumentError)) }
|
506
|
+
it { expect(cad_10 == nil).to(eq(false)) }
|
507
|
+
it { expect { cad_10 <= nil }.to(raise_error(ArgumentError)) }
|
508
|
+
it { expect { cad_10 < nil }.to(raise_error(ArgumentError)) }
|
509
|
+
end
|
399
510
|
end
|
400
511
|
|
401
512
|
describe "#subunits" 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.12.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-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|