shopify-money 1.1.1 → 1.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/.github/workflows/tests.yml +1 -1
- data/README.md +9 -4
- data/dev.yml +1 -1
- data/lib/money/helpers.rb +1 -0
- data/lib/money/version.rb +1 -1
- data/lib/rubocop/cop/money/unsafe_to_money.rb +35 -0
- data/spec/money_spec.rb +8 -0
- data/spec/rubocop/cop/money/unsafe_to_money_spec.rb +57 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c67c2dd8b088c4dd304e61c162f100be69c492de2a3a45f8954bca27a5a524dc
|
4
|
+
data.tar.gz: d162ab0971609899ac9b152e62eb96bce8c54908761310e99a361280b20fd005
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b272a70b2e03787371782b781e62b291d292c5486070899253ba286d8a91461581fda8904e1f38025dceabda6d3690373b72333f8d103d8b6cd698bf1213787c
|
7
|
+
data.tar.gz: b3f3165474b9d0fc614dca9c3f1a771de70aa8e02423e68deb3ecc2c90201454d52dac62a813ab132605526f122e43822abfd3049d55ced561b0eecb2532fc9a
|
data/.github/workflows/tests.yml
CHANGED
data/README.md
CHANGED
@@ -156,12 +156,17 @@ require:
|
|
156
156
|
|
157
157
|
Money/MissingCurrency:
|
158
158
|
Enabled: true
|
159
|
-
|
159
|
+
# If your application is currently handling only one currency,
|
160
|
+
# it can autocorrect this by specifying a default currency.
|
161
|
+
ReplacementCurrency: CAD
|
160
162
|
|
161
|
-
|
163
|
+
Money/ZeroMoney:
|
164
|
+
Enabled: true
|
165
|
+
# Same here:
|
166
|
+
# ReplacementCurrency: CAD
|
162
167
|
|
163
|
-
|
164
|
-
|
168
|
+
Money/UnsafeToMoney:
|
169
|
+
Enabled: true
|
165
170
|
```
|
166
171
|
|
167
172
|
## Contributing to money
|
data/dev.yml
CHANGED
data/lib/money/helpers.rb
CHANGED
data/lib/money/version.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
module RuboCop
|
3
|
+
module Cop
|
4
|
+
module Money
|
5
|
+
# Prevents the use of `to_money` because it has inconsistent behaviour.
|
6
|
+
# Use `Money.new` instead.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# "2.000".to_money("USD") #<Money value:2000.00 currency:USD>
|
11
|
+
#
|
12
|
+
# # good
|
13
|
+
# Money.new("2.000", "USD") #<Money value:2.00 currency:USD>
|
14
|
+
class UnsafeToMoney < Cop
|
15
|
+
MSG = '`to_money` has inconsistent behaviour. Use `Money.new` instead.'.freeze
|
16
|
+
|
17
|
+
def on_send(node)
|
18
|
+
return unless node.method?(:to_money)
|
19
|
+
return if node.receiver.is_a?(AST::NumericNode)
|
20
|
+
|
21
|
+
add_offense(node, location: :selector)
|
22
|
+
end
|
23
|
+
|
24
|
+
def autocorrect(node)
|
25
|
+
lambda do |corrector|
|
26
|
+
receiver = node.receiver.source
|
27
|
+
args = node.arguments.map(&:source)
|
28
|
+
args.prepend(receiver)
|
29
|
+
corrector.replace(node.loc.expression, "Money.new(#{args.join(', ')})")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/money_spec.rb
CHANGED
@@ -376,6 +376,10 @@ RSpec.describe "Money" do
|
|
376
376
|
expect(Money.from_subunits(100, 'ISK', format: :stripe)).to eq(Money.new(1, 'ISK'))
|
377
377
|
end
|
378
378
|
|
379
|
+
it 'overrides the subunit_to_unit amount for UGX' do
|
380
|
+
expect(Money.from_subunits(100, 'UGX', format: :stripe)).to eq(Money.new(1, 'UGX'))
|
381
|
+
end
|
382
|
+
|
379
383
|
it 'fallbacks to the default subunit_to_unit amount if no override is specified' do
|
380
384
|
expect(Money.from_subunits(100, 'USD', format: :stripe)).to eq(Money.new(1, 'USD'))
|
381
385
|
end
|
@@ -622,6 +626,10 @@ RSpec.describe "Money" do
|
|
622
626
|
expect(Money.new(1, 'ISK').subunits(format: :stripe)).to eq(100)
|
623
627
|
end
|
624
628
|
|
629
|
+
it 'overrides the subunit_to_unit amount for UGX' do
|
630
|
+
expect(Money.new(1, 'UGX').subunits(format: :stripe)).to eq(100)
|
631
|
+
end
|
632
|
+
|
625
633
|
it 'fallbacks to the default subunit_to_unit amount if no override is specified' do
|
626
634
|
expect(Money.new(1, 'USD').subunits(format: :stripe)).to eq(100)
|
627
635
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../../rubocop_helper'
|
4
|
+
require 'rubocop/cop/money/unsafe_to_money'
|
5
|
+
|
6
|
+
RSpec.describe RuboCop::Cop::Money::UnsafeToMoney do
|
7
|
+
subject(:cop) { described_class.new(config) }
|
8
|
+
|
9
|
+
let(:config) { RuboCop::Config.new }
|
10
|
+
|
11
|
+
context 'with default configuration' do
|
12
|
+
it 'does not register an offense for literal integer' do
|
13
|
+
expect_no_offenses(<<~RUBY)
|
14
|
+
1.to_money
|
15
|
+
RUBY
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'does not register an offense for literal float' do
|
19
|
+
expect_no_offenses(<<~RUBY)
|
20
|
+
1.000.to_money
|
21
|
+
RUBY
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'registers an offense and corrects for Money.new without a currency argument' do
|
25
|
+
expect_offense(<<~RUBY)
|
26
|
+
'2.000'.to_money
|
27
|
+
^^^^^^^^ #{described_class::MSG}
|
28
|
+
RUBY
|
29
|
+
|
30
|
+
expect_correction(<<~RUBY)
|
31
|
+
Money.new('2.000')
|
32
|
+
RUBY
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'registers an offense and corrects for Money.new with a currency argument' do
|
36
|
+
expect_offense(<<~RUBY)
|
37
|
+
'2.000'.to_money('USD')
|
38
|
+
^^^^^^^^ #{described_class::MSG}
|
39
|
+
RUBY
|
40
|
+
|
41
|
+
expect_correction(<<~RUBY)
|
42
|
+
Money.new('2.000', 'USD')
|
43
|
+
RUBY
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'registers an offense and corrects for Money.new with a complex receiver' do
|
47
|
+
expect_offense(<<~RUBY)
|
48
|
+
obj.money.to_money('USD')
|
49
|
+
^^^^^^^^ #{described_class::MSG}
|
50
|
+
RUBY
|
51
|
+
|
52
|
+
expect_correction(<<~RUBY)
|
53
|
+
Money.new(obj.money, 'USD')
|
54
|
+
RUBY
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
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: 1.
|
4
|
+
version: 1.2.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: 2023-
|
11
|
+
date: 2023-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -140,6 +140,7 @@ files:
|
|
140
140
|
- lib/money_column/railtie.rb
|
141
141
|
- lib/rubocop/cop/money.rb
|
142
142
|
- lib/rubocop/cop/money/missing_currency.rb
|
143
|
+
- lib/rubocop/cop/money/unsafe_to_money.rb
|
143
144
|
- lib/rubocop/cop/money/zero_money.rb
|
144
145
|
- lib/shopify-money.rb
|
145
146
|
- money.gemspec
|
@@ -159,6 +160,7 @@ files:
|
|
159
160
|
- spec/rails/job_argument_serializer_spec.rb
|
160
161
|
- spec/rails_spec_helper.rb
|
161
162
|
- spec/rubocop/cop/money/missing_currency_spec.rb
|
163
|
+
- spec/rubocop/cop/money/unsafe_to_money_spec.rb
|
162
164
|
- spec/rubocop/cop/money/zero_money_spec.rb
|
163
165
|
- spec/rubocop_helper.rb
|
164
166
|
- spec/schema.rb
|
@@ -183,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
183
185
|
- !ruby/object:Gem::Version
|
184
186
|
version: '0'
|
185
187
|
requirements: []
|
186
|
-
rubygems_version: 3.
|
188
|
+
rubygems_version: 3.4.14
|
187
189
|
signing_key:
|
188
190
|
specification_version: 4
|
189
191
|
summary: Shopify's money gem
|
@@ -204,6 +206,7 @@ test_files:
|
|
204
206
|
- spec/rails/job_argument_serializer_spec.rb
|
205
207
|
- spec/rails_spec_helper.rb
|
206
208
|
- spec/rubocop/cop/money/missing_currency_spec.rb
|
209
|
+
- spec/rubocop/cop/money/unsafe_to_money_spec.rb
|
207
210
|
- spec/rubocop/cop/money/zero_money_spec.rb
|
208
211
|
- spec/rubocop_helper.rb
|
209
212
|
- spec/schema.rb
|