shopify-money 1.1.2 → 1.2.0

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: 68cd70a11908e256c3743d9cd5c66ad2d3fb93eac02fd7973a5e8c20c6ca3e6b
4
- data.tar.gz: c5619ce2dc46b6271ae96c326c57b8d4fc7dfef9f3696db1f140c83b5df0f5bf
3
+ metadata.gz: c67c2dd8b088c4dd304e61c162f100be69c492de2a3a45f8954bca27a5a524dc
4
+ data.tar.gz: d162ab0971609899ac9b152e62eb96bce8c54908761310e99a361280b20fd005
5
5
  SHA512:
6
- metadata.gz: 116fc4ca873f7373653509bc104d8343160877c3085ac2446dacbb918ecf2e823a915b9f2bb97ab48d7a39c4f2515518fa64ffece7a2c501b4a3958ea5455457
7
- data.tar.gz: 3b702aa16acd05f2100589ac319931c268999e616ce28e741c921539000c39b48521807c6a81d1a0aaaf70c2f436a02a4f236109104db00bd4cdb5b9c8e9c01e
6
+ metadata.gz: b272a70b2e03787371782b781e62b291d292c5486070899253ba286d8a91461581fda8904e1f38025dceabda6d3690373b72333f8d103d8b6cd698bf1213787c
7
+ data.tar.gz: b3f3165474b9d0fc614dca9c3f1a771de70aa8e02423e68deb3ecc2c90201454d52dac62a813ab132605526f122e43822abfd3049d55ced561b0eecb2532fc9a
@@ -9,7 +9,7 @@ jobs:
9
9
 
10
10
  strategy:
11
11
  matrix:
12
- ruby: ['2.6', '2.7', '3.0', '3.1']
12
+ ruby: ['2.6', '2.7', '3.0', '3.1', '3.2']
13
13
 
14
14
  name: Ruby ${{ matrix.ruby }}
15
15
  steps:
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
- If your application is currently handling only one currency, it can autocorrect this by specifying a currency under the `Enabled` line:
163
+ Money/ZeroMoney:
164
+ Enabled: true
165
+ # Same here:
166
+ # ReplacementCurrency: CAD
162
167
 
163
- ```yaml
164
- ReplacementCurrency: 'CAD'
168
+ Money/UnsafeToMoney:
169
+ Enabled: true
165
170
  ```
166
171
 
167
172
  ## Contributing to money
data/dev.yml CHANGED
@@ -3,7 +3,7 @@
3
3
  ---
4
4
  name: money
5
5
  up:
6
- - ruby: 2.6.6
6
+ - ruby: 3.2.0
7
7
  - bundler
8
8
  commands:
9
9
  test: bundle exec rspec
data/lib/money/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class Money
3
- VERSION = "1.1.2"
3
+ VERSION = "1.2.0"
4
4
  end
@@ -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
@@ -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.2
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-01-24 00:00:00.000000000 Z
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.3.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