shopify-money 0.13.1 → 0.14.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -3
  3. data/Gemfile +2 -1
  4. data/README.md +20 -1
  5. data/Rakefile +1 -0
  6. data/bin/console +1 -0
  7. data/config/currency_historic.yml +152 -0
  8. data/config/currency_iso.yml +2623 -0
  9. data/config/currency_non_iso.yml +77 -0
  10. data/dev.yml +1 -1
  11. data/lib/money.rb +3 -0
  12. data/lib/money/allocator.rb +10 -11
  13. data/lib/money/core_extensions.rb +1 -0
  14. data/lib/money/currency.rb +1 -0
  15. data/lib/money/currency/loader.rb +28 -15
  16. data/lib/money/deprecations.rb +1 -0
  17. data/lib/money/errors.rb +1 -0
  18. data/lib/money/helpers.rb +5 -15
  19. data/lib/money/money.rb +3 -2
  20. data/lib/money/null_currency.rb +1 -0
  21. data/lib/money/version.rb +2 -1
  22. data/lib/money_accessor.rb +1 -0
  23. data/lib/money_column.rb +1 -0
  24. data/lib/money_column/active_record_hooks.rb +2 -1
  25. data/lib/money_column/active_record_type.rb +1 -0
  26. data/lib/money_column/railtie.rb +1 -0
  27. data/lib/rubocop/cop/money.rb +3 -0
  28. data/lib/rubocop/cop/money/missing_currency.rb +75 -0
  29. data/lib/shopify-money.rb +2 -0
  30. data/money.gemspec +7 -3
  31. data/spec/accounting_money_parser_spec.rb +1 -0
  32. data/spec/allocator_spec.rb +13 -0
  33. data/spec/core_extensions_spec.rb +2 -1
  34. data/spec/currency/loader_spec.rb +1 -0
  35. data/spec/currency_spec.rb +1 -0
  36. data/spec/helpers_spec.rb +1 -6
  37. data/spec/money_accessor_spec.rb +1 -0
  38. data/spec/money_column_spec.rb +1 -0
  39. data/spec/money_parser_spec.rb +1 -0
  40. data/spec/money_spec.rb +3 -2
  41. data/spec/null_currency_spec.rb +1 -0
  42. data/spec/rubocop/cop/money/missing_currency_spec.rb +108 -0
  43. data/spec/rubocop_helper.rb +10 -0
  44. data/spec/schema.rb +1 -0
  45. data/spec/spec_helper.rb +1 -5
  46. metadata +19 -25
  47. data/config/currency_historic.json +0 -157
  48. data/config/currency_iso.json +0 -2642
  49. data/config/currency_non_iso.json +0 -82
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  RSpec.describe "Allocator" do
@@ -67,6 +68,12 @@ RSpec.describe "Allocator" do
67
68
  specify "#allocate raise ArgumentError when invalid strategy is provided" do
68
69
  expect { new_allocator(0.03).allocate([0.5, 0.5], :bad_strategy_name) }.to raise_error(ArgumentError, "Invalid strategy. Valid options: :roundrobin, :roundrobin_reverse")
69
70
  end
71
+
72
+ specify "#allocate does not raise ArgumentError when invalid splits types are provided" do
73
+ moneys = new_allocator(0.03).allocate([0.5, 0.5], :roundrobin)
74
+ expect(moneys[0]).to eq(Money.new(0.02))
75
+ expect(moneys[1]).to eq(Money.new(0.01))
76
+ end
70
77
  end
71
78
 
72
79
  describe 'allocate_max_amounts' do
@@ -127,6 +134,12 @@ RSpec.describe "Allocator" do
127
134
  new_allocator(3).allocate_max_amounts([Money.empty, Money.empty, Money.empty]),
128
135
  ).to eq([Money.empty, Money.empty, Money.empty])
129
136
  end
137
+
138
+ specify "#allocate_max_amounts allocates the right amount without rounding error" do
139
+ expect(
140
+ new_allocator(24.2).allocate_max_amounts([Money.new(46), Money.new(46), Money.new(50), Money.new(50),Money.new(50)]),
141
+ ).to eq([Money.new(4.6), Money.new(4.6), Money.new(5), Money.new(5), Money.new(5)])
142
+ end
130
143
  end
131
144
 
132
145
  def new_allocator(amount, currency = nil)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  RSpec.shared_examples_for "an object supporting to_money" do
@@ -56,6 +57,6 @@ RSpec.describe BigDecimal do
56
57
  it_should_behave_like "an object supporting to_money"
57
58
 
58
59
  it "parses a zero BigDecimal to Money.zero" do
59
- expect(BigDecimal.new("-0.000").to_money).to eq(Money.zero)
60
+ expect(BigDecimal("-0.000").to_money).to eq(Money.zero)
60
61
  end
61
62
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  RSpec.describe Money::Currency::Loader do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  RSpec.describe "Currency" do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  RSpec.describe Money::Helpers do
@@ -49,12 +50,6 @@ RSpec.describe Money::Helpers do
49
50
  expect(subject.value_to_decimal('invalid')).to eq(0)
50
51
  end
51
52
 
52
- it 'returns the bigdecimal representation of numbers while they are deprecated' do
53
- expect(Money).to receive(:deprecate).exactly(2).times
54
- expect(subject.value_to_decimal('1.23abc')).to eq(amount)
55
- expect(subject.value_to_decimal("1.23\n23")).to eq(amount)
56
- end
57
-
58
53
  it 'raises on invalid object' do
59
54
  expect { subject.value_to_decimal(OpenStruct.new(amount: 1)) }.to raise_error(ArgumentError)
60
55
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  class NormalObject
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  class MoneyRecord < ActiveRecord::Base
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  RSpec.describe MoneyParser do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
  require 'yaml'
3
4
 
@@ -744,8 +745,8 @@ RSpec.describe "Money" do
744
745
  end
745
746
 
746
747
  it "accepts Rational number" do
747
- expect(Money.from_amount(Rational("999999999999999999.999")).value).to eql(BigDecimal.new("1000000000000000000", Money::Helpers::MAX_DECIMAL))
748
- expect(Money.from_amount(Rational("999999999999999999.99")).value).to eql(BigDecimal.new("999999999999999999.99", Money::Helpers::MAX_DECIMAL))
748
+ expect(Money.from_amount(Rational("999999999999999999.999")).value).to eql(BigDecimal("1000000000000000000", Money::Helpers::MAX_DECIMAL))
749
+ expect(Money.from_amount(Rational("999999999999999999.99")).value).to eql(BigDecimal("999999999999999999.99", Money::Helpers::MAX_DECIMAL))
749
750
  end
750
751
 
