shopify-money 1.1.1 → 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: ae2953d70e905d5f2425b6c334f5e8a920ec749a0e82341739b90dee7c446254
4
- data.tar.gz: 42fd32e212f52fb97c366cc39fe8bf462fff7be09ebb54690a6c7a3d80e5c08d
3
+ metadata.gz: c67c2dd8b088c4dd304e61c162f100be69c492de2a3a45f8954bca27a5a524dc
4
+ data.tar.gz: d162ab0971609899ac9b152e62eb96bce8c54908761310e99a361280b20fd005
5
5
  SHA512:
6
- metadata.gz: 538545671ca1a88fd930f5148b0477a2558a6166764439a2a30ff3a7b34f7057fbf510c11f238d44cae13296c2eb81cfd07ffa510bfacae2b8780c49920404ed
7
- data.tar.gz: 67944f9355d6b02e61c0e90f9e34cfd3ec7d3adea2f6dfbb45fc88241e38af516ad89726bd13b2e4a211e4adb26741f842904f92095e678808e1e25b871abfde
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/helpers.rb CHANGED
@@ -10,6 +10,7 @@ class Money
10
10
 
11
11
  STRIPE_SUBUNIT_OVERRIDE = {
12
12
  'ISK' => 100,
13
+ 'UGX' => 100,
13
14
  }.freeze
14
15
 
15
16
  def value_to_decimal(num)
data/lib/money/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class Money
3
- VERSION = "1.1.1"
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
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.1.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-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