shopify-money 0.14.6 → 1.0.0.pre
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 +34 -0
- data/README.md +8 -2
- data/UPGRADING.md +57 -0
- data/config/currency_historic.yml +1 -1
- data/lib/money.rb +1 -1
- data/lib/money/allocator.rb +3 -1
- data/lib/money/config.rb +26 -0
- data/lib/money/currency.rb +4 -0
- data/lib/money/helpers.rb +12 -4
- data/lib/money/money.rb +52 -35
- data/lib/money/money_parser.rb +11 -5
- data/lib/money/null_currency.rb +32 -1
- data/lib/money/version.rb +1 -1
- data/lib/money_column/active_record_hooks.rb +12 -7
- data/lib/rubocop/cop/money.rb +1 -0
- data/lib/rubocop/cop/money/missing_currency.rb +16 -8
- data/lib/rubocop/cop/money/zero_money.rb +65 -0
- data/money.gemspec +1 -1
- data/spec/accounting_money_parser_spec.rb +4 -2
- data/spec/allocator_spec.rb +3 -3
- data/spec/config_spec.rb +60 -0
- data/spec/core_extensions_spec.rb +5 -5
- data/spec/currency_spec.rb +10 -0
- data/spec/helpers_spec.rb +11 -5
- data/spec/money_column_spec.rb +55 -34
- data/spec/money_parser_spec.rb +20 -8
- data/spec/money_spec.rb +123 -90
- data/spec/rubocop/cop/money/missing_currency_spec.rb +43 -8
- data/spec/rubocop/cop/money/zero_money_spec.rb +78 -0
- data/spec/spec_helper.rb +15 -0
- metadata +14 -10
- data/.travis.yml +0 -13
- data/lib/money_accessor.rb +0 -33
- data/spec/money_accessor_spec.rb +0 -87
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../../rubocop_helper'
|
4
|
+
require 'rubocop/cop/money/zero_money'
|
5
|
+
|
6
|
+
RSpec.describe RuboCop::Cop::Money::ZeroMoney do
|
7
|
+
subject(:cop) { described_class.new(config) }
|
8
|
+
|
9
|
+
let(:config) { RuboCop::Config.new }
|
10
|
+
|
11
|
+
context 'with default configuration' do
|
12
|
+
it 'registers an offense and corrects Money.zero without currency' do
|
13
|
+
expect_offense(<<~RUBY)
|
14
|
+
Money.zero
|
15
|
+
^^^^^^^^^^ Money.zero is removed, use `Money.new(0, Money::NULL_CURRENCY)`.
|
16
|
+
RUBY
|
17
|
+
|
18
|
+
expect_correction(<<~RUBY)
|
19
|
+
Money.new(0, Money::NULL_CURRENCY)
|
20
|
+
RUBY
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'registers an offense and corrects Money.zero with currency' do
|
24
|
+
expect_offense(<<~RUBY)
|
25
|
+
Money.zero('CAD')
|
26
|
+
^^^^^^^^^^^^^^^^^ Money.zero is removed, use `Money.new(0, 'CAD')`.
|
27
|
+
RUBY
|
28
|
+
|
29
|
+
expect_correction(<<~RUBY)
|
30
|
+
Money.new(0, 'CAD')
|
31
|
+
RUBY
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'does not register an offense when using Money.new with a currency' do
|
35
|
+
expect_no_offenses(<<~RUBY)
|
36
|
+
Money.new(0, 'CAD')
|
37
|
+
RUBY
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with ReplacementCurrency configuration' do
|
42
|
+
let(:config) do
|
43
|
+
RuboCop::Config.new(
|
44
|
+
'Money/ZeroMoney' => {
|
45
|
+
'ReplacementCurrency' => 'CAD'
|
46
|
+
}
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'registers an offense and corrects Money.zero without currency' do
|
51
|
+
expect_offense(<<~RUBY)
|
52
|
+
Money.zero
|
53
|
+
^^^^^^^^^^ Money.zero is removed, use `Money.new(0, 'CAD')`.
|
54
|
+
RUBY
|
55
|
+
|
56
|
+
expect_correction(<<~RUBY)
|
57
|
+
Money.new(0, 'CAD')
|
58
|
+
RUBY
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'registers an offense and corrects Money.zero with currency' do
|
62
|
+
expect_offense(<<~RUBY)
|
63
|
+
Money.zero('EUR')
|
64
|
+
^^^^^^^^^^^^^^^^^ Money.zero is removed, use `Money.new(0, 'EUR')`.
|
65
|
+
RUBY
|
66
|
+
|
67
|
+
expect_correction(<<~RUBY)
|
68
|
+
Money.new(0, 'EUR')
|
69
|
+
RUBY
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'does not register an offense when using Money.new with a currency' do
|
73
|
+
expect_no_offenses(<<~RUBY)
|
74
|
+
Money.new(0, 'EUR')
|
75
|
+
RUBY
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -68,3 +68,18 @@ RSpec::Matchers.define :quack_like do
|
|
68
68
|
expected.instance_methods - actual.instance_methods
|
69
69
|
end
|
70
70
|
end
|
71
|
+
|
72
|
+
|
73
|
+
def configure(default_currency: nil, legacy_json_format: nil, legacy_deprecations: nil, legacy_default_currency: nil, parser: nil)
|
74
|
+
old_config = Money.config
|
75
|
+
Money.config = Money::Config.new.tap do |config|
|
76
|
+
config.default_currency = default_currency if default_currency
|
77
|
+
config.parser = parser if parser
|
78
|
+
config.legacy_json_format! if legacy_json_format
|
79
|
+
config.legacy_deprecations! if legacy_deprecations
|
80
|
+
config.legacy_default_currency! if legacy_default_currency
|
81
|
+
end
|
82
|
+
yield
|
83
|
+
ensure
|
84
|
+
Money.config = old_config
|
85
|
+
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: 0.
|
4
|
+
version: 1.0.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shopify Inc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: simplecov
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -103,13 +103,14 @@ extra_rdoc_files: []
|
|
103
103
|
files:
|
104
104
|
- ".document"
|
105
105
|
- ".github/probots.yml"
|
106
|
+
- ".github/workflows/tests.yml"
|
106
107
|
- ".gitignore"
|
107
108
|
- ".rspec"
|
108
|
-
- ".travis.yml"
|
109
109
|
- Gemfile
|
110
110
|
- LICENSE.txt
|
111
111
|
- README.md
|
112
112
|
- Rakefile
|
113
|
+
- UPGRADING.md
|
113
114
|
- bin/console
|
114
115
|
- config/currency_historic.yml
|
115
116
|
- config/currency_iso.yml
|
@@ -118,6 +119,7 @@ files:
|
|
118
119
|
- lib/money.rb
|
119
120
|
- lib/money/accounting_money_parser.rb
|
120
121
|
- lib/money/allocator.rb
|
122
|
+
- lib/money/config.rb
|
121
123
|
- lib/money/core_extensions.rb
|
122
124
|
- lib/money/currency.rb
|
123
125
|
- lib/money/currency/loader.rb
|
@@ -128,27 +130,28 @@ files:
|
|
128
130
|
- lib/money/money_parser.rb
|
129
131
|
- lib/money/null_currency.rb
|
130
132
|
- lib/money/version.rb
|
131
|
-
- lib/money_accessor.rb
|
132
133
|
- lib/money_column.rb
|
133
134
|
- lib/money_column/active_record_hooks.rb
|
134
135
|
- lib/money_column/active_record_type.rb
|
135
136
|
- lib/money_column/railtie.rb
|
136
137
|
- lib/rubocop/cop/money.rb
|
137
138
|
- lib/rubocop/cop/money/missing_currency.rb
|
139
|
+
- lib/rubocop/cop/money/zero_money.rb
|
138
140
|
- lib/shopify-money.rb
|
139
141
|
- money.gemspec
|
140
142
|
- spec/accounting_money_parser_spec.rb
|
141
143
|
- spec/allocator_spec.rb
|
144
|
+
- spec/config_spec.rb
|
142
145
|
- spec/core_extensions_spec.rb
|
143
146
|
- spec/currency/loader_spec.rb
|
144
147
|
- spec/currency_spec.rb
|
145
148
|
- spec/helpers_spec.rb
|
146
|
-
- spec/money_accessor_spec.rb
|
147
149
|
- spec/money_column_spec.rb
|
148
150
|
- spec/money_parser_spec.rb
|
149
151
|
- spec/money_spec.rb
|
150
152
|
- spec/null_currency_spec.rb
|
151
153
|
- spec/rubocop/cop/money/missing_currency_spec.rb
|
154
|
+
- spec/rubocop/cop/money/zero_money_spec.rb
|
152
155
|
- spec/rubocop_helper.rb
|
153
156
|
- spec/schema.rb
|
154
157
|
- spec/spec_helper.rb
|
@@ -168,9 +171,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
168
171
|
version: '2.6'
|
169
172
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
173
|
requirements:
|
171
|
-
- - "
|
174
|
+
- - ">"
|
172
175
|
- !ruby/object:Gem::Version
|
173
|
-
version:
|
176
|
+
version: 1.3.1
|
174
177
|
requirements: []
|
175
178
|
rubygems_version: 3.0.3
|
176
179
|
signing_key:
|
@@ -179,16 +182,17 @@ summary: Shopify's money gem
|
|
179
182
|
test_files:
|
180
183
|
- spec/accounting_money_parser_spec.rb
|
181
184
|
- spec/allocator_spec.rb
|
185
|
+
- spec/config_spec.rb
|
182
186
|
- spec/core_extensions_spec.rb
|
183
187
|
- spec/currency/loader_spec.rb
|
184
188
|
- spec/currency_spec.rb
|
185
189
|
- spec/helpers_spec.rb
|
186
|
-
- spec/money_accessor_spec.rb
|
187
190
|
- spec/money_column_spec.rb
|
188
191
|
- spec/money_parser_spec.rb
|
189
192
|
- spec/money_spec.rb
|
190
193
|
- spec/null_currency_spec.rb
|
191
194
|
- spec/rubocop/cop/money/missing_currency_spec.rb
|
195
|
+
- spec/rubocop/cop/money/zero_money_spec.rb
|
192
196
|
- spec/rubocop_helper.rb
|
193
197
|
- spec/schema.rb
|
194
198
|
- spec/spec_helper.rb
|
data/.travis.yml
DELETED
data/lib/money_accessor.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module MoneyAccessor
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def money_accessor(*columns)
|
9
|
-
variable_get = self <= Struct ? :[] : :instance_variable_get
|
10
|
-
variable_set = self <= Struct ? :[]= : :instance_variable_set
|
11
|
-
|
12
|
-
Array(columns).flatten.each do |name|
|
13
|
-
variable_name = self <= Struct ? name : "@#{name}"
|
14
|
-
|
15
|
-
define_method(name) do
|
16
|
-
value = public_send(variable_get, variable_name)
|
17
|
-
value.blank? ? nil : Money.new(value)
|
18
|
-
end
|
19
|
-
|
20
|
-
define_method("#{name}=") do |value|
|
21
|
-
if value.blank? || !value.respond_to?(:to_money)
|
22
|
-
public_send(variable_set, variable_name, nil)
|
23
|
-
nil
|
24
|
-
else
|
25
|
-
money = value.to_money
|
26
|
-
public_send(variable_set, variable_name, money.value)
|
27
|
-
money
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
data/spec/money_accessor_spec.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
class NormalObject
|
5
|
-
include MoneyAccessor
|
6
|
-
|
7
|
-
money_accessor :price
|
8
|
-
|
9
|
-
def initialize(price)
|
10
|
-
@price = price
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class StructObject < Struct.new(:price)
|
15
|
-
include MoneyAccessor
|
16
|
-
|
17
|
-
money_accessor :price
|
18
|
-
end
|
19
|
-
|
20
|
-
RSpec.shared_examples_for "an object with a money accessor" do
|
21
|
-
it "generates an attribute reader that returns a money object" do
|
22
|
-
object = described_class.new(100)
|
23
|
-
|
24
|
-
expect(object.price).to eq(Money.new(100))
|
25
|
-
end
|
26
|
-
|
27
|
-
it "generates an attribute reader that returns a nil object if the value was nil" do
|
28
|
-
object = described_class.new(nil)
|
29
|
-
|
30
|
-
expect(object.price).to eq(nil)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "generates an attribute reader that returns a nil object if the value was blank" do
|
34
|
-
object = described_class.new('')
|
35
|
-
|
36
|
-
expect(object.price).to eq(nil)
|
37
|
-
end
|
38
|
-
|
39
|
-
it "generates an attribute writer that allow setting a money object" do
|
40
|
-
object = described_class.new(0)
|
41
|
-
object.price = Money.new(10)
|
42
|
-
|
43
|
-
expect(object.price).to eq(Money.new(10))
|
44
|
-
end
|
45
|
-
|
46
|
-
it "generates an attribute writer that allow setting a integer value" do
|
47
|
-
object = described_class.new(0)
|
48
|
-
object.price = 10
|
49
|
-
|
50
|
-
expect(object.price).to eq(Money.new(10))
|
51
|
-
end
|
52
|
-
|
53
|
-
it "generates an attribute writer that allow setting a float value" do
|
54
|
-
object = described_class.new(0)
|
55
|
-
object.price = 10.12
|
56
|
-
|
57
|
-
expect(object.price).to eq(Money.new(10.12))
|
58
|
-
end
|
59
|
-
|
60
|
-
it "generates an attribute writer that allow setting a nil value" do
|
61
|
-
object = described_class.new(0)
|
62
|
-
object.price = nil
|
63
|
-
|
64
|
-
expect(object.price).to eq(nil)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "generates an attribute writer that allow setting a blank value" do
|
68
|
-
object = described_class.new(0)
|
69
|
-
object.price = ''
|
70
|
-
|
71
|
-
expect(object.price).to eq(nil)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
RSpec.describe NormalObject do
|
76
|
-
it_behaves_like "an object with a money accessor"
|
77
|
-
end
|
78
|
-
|
79
|
-
RSpec.describe StructObject do
|
80
|
-
it_behaves_like "an object with a money accessor"
|
81
|
-
|
82
|
-
it 'does not generate an ivar to store the price value' do
|
83
|
-
object = described_class.new(10.00)
|
84
|
-
|
85
|
-
expect(object.instance_variable_get(:@price)).to eq(nil)
|
86
|
-
end
|
87
|
-
end
|