shopify-money 0.15.0 → 1.0.1.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -2
- data/UPGRADING.md +57 -0
- data/config/currency_historic.yml +15 -1
- data/config/currency_iso.yml +22 -4
- data/lib/money/allocator.rb +3 -1
- data/lib/money/config.rb +26 -0
- data/lib/money/helpers.rb +8 -4
- data/lib/money/money.rb +48 -19
- data/lib/money/money_parser.rb +11 -5
- data/lib/money/rails/job_argument_serializer.rb +22 -0
- data/lib/money/railtie.rb +12 -0
- data/lib/money/version.rb +1 -1
- data/lib/money.rb +3 -1
- data/lib/money_column/active_record_hooks.rb +12 -7
- data/spec/accounting_money_parser_spec.rb +4 -2
- data/spec/config_spec.rb +60 -0
- data/spec/helpers_spec.rb +11 -5
- data/spec/money_column_spec.rb +54 -33
- data/spec/money_parser_spec.rb +19 -7
- data/spec/money_spec.rb +130 -47
- data/spec/rails/job_argument_serializer_spec.rb +20 -0
- data/spec/rails_spec_helper.rb +11 -0
- data/spec/spec_helper.rb +15 -0
- metadata +15 -8
- data/lib/money_accessor.rb +0 -33
- data/spec/money_accessor_spec.rb +0 -87
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae5e66176d7c6e79d5c6346a3ca6915489d3750d7bb67aa68973c52a0198cb3f
|
4
|
+
data.tar.gz: 7219d1edd8665498e64eb06e6212028d8ec559dd5ce9206cbe698e5b1e42d2ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4188d1251f442d347b287b3b3ac69c08f716b149ef9cc8a78f2c8e75fb563524f3818a9b8116cf2d433117a9e66cbb96b428aed9bf8cd9f9e79eaefa94ca5c45
|
7
|
+
data.tar.gz: 9e70a02a0acdf5eca299ad5f4b201b3fb44a98977d972d08db4167938f24793b732a55f25d6ea3585c8c63d92b014bc108083cce1aef1659b65c628f3ab0b27f
|
data/README.md
CHANGED
@@ -18,7 +18,11 @@ money_column expects a DECIMAL(21,3) database field.
|
|
18
18
|
|
19
19
|
## Installation
|
20
20
|
|
21
|
-
gem 'shopify-money'
|
21
|
+
gem 'shopify-money'
|
22
|
+
|
23
|
+
## Upgrading to v1.0
|
24
|
+
|
25
|
+
see instructions and breaking changes: https://github.com/Shopify/money/blob/master/UPGRADING.md
|
22
26
|
|
23
27
|
## Usage
|
24
28
|
|
@@ -77,7 +81,9 @@ By default `Money` defaults to Money::NullCurrency as its currency. This is a
|
|
77
81
|
global variable that can be changed using:
|
78
82
|
|
79
83
|
``` ruby
|
80
|
-
Money.
|
84
|
+
Money.configure do |config|
|
85
|
+
config.default_currency = Money::Currency.new("USD")
|
86
|
+
end
|
81
87
|
```
|
82
88
|
|
83
89
|
In web apps you might want to set the default currency on a per request basis.
|
data/UPGRADING.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
## Upgrading to v1.0
|
2
|
+
|
3
|
+
In an initializer add the following
|
4
|
+
```ruby
|
5
|
+
Money.configure do |config|
|
6
|
+
config.legacy_default_currency!
|
7
|
+
config.legacy_deprecations!
|
8
|
+
config.legacy_json_format!
|
9
|
+
#...
|
10
|
+
end
|
11
|
+
```
|
12
|
+
|
13
|
+
Remove each legacy setting making sure your app functions as expected.
|
14
|
+
|
15
|
+
### Legacy support
|
16
|
+
|
17
|
+
#### legacy_default_currency!
|
18
|
+
|
19
|
+
By enabling this setting your app will accept money object that are missing a currency
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
Money.new(1) #=> value: 1, currency: XXX
|
23
|
+
```
|
24
|
+
|
25
|
+
#### legacy_deprecations!
|
26
|
+
|
27
|
+
invalid money values return zero
|
28
|
+
```ruby
|
29
|
+
Money.new('a', 'USD') #=> Money.new(0, 'USD')
|
30
|
+
```
|
31
|
+
|
32
|
+
invalid currency is ignored
|
33
|
+
```ruby
|
34
|
+
Money.new(1, 'ABCD') #=> Money.new(1)
|
35
|
+
```
|
36
|
+
|
37
|
+
mathematical operations between objects are allowed
|
38
|
+
```ruby
|
39
|
+
Money.new(1, 'USD') + Money.new(1, 'CAD') #=> Money.new(2, 'USD')
|
40
|
+
```
|
41
|
+
|
42
|
+
parsing a string with invalid delimiters
|
43
|
+
```ruby
|
44
|
+
Money.parse('123*12') #=> Money.new(123)
|
45
|
+
```
|
46
|
+
|
47
|
+
#### legacy_json_format!
|
48
|
+
|
49
|
+
to_json will return only the value (no currency)
|
50
|
+
```ruby
|
51
|
+
# with legacy_json_format!
|
52
|
+
money.to_json #=> "1"
|
53
|
+
|
54
|
+
# without
|
55
|
+
money.to_json #=> { value: 1, currency: 'USD' }
|
56
|
+
```
|
57
|
+
|
@@ -29,7 +29,7 @@ eek:
|
|
29
29
|
smallest_denomination: 5
|
30
30
|
ghc:
|
31
31
|
priority: 100
|
32
|
-
iso_code:
|
32
|
+
iso_code: GHC
|
33
33
|
name: Ghanaian Cedi
|
34
34
|
symbol: "₵"
|
35
35
|
disambiguate_symbol: GH₵
|
@@ -73,6 +73,20 @@ mtl:
|
|
73
73
|
thousands_separator: ","
|
74
74
|
iso_numeric: '470'
|
75
75
|
smallest_denomination: 1
|
76
|
+
std:
|
77
|
+
priority: 100
|
78
|
+
iso_code: STD
|
79
|
+
name: São Tomé and Príncipe Dobra
|
80
|
+
symbol: Db
|
81
|
+
alternate_symbols: []
|
82
|
+
subunit: Cêntimo
|
83
|
+
subunit_to_unit: 100
|
84
|
+
symbol_first: false
|
85
|
+
html_entity: ''
|
86
|
+
decimal_mark: "."
|
87
|
+
thousands_separator: ","
|
88
|
+
iso_numeric: '678'
|
89
|
+
smallest_denomination: 10000
|
76
90
|
tmm:
|
77
91
|
priority: 100
|
78
92
|
iso_code: TMM
|
data/config/currency_iso.yml
CHANGED
@@ -2029,9 +2029,9 @@ ssp:
|
|
2029
2029
|
thousands_separator: ","
|
2030
2030
|
iso_numeric: '728'
|
2031
2031
|
smallest_denomination: 5
|
2032
|
-
|
2032
|
+
stn:
|
2033
2033
|
priority: 100
|
2034
|
-
iso_code:
|
2034
|
+
iso_code: STN
|
2035
2035
|
name: São Tomé and Príncipe Dobra
|
2036
2036
|
symbol: Db
|
2037
2037
|
alternate_symbols: []
|
@@ -2041,8 +2041,8 @@ std:
|
|
2041
2041
|
html_entity: ''
|
2042
2042
|
decimal_mark: "."
|
2043
2043
|
thousands_separator: ","
|
2044
|
-
iso_numeric: '
|
2045
|
-
smallest_denomination:
|
2044
|
+
iso_numeric: '930'
|
2045
|
+
smallest_denomination: 1
|
2046
2046
|
svc:
|
2047
2047
|
priority: 100
|
2048
2048
|
iso_code: SVC
|
@@ -2304,6 +2304,24 @@ uzs:
|
|
2304
2304
|
thousands_separator: ","
|
2305
2305
|
iso_numeric: '860'
|
2306
2306
|
smallest_denomination: 100
|
2307
|
+
ved:
|
2308
|
+
priority: 100
|
2309
|
+
iso_code: VED
|
2310
|
+
name: Venezuelan Bolívar soberano
|
2311
|
+
symbol: Bs.D.
|
2312
|
+
alternate_symbols: []
|
2313
|
+
subunit: Céntimo
|
2314
|
+
subunit_to_unit: 100
|
2315
|
+
symbol_first: true
|
2316
|
+
html_entity: ''
|
2317
|
+
decimal_mark: ","
|
2318
|
+
thousands_separator: "."
|
2319
|
+
iso_numeric: '926'
|
2320
|
+
# "On 1 Oct 2021, [...] another (redenomination) happened, but called 'Nueva expresión monetaria',
|
2321
|
+
# or new monetary expression, which removed 6 zeroes from the currency without affecting its denomination."
|
2322
|
+
# The VED has banknotes in denominations of 5, 10, 20, 50, and 100, and coins in 50 céntimos and 1 Bs.D.
|
2323
|
+
# Source: https://en.wikipedia.org/wiki/Venezuelan_bol%C3%ADvar
|
2324
|
+
smallest_denomination: 1
|
2307
2325
|
ves:
|
2308
2326
|
priority: 100
|
2309
2327
|
iso_code: VES
|
data/lib/money/allocator.rb
CHANGED
@@ -7,6 +7,8 @@ class Money
|
|
7
7
|
super
|
8
8
|
end
|
9
9
|
|
10
|
+
ONE = BigDecimal("1")
|
11
|
+
|
10
12
|
# Allocates money between different parties without losing pennies.
|
11
13
|
# After the mathematically split has been performed, left over pennies will
|
12
14
|
# be distributed round-robin amongst the parties. This means that parties
|
@@ -43,7 +45,7 @@ class Money
|
|
43
45
|
splits.map!(&:to_r)
|
44
46
|
allocations = splits.inject(0, :+)
|
45
47
|
|
46
|
-
if (allocations -
|
48
|
+
if (allocations - ONE) > Float::EPSILON
|
47
49
|
raise ArgumentError, "splits add to more than 100%"
|
48
50
|
end
|
49
51
|
|
data/lib/money/config.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Money
|
4
|
+
class Config
|
5
|
+
attr_accessor :parser, :default_currency, :legacy_json_format, :legacy_deprecations
|
6
|
+
|
7
|
+
def legacy_default_currency!
|
8
|
+
@default_currency ||= Money::NULL_CURRENCY
|
9
|
+
end
|
10
|
+
|
11
|
+
def legacy_deprecations!
|
12
|
+
@legacy_deprecations = true
|
13
|
+
end
|
14
|
+
|
15
|
+
def legacy_json_format!
|
16
|
+
@legacy_json_format = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@parser = MoneyParser
|
21
|
+
@default_currency = nil
|
22
|
+
@legacy_json_format = false
|
23
|
+
@legacy_deprecations = false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/money/helpers.rb
CHANGED
@@ -28,7 +28,7 @@ class Money
|
|
28
28
|
when Rational
|
29
29
|
BigDecimal(num, MAX_DECIMAL)
|
30
30
|
when String
|
31
|
-
decimal = BigDecimal(num, exception:
|
31
|
+
decimal = BigDecimal(num, exception: !Money.config.legacy_deprecations)
|
32
32
|
return decimal if decimal
|
33
33
|
|
34
34
|
Money.deprecate("using Money.new('#{num}') is deprecated and will raise an ArgumentError in the next major release")
|
@@ -46,7 +46,7 @@ class Money
|
|
46
46
|
currency
|
47
47
|
when nil, ''
|
48
48
|
default = Money.current_currency || Money.default_currency
|
49
|
-
raise(
|
49
|
+
raise(Money::Currency::UnknownCurrency, 'missing currency') if default.nil? || default == ''
|
50
50
|
value_to_currency(default)
|
51
51
|
when 'xxx', 'XXX'
|
52
52
|
Money::NULL_CURRENCY
|
@@ -54,8 +54,12 @@ class Money
|
|
54
54
|
begin
|
55
55
|
Currency.find!(currency)
|
56
56
|
rescue Money::Currency::UnknownCurrency => error
|
57
|
-
Money.
|
58
|
-
|
57
|
+
if Money.config.legacy_deprecations
|
58
|
+
Money.deprecate(error.message)
|
59
|
+
Money::NULL_CURRENCY
|
60
|
+
else
|
61
|
+
raise error
|
62
|
+
end
|
59
63
|
end
|
60
64
|
else
|
61
65
|
raise ArgumentError, "could not parse as currency #{currency.inspect}"
|
data/lib/money/money.rb
CHANGED
@@ -11,7 +11,14 @@ class Money
|
|
11
11
|
def_delegators :@value, :zero?, :nonzero?, :positive?, :negative?, :to_i, :to_f, :hash
|
12
12
|
|
13
13
|
class << self
|
14
|
-
|
14
|
+
extend Forwardable
|
15
|
+
attr_accessor :config
|
16
|
+
def_delegators :@config, :parser, :parser=, :default_currency, :default_currency=
|
17
|
+
|
18
|
+
def configure
|
19
|
+
self.config ||= Config.new
|
20
|
+
yield(config) if block_given?
|
21
|
+
end
|
15
22
|
|
16
23
|
def new(value = 0, currency = nil)
|
17
24
|
value = Helpers.value_to_decimal(value)
|
@@ -73,13 +80,8 @@ class Money
|
|
73
80
|
Money.current_currency = old_currency
|
74
81
|
end
|
75
82
|
end
|
76
|
-
|
77
|
-
def default_settings
|
78
|
-
self.parser = MoneyParser
|
79
|
-
self.default_currency = Money::NULL_CURRENCY
|
80
|
-
end
|
81
83
|
end
|
82
|
-
|
84
|
+
configure
|
83
85
|
|
84
86
|
def initialize(value, currency)
|
85
87
|
raise ArgumentError if value.nan?
|
@@ -126,20 +128,27 @@ class Money
|
|
126
128
|
|
127
129
|
def +(other)
|
128
130
|
arithmetic(other) do |money|
|
131
|
+
return self if money.value == 0 && !no_currency?
|
129
132
|
Money.new(value + money.value, calculated_currency(money.currency))
|
130
133
|
end
|
131
134
|
end
|
132
135
|
|
133
136
|
def -(other)
|
134
137
|
arithmetic(other) do |money|
|
138
|
+
return self if money.value == 0 && !no_currency?
|
135
139
|
Money.new(value - money.value, calculated_currency(money.currency))
|
136
140
|
end
|
137
141
|
end
|
138
142
|
|
139
143
|
def *(numeric)
|
140
144
|
unless numeric.is_a?(Numeric)
|
141
|
-
|
145
|
+
if Money.config.legacy_deprecations
|
146
|
+
Money.deprecate("Multiplying Money with #{numeric.class.name} is deprecated and will be removed in the next major release.")
|
147
|
+
else
|
148
|
+
raise ArgumentError, "Money objects can only be multiplied by a Numeric"
|
149
|
+
end
|
142
150
|
end
|
151
|
+
return self if numeric == 1
|
143
152
|
Money.new(value.to_r * numeric, currency)
|
144
153
|
end
|
145
154
|
|
@@ -198,8 +207,12 @@ class Money
|
|
198
207
|
|
199
208
|
curr = Helpers.value_to_currency(curr)
|
200
209
|
unless currency.compatible?(curr)
|
201
|
-
|
202
|
-
|
210
|
+
msg = "mathematical operation not permitted for Money objects with different currencies #{curr} and #{currency}"
|
211
|
+
if Money.config.legacy_deprecations
|
212
|
+
Money.deprecate("#{msg}. A Money::IncompatibleCurrencyError will raise in the next major release")
|
213
|
+
else
|
214
|
+
raise Money::IncompatibleCurrencyError, msg
|
215
|
+
end
|
203
216
|
end
|
204
217
|
|
205
218
|
self
|
@@ -209,14 +222,14 @@ class Money
|
|
209
222
|
value
|
210
223
|
end
|
211
224
|
|
212
|
-
def
|
225
|
+
def to_fs(style = nil)
|
213
226
|
units = case style
|
214
227
|
when :legacy_dollars
|
215
228
|
2
|
216
229
|
when :amount, nil
|
217
230
|
currency.minor_units
|
218
231
|
else
|
219
|
-
raise ArgumentError, "Unexpected
|
232
|
+
raise ArgumentError, "Unexpected format: #{style}"
|
220
233
|
end
|
221
234
|
|
222
235
|
rounded_value = value.round(units)
|
@@ -228,25 +241,41 @@ class Money
|
|
228
241
|
sprintf("%s%d.%0#{units}d", sign, rounded_value.truncate, rounded_value.frac * (10 ** units))
|
229
242
|
end
|
230
243
|
end
|
244
|
+
alias_method :to_s, :to_fs
|
245
|
+
alias_method :to_formatted_s, :to_fs
|
231
246
|
|
232
|
-
def to_json(options =
|
233
|
-
|
247
|
+
def to_json(options = nil)
|
248
|
+
if (options.is_a?(Hash) && options.delete(:legacy_format)) || Money.config.legacy_json_format
|
249
|
+
to_s
|
250
|
+
else
|
251
|
+
as_json(options).to_json
|
252
|
+
end
|
234
253
|
end
|
235
254
|
|
236
|
-
def as_json(
|
237
|
-
|
255
|
+
def as_json(options = nil)
|
256
|
+
if (options.is_a?(Hash) && options.delete(:legacy_format)) || Money.config.legacy_json_format
|
257
|
+
to_s
|
258
|
+
else
|
259
|
+
{ value: to_s(:amount), currency: currency.to_s }
|
260
|
+
end
|
238
261
|
end
|
239
262
|
|
240
263
|
def abs
|
241
|
-
|
264
|
+
abs = value.abs
|
265
|
+
return self if value == abs
|
266
|
+
Money.new(abs, currency)
|
242
267
|
end
|
243
268
|
|
244
269
|
def floor
|
245
|
-
|
270
|
+
floor = value.floor
|
271
|
+
return self if floor == value
|
272
|
+
Money.new(floor, currency)
|
246
273
|
end
|
247
274
|
|
248
275
|
def round(ndigits=0)
|
249
|
-
|
276
|
+
round = value.round(ndigits)
|
277
|
+
return self if round == value
|
278
|
+
Money.new(round, currency)
|
250
279
|
end
|
251
280
|
|
252
281
|
def fraction(rate)
|
data/lib/money/money_parser.rb
CHANGED
@@ -82,9 +82,12 @@ class MoneyParser
|
|
82
82
|
number = number.to_s.strip
|
83
83
|
|
84
84
|
if number.empty?
|
85
|
-
|
86
|
-
|
87
|
-
|
85
|
+
if Money.config.legacy_deprecations && !strict
|
86
|
+
Money.deprecate("invalid money strings will raise in the next major release \"#{input}\"")
|
87
|
+
return '0'
|
88
|
+
else
|
89
|
+
raise MoneyFormatError, "invalid money string: #{input}"
|
90
|
+
end
|
88
91
|
end
|
89
92
|
|
90
93
|
marks = number.scan(/[#{ESCAPED_MARKS}]/).flatten
|
@@ -107,8 +110,11 @@ class MoneyParser
|
|
107
110
|
return amount.tr(ESCAPED_NON_COMMA_MARKS, '').sub(',', '.')
|
108
111
|
end
|
109
112
|
|
110
|
-
|
111
|
-
|
113
|
+
if Money.config.legacy_deprecations && !strict
|
114
|
+
Money.deprecate("invalid money strings will raise in the next major release \"#{input}\"")
|
115
|
+
else
|
116
|
+
raise MoneyFormatError, "invalid money string: #{input}"
|
117
|
+
end
|
112
118
|
|
113
119
|
normalize_number(number, marks, currency)
|
114
120
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Money
|
4
|
+
module Rails
|
5
|
+
class JobArgumentSerializer < ::ActiveJob::Serializers::ObjectSerializer
|
6
|
+
def serialize(money)
|
7
|
+
super("value" => money.value, "currency" => money.currency.iso_code)
|
8
|
+
end
|
9
|
+
|
10
|
+
def deserialize(hash)
|
11
|
+
Money.new(hash["value"], hash["currency"])
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def klass
|
17
|
+
Money
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Money
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "shopify-money.setup_active_job_serializer" do
|
6
|
+
ActiveSupport.on_load :active_job do
|
7
|
+
require_relative "rails/job_argument_serializer"
|
8
|
+
ActiveJob::Serializers.add_serializers ::Money::Rails::JobArgumentSerializer
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/money/version.rb
CHANGED
data/lib/money.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require_relative 'money/version'
|
2
3
|
require_relative 'money/money_parser'
|
3
4
|
require_relative 'money/helpers'
|
4
5
|
require_relative 'money/currency'
|
5
6
|
require_relative 'money/null_currency'
|
6
7
|
require_relative 'money/allocator'
|
8
|
+
require_relative 'money/config'
|
7
9
|
require_relative 'money/money'
|
8
10
|
require_relative 'money/errors'
|
9
11
|
require_relative 'money/deprecations'
|
10
12
|
require_relative 'money/accounting_money_parser'
|
11
13
|
require_relative 'money/core_extensions'
|
12
|
-
require_relative 'money_accessor'
|
13
14
|
require_relative 'money_column' if defined?(ActiveRecord)
|
15
|
+
require_relative 'money/railtie' if defined?(Rails::Railtie)
|
14
16
|
|
15
17
|
require_relative 'rubocop/cop/money' if defined?(RuboCop)
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module MoneyColumn
|
3
|
+
class CurrencyReadOnlyError < StandardError; end
|
4
|
+
|
3
5
|
module ActiveRecordHooks
|
4
6
|
def self.included(base)
|
5
7
|
base.extend(ClassMethods)
|
@@ -49,16 +51,19 @@ module MoneyColumn
|
|
49
51
|
return self[column] = nil
|
50
52
|
end
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
if !money.is_a?(Money)
|
55
|
-
return self[column] = Money.new(money, currency_raw_source).value
|
54
|
+
unless money.is_a?(Money)
|
55
|
+
return self[column] = Money::Helpers.value_to_decimal(money)
|
56
56
|
end
|
57
57
|
|
58
58
|
if options[:currency_read_only]
|
59
|
-
|
60
|
-
if
|
61
|
-
|
59
|
+
currency = options[:currency] || try(options[:currency_column])
|
60
|
+
if currency && !money.currency.compatible?(Money::Helpers.value_to_currency(currency))
|
61
|
+
msg = "[money_column] currency mismatch between #{currency} and #{money.currency} in column #{column}."
|
62
|
+
if Money.config.legacy_deprecations
|
63
|
+
Money.deprecate(msg)
|
64
|
+
else
|
65
|
+
raise MoneyColumn::CurrencyReadOnlyError, msg
|
66
|
+
end
|
62
67
|
end
|
63
68
|
else
|
64
69
|
self[options[:currency_column]] = money.currency.to_s unless money.no_currency?
|
@@ -20,8 +20,10 @@ RSpec.describe AccountingMoneyParser do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it "parses an invalid string to $0" do
|
23
|
-
|
24
|
-
|
23
|
+
configure(legacy_deprecations: true) do
|
24
|
+
expect(Money).to receive(:deprecate).once
|
25
|
+
expect(@parser.parse("no money", 'USD')).to eq(Money.new(0, 'USD'))
|
26
|
+
end
|
25
27
|
end
|
26
28
|
|
27
29
|
it "parses a single digit integer string" do
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
RSpec.describe "Money::Config" do
|
5
|
+
describe 'legacy_deprecations' do
|
6
|
+
it "respects the default currency" do
|
7
|
+
configure(default_currency: 'USD', legacy_deprecations: true) do
|
8
|
+
expect(Money.default_currency).to eq("USD")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'defaults to not opt-in to v1' do
|
13
|
+
expect(Money::Config.new.legacy_deprecations).to eq(false)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'legacy_deprecations returns true when opting in to v1' do
|
17
|
+
configure(legacy_deprecations: true) do
|
18
|
+
expect(Money.config.legacy_deprecations).to eq(true)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'sets the deprecations to raise' do
|
23
|
+
configure(legacy_deprecations: true) do
|
24
|
+
expect { Money.deprecate("test") }.to raise_error(ActiveSupport::DeprecationException)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'legacy_deprecations defaults to NULL_CURRENCY' do
|
29
|
+
configure(legacy_default_currency: true) do
|
30
|
+
expect(Money.config.default_currency).to eq(Money::NULL_CURRENCY)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'parser' do
|
36
|
+
it 'defaults to MoneyParser' do
|
37
|
+
expect(Money::Config.new.parser).to eq(MoneyParser)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'can be set to a new parser' do
|
41
|
+
configure(parser: AccountingMoneyParser) do
|
42
|
+
expect(Money.config.parser).to eq(AccountingMoneyParser)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'default_currency' do
|
48
|
+
it 'defaults to nil' do
|
49
|
+
configure do
|
50
|
+
expect(Money.config.default_currency).to eq(nil)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'can be set to a new currency' do
|
55
|
+
configure(default_currency: 'USD') do
|
56
|
+
expect(Money.config.default_currency).to eq('USD')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/helpers_spec.rb
CHANGED
@@ -46,8 +46,10 @@ RSpec.describe Money::Helpers do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'invalid string returns zero' do
|
49
|
-
|
50
|
-
|
49
|
+
configure(legacy_deprecations: true) do
|
50
|
+
expect(Money).to receive(:deprecate).once
|
51
|
+
expect(subject.value_to_decimal('invalid')).to eq(0)
|
52
|
+
end
|
51
53
|
end
|
52
54
|
|
53
55
|
it 'raises on invalid object' do
|
@@ -70,7 +72,9 @@ RSpec.describe Money::Helpers do
|
|
70
72
|
end
|
71
73
|
|
72
74
|
it 'returns the default currency when value is empty' do
|
73
|
-
|
75
|
+
configure(legacy_deprecations: true, default_currency: 'USD') do
|
76
|
+
expect(subject.value_to_currency('')).to eq(Money::Currency.new('USD'))
|
77
|
+
end
|
74
78
|
end
|
75
79
|
|
76
80
|
it 'returns the default currency when value is xxx' do
|
@@ -86,8 +90,10 @@ RSpec.describe Money::Helpers do
|
|
86
90
|
end
|
87
91
|
|
88
92
|
it 'returns the null currency when invalid iso is passed' do
|
89
|
-
|
90
|
-
|
93
|
+
configure(legacy_deprecations: true) do
|
94
|
+
expect(Money).to receive(:deprecate).once
|
95
|
+
expect(subject.value_to_currency('invalid')).to eq(Money::NULL_CURRENCY)
|
96
|
+
end
|
91
97
|
end
|
92
98
|
|
93
99
|
it 'raises on invalid object' do
|