751
752
  it "raises ArgumentError with unsupported argument" do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  RSpec.describe "NullCurrency" do
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../rubocop_helper'
4
+ require 'rubocop/cop/money/missing_currency'
5
+
6
+ RSpec.describe RuboCop::Cop::Money::MissingCurrency do
7
+ subject(:cop) { described_class.new(config) }
8
+
9
+ let(:config) { RuboCop::Config.new }
10
+
11
+ describe '#on_send' do
12
+ it 'registers an offense for Money.new without a currency argument' do
13
+ expect_offense(<<~RUBY)
14
+ Money.new(1)
15
+ ^^^^^^^^^^^^ Money is missing currency argument
16
+ RUBY
17
+ end
18
+
19
+ it 'does not register an offense for Money.new with currency argument' do
20
+ expect_no_offenses(<<~RUBY)
21
+ Money.new(1, 'CAD')
22
+ RUBY
23
+ end
24
+
25
+ it 'registers an offense for Money.new without a currency argument' do
26
+ expect_offense(<<~RUBY)
27
+ Money.new
28
+ ^^^^^^^^^ Money is missing currency argument
29
+ RUBY
30
+ end
31
+
32
+ it 'registers an offense for Money.from_amount without a currency argument' do
33
+ expect_offense(<<~RUBY)
34
+ Money.from_amount(1)
35
+ ^^^^^^^^^^^^^^^^^^^^ Money is missing currency argument
36
+ RUBY
37
+ end
38
+
39
+ it 'does not register an offense for Money.from_amount with currency argument' do
40
+ expect_no_offenses(<<~RUBY)
41
+ Money.from_amount(1, 'CAD')
42
+ RUBY
43
+ end
44
+
45
+ it 'registers an offense for Money.from_cents without a currency argument' do
46
+ expect_offense(<<~RUBY)
47
+ Money.from_cents(1)
48
+ ^^^^^^^^^^^^^^^^^^^ Money is missing currency argument
49
+ RUBY
50
+ end
51
+
52
+ it 'does not register an offense for Money.from_cents with currency argument' do
53
+ expect_no_offenses(<<~RUBY)
54
+ Money.from_cents(1, 'CAD')
55
+ RUBY
56
+ end
57
+
58
+ it 'registers an offense for to_money without a currency argument' do
59
+ expect_offense(<<~RUBY)
60
+ '1'.to_money
61
+ ^^^^^^^^^^^^ to_money is missing currency argument
62
+ RUBY
63
+ end
64
+
65
+ it 'does not register an offense for to_money with currency argument' do
66
+ expect_no_offenses(<<~RUBY)
67
+ '1'.to_money('CAD')
68
+ RUBY
69
+ end
70
+
71
+ it 'registers an offense for to_money block pass form' do
72
+ expect_offense(<<~RUBY)
73
+ ['1'].map(&:to_money)
74
+ ^^^^^^^^^^^^^^^^^^^^^ to_money is missing currency argument
75
+ RUBY
76
+ end
77
+ end
78
+
79
+ describe '#autocorrect' do
80
+ let(:config) do
81
+ RuboCop::Config.new(
82
+ 'Money/MissingCurrency' => {
83
+ 'ReplacementCurrency' => 'CAD'
84
+ }
85
+ )
86
+ end
87
+
88
+ it 'corrects Money.new without currency' do
89
+ new_source = autocorrect_source('Money.new(1)')
90
+ expect(new_source).to eq("Money.new(1, 'CAD')")
91
+ end
92
+
93
+ it 'corrects Money.new without amount or currency' do
94
+ new_source = autocorrect_source('Money.new')
95
+ expect(new_source).to eq("Money.new(0, 'CAD')")
96
+ end
97
+
98
+ it 'corrects to_money without currency' do
99
+ new_source = autocorrect_source('1.to_money')
100
+ expect(new_source).to eq("1.to_money('CAD')")
101
+ end
102
+
103
+ it 'corrects to_money block pass form' do
104
+ new_source = autocorrect_source("['1'].map(&:to_money)")
105
+ expect(new_source).to eq("['1'].map { |x| x.to_money('CAD') }")
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ require 'rubocop'
6
+ require 'rubocop/rspec/support'
7
+
8
+ RSpec.configure do |config|
9
+ config.include RuboCop::RSpec::ExpectOffense
10
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  ActiveRecord::Schema.define do
2
3
  create_table "money_records", :force => true do |t|
3
4
  t.decimal "price", precision: 20, scale: 3, default: '0.000'
@@ -1,14 +1,10 @@
1
+ # frozen_string_literal: true
1
2
  require 'simplecov'
2
3
  SimpleCov.minimum_coverage 100
3
4
  SimpleCov.start do
4
5
  add_filter "/spec/"
5
6
  end
6
7
 
7
- if ENV['CI'] == 'true'
8
- require 'codecov'
9
- SimpleCov.formatter = SimpleCov::Formatter::Codecov
10
- end
11
-
12
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
14
10
 
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.13.1
4
+ version: 0.14.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-29 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.0'
47
+ version: '6.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '5.0'
54
+ version: '6.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -86,28 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 1.3.6
89
+ version: 1.4.2
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 1.3.6
97
- - !ruby/object:Gem::Dependency
98
- name: bigdecimal
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: 1.3.2
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 1.3.2
96
+ version: 1.4.2
111
97
  description: Manage money in Shopify with a class that wont lose pennies during division!
