shopify-money 1.1.2 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +1 -1
- data/README.md +9 -4
- data/dev.yml +1 -1
- data/lib/money/version.rb +1 -1
- data/lib/rubocop/cop/money/unsafe_to_money.rb +35 -0
- data/lib/rubocop/cop/money.rb +1 -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: de174294a88067d61cfcf9321bc62fc3de2b4c265e089d07ff8bedca30ce782a
|
4
|
+
data.tar.gz: 9cb65f619cf3ceb3054437c3ede664d9fa949859af7858e1f280d82f983cdace
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfd7348a2506689d46b70eebb613c4ebdb3ef33ad31ae361d130c4348691f90049a66a50920f96c9f39f532bfe3fc0901c939a6d2e0be6d90d6d045165209f37
|
7
|
+
data.tar.gz: 729391b98fbbfc671726cedd35d6ee13afed28f6b88b98d825eb6cae23cc968de72437fdacf51bf5f68dbaf3aabf4f84d296709764031ce83128a4010012a1b7
|
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/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/lib/rubocop/cop/money.rb
CHANGED
@@ -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.1
|
4
|
+
version: 1.2.1
|
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
|