112
98
  email: gems@shopify.com
113
99
  executables:
@@ -125,9 +111,9 @@ files:
125
111
  - README.md
126
112
  - Rakefile
127
113
  - bin/console
128
- - config/currency_historic.json
129
- - config/currency_iso.json
130
- - config/currency_non_iso.json
114
+ - config/currency_historic.yml
115
+ - config/currency_iso.yml
116
+ - config/currency_non_iso.yml
131
117
  - dev.yml
132
118
  - lib/money.rb
133
119
  - lib/money/accounting_money_parser.rb
@@ -147,6 +133,9 @@ files:
147
133
  - lib/money_column/active_record_hooks.rb
148
134
  - lib/money_column/active_record_type.rb
149
135
  - lib/money_column/railtie.rb
136
+ - lib/rubocop/cop/money.rb
137
+ - lib/rubocop/cop/money/missing_currency.rb
138
+ - lib/shopify-money.rb
150
139
  - money.gemspec
151
140
  - spec/accounting_money_parser_spec.rb
152
141
  - spec/allocator_spec.rb
@@ -159,12 +148,15 @@ files:
159
148
  - spec/money_parser_spec.rb
160
149
  - spec/money_spec.rb
161
150
  - spec/null_currency_spec.rb
151
+ - spec/rubocop/cop/money/missing_currency_spec.rb
152
+ - spec/rubocop_helper.rb
162
153
  - spec/schema.rb
163
154
  - spec/spec_helper.rb
164
155
  homepage: https://github.com/Shopify/money
165
156
  licenses:
166
157
  - MIT
167
- metadata: {}
158
+ metadata:
159
+ allowed_push_host: https://rubygems.org
168
160
  post_install_message:
169
161
  rdoc_options: []
170
162
  require_paths:
@@ -173,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
173
165
  requirements:
174
166
  - - ">="
175
167
  - !ruby/object:Gem::Version
176
- version: '0'
168
+ version: '2.6'
177
169
  required_rubygems_version: !ruby/object:Gem::Requirement
178
170
  requirements:
179
171
  - - ">="
@@ -196,5 +188,7 @@ test_files:
196
188
  - spec/money_parser_spec.rb
197
189
  - spec/money_spec.rb
198
190
  - spec/null_currency_spec.rb
191
+ - spec/rubocop/cop/money/missing_currency_spec.rb
192
+ - spec/rubocop_helper.rb
199
193
  - spec/schema.rb
200
194
  - spec/spec_helper.rb
@@ -1,157 +0,0 @@
1
- {
2
- "cyp": {
3
- "priority": 100,
4
- "iso_code": "CYP",
5
- "name": "Cypriot pound",
6
- "symbol": "£",
7
- "alternate_symbols": [],
8
- "subunit": "Cent",
9
- "subunit_to_unit": 100,
10
- "symbol_first": true,
11
- "html_entity": "&#x00A3;",
12
- "decimal_mark": ".",
13
- "thousands_separator": ",",
14
- "iso_numeric": "196",
15
- "smallest_denomination": 1
16
- },
17
- "eek": {
18
- "priority": 100,
19
- "iso_code": "EEK",
20
- "name": "Estonian Kroon",
21
- "symbol": "KR",
22
- "alternate_symbols": [],
23
- "subunit": "Sent",
24
- "subunit_to_unit": 100,
25
- "symbol_first": false,
26
- "html_entity": "",
27
- "decimal_mark": ".",
28
- "thousands_separator": ",",
29
- "iso_numeric": "233",
30
- "smallest_denomination": 5
31
- },
32
- "ghc": {
33
- "priority": 100,
34
- "iso_code": "GHS",
35
- "name": "Ghanaian Cedi",
36
- "symbol": "₵",
37
- "disambiguate_symbol": "GH₵",
38
- "alternate_symbols": ["GH₵"],
39
- "subunit": "Pesewa",
40
- "subunit_to_unit": 100,
41
- "symbol_first": true,
42
- "html_entity": "&#x20B5;",
43
- "decimal_mark": ".",
44
- "thousands_separator": ",",
45
- "iso_numeric": "288",
46
- "smallest_denomination": 1
47
- },
48
- "mtl": {
49
- "priority": 100,
50
- "iso_code": "MTL",
51
- "name": "Maltese Lira",
52
- "symbol": "₤",
53
- "alternate_symbols": ["Lm"],
54
- "subunit": "Cent",
55
- "subunit_to_unit": 100,
56
- "symbol_first": true,
57
- "html_entity": "&#x00A3;",
58
- "decimal_mark": ".",
59
- "thousands_separator": ",",
60
- "iso_numeric": "470",
61
- "smallest_denomination": 1
62
- },
63
- "tmm": {
64
- "priority": 100,
65
- "iso_code": "TMM",
66
- "name": "Turkmenistani Manat",
67
- "symbol": "m",
68
- "alternate_symbols": [],
69
- "subunit": "Tennesi",
70
- "subunit_to_unit": 100,
71
- "symbol_first": false,
72
- "html_entity": "",
73
- "decimal_mark": ".",
74
- "thousands_separator": ",",
75
- "iso_numeric": "795",
76
- "smallest_denomination": 1
77
- },
78
- "VEB": {
79
- "priority": 100,
80
- "iso_code": "VEB",
81
- "name": "Venezuelan Bolívar",
82
- "symbol": "Bs.",
83
- "alternate_symbols": [],
84
- "subunit": "Céntimo",
85
- "subunit_to_unit": 100,
86
- "symbol_first": true,
87
- "html_entity": "",
88
- "decimal_mark": ",",
89
- "thousands_separator": ".",
90
- "iso_numeric": "862",
91
- "smallest_denomination": 1
92
- },
93
- "zwd": {
94
- "priority": 100,
95
- "iso_code": "ZWD",
96
- "name": "Zimbabwean Dollar",
97
- "symbol": "$",
98
- "disambiguate_symbol": "ZWD",
99
- "alternate_symbols": ["Z$"],
100
- "subunit": "Cent",
101
- "subunit_to_unit": 100,
102
- "symbol_first": true,
103
- "html_entity": "$",
104
- "decimal_mark": ".",
105
- "thousands_separator": ",",
106
- "iso_numeric": "716",
107
- "smallest_denomination": 100
108
- },
109
- "zwl": {
110
- "priority": 100,
111
- "iso_code": "ZWL",
112
- "name": "Zimbabwean Dollar",
113
- "symbol": "$",
114
- "disambiguate_symbol": "ZWL",
115
- "alternate_symbols": ["Z$"],
116
- "subunit": "Cent",
117
- "subunit_to_unit": 100,
118
- "symbol_first": true,
119
- "html_entity": "$",
120
- "decimal_mark": ".",
121
- "thousands_separator": ",",
122
- "iso_numeric": "932",
123
- "smallest_denomination": 100
124
- },
125
- "zwn": {
126
- "priority": 100,
127
- "iso_code": "ZWN",
128
- "name": "Zimbabwean Dollar",
129
- "symbol": "$",
130
- "disambiguate_symbol": "ZWN",
131
- "alternate_symbols": ["Z$"],
132
- "subunit": "Cent",
133
- "subunit_to_unit": 100,
134
- "symbol_first": true,
135
- "html_entity": "$",
136
- "decimal_mark": ".",
137
- "thousands_separator": ",",
138
- "iso_numeric": "942",
139
- "smallest_denomination": 100
140
- },
141
- "zwr": {
142
- "priority": 100,
143
- "iso_code": "ZWR",
144
- "name": "Zimbabwean Dollar",
145
- "symbol": "$",
146
- "disambiguate_symbol": "ZWR",
147
- "alternate_symbols": ["Z$"],
148
- "subunit": "Cent",
149
- "subunit_to_unit": 100,
150
- "symbol_first": true,
151
- "html_entity": "$",
152
- "decimal_mark": ".",
153
- "thousands_separator": ",",
154
- "iso_numeric": "935",
155
- "smallest_denomination": 100
156
- }
157
- }