money 6.18.0 → 7.0.0
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/CHANGELOG.md +110 -49
- data/LICENSE +1 -1
- data/README.md +55 -54
- data/config/currency_backwards_compatible.json +17 -0
- data/config/currency_iso.json +47 -33
- data/config/currency_non_iso.json +1 -1
- data/lib/money/bank/base.rb +2 -0
- data/lib/money/bank/single_currency.rb +2 -0
- data/lib/money/bank/variable_exchange.rb +2 -0
- data/lib/money/currency/heuristics.rb +1 -1
- data/lib/money/currency/loader.rb +2 -0
- data/lib/money/currency.rb +35 -9
- data/lib/money/locale_backend/base.rb +2 -0
- data/lib/money/locale_backend/currency.rb +2 -0
- data/lib/money/locale_backend/errors.rb +2 -0
- data/lib/money/locale_backend/i18n.rb +4 -1
- data/lib/money/money/allocation.rb +51 -10
- data/lib/money/money/arithmetic.rb +31 -13
- data/lib/money/money/constructors.rb +2 -0
- data/lib/money/money/formatter.rb +13 -62
- data/lib/money/money/formatting_rules.rb +9 -59
- data/lib/money/money/locale_backend.rb +1 -3
- data/lib/money/money.rb +105 -119
- data/lib/money/rates_store/memory.rb +2 -0
- data/lib/money/version.rb +3 -1
- data/lib/money.rb +3 -0
- data/money.gemspec +8 -3
- metadata +22 -17
- data/lib/money/locale_backend/legacy.rb +0 -28
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Money
|
|
4
4
|
class FormattingRules
|
|
@@ -9,12 +9,9 @@ class Money
|
|
|
9
9
|
@rules = normalize_formatting_rules(raw_rules)
|
|
10
10
|
|
|
11
11
|
@rules = default_formatting_rules.merge(@rules) unless @rules[:ignore_defaults]
|
|
12
|
-
@rules = localize_formatting_rules(@rules)
|
|
13
12
|
@rules = translate_formatting_rules(@rules) if @rules[:translate]
|
|
14
|
-
@rules[:format] ||=
|
|
13
|
+
@rules[:format] ||= determine_format
|
|
15
14
|
@rules[:delimiter_pattern] ||= delimiter_pattern_rule(@rules)
|
|
16
|
-
|
|
17
|
-
warn_about_deprecated_rules(@rules)
|
|
18
15
|
end
|
|
19
16
|
|
|
20
17
|
def [](key)
|
|
@@ -71,72 +68,25 @@ class Money
|
|
|
71
68
|
rules
|
|
72
69
|
end
|
|
73
70
|
|
|
74
|
-
def
|
|
75
|
-
|
|
76
|
-
rules[:symbol] = "円" unless rules[:symbol] == false
|
|
77
|
-
rules[:format] = '%n%u'
|
|
78
|
-
end
|
|
79
|
-
rules
|
|
71
|
+
def determine_format
|
|
72
|
+
Money.locale_backend&.lookup(:format, @currency) || default_format
|
|
80
73
|
end
|
|
81
74
|
|
|
82
|
-
def
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
symbol_position = symbol_position_from(rules)
|
|
86
|
-
|
|
87
|
-
if symbol_position == :before
|
|
88
|
-
rules.fetch(:symbol_before_without_space, true) ? '%u%n' : '%u %n'
|
|
75
|
+
def default_format
|
|
76
|
+
if currency.format
|
|
77
|
+
currency.format
|
|
89
78
|
else
|
|
90
|
-
|
|
79
|
+
currency.symbol_first? ? "%u%n" : "%n %u"
|
|
91
80
|
end
|
|
92
81
|
end
|
|
93
82
|
|
|
94
83
|
def delimiter_pattern_rule(rules)
|
|
95
84
|
if rules[:south_asian_number_formatting]
|
|
96
|
-
# from
|
|
85
|
+
# from https://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
|
|
97
86
|
/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
|
|
98
87
|
else
|
|
99
88
|
/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
|
|
100
89
|
end
|
|
101
90
|
end
|
|
102
|
-
|
|
103
|
-
def symbol_position_from(rules)
|
|
104
|
-
if rules.has_key?(:symbol_position)
|
|
105
|
-
if [:before, :after].include?(rules[:symbol_position])
|
|
106
|
-
return rules[:symbol_position]
|
|
107
|
-
else
|
|
108
|
-
raise ArgumentError, ":symbol_position must be ':before' or ':after'"
|
|
109
|
-
end
|
|
110
|
-
elsif currency.symbol_first?
|
|
111
|
-
:before
|
|
112
|
-
else
|
|
113
|
-
:after
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
def warn_about_deprecated_rules(rules)
|
|
118
|
-
if rules.has_key?(:symbol_position)
|
|
119
|
-
position = rules[:symbol_position]
|
|
120
|
-
template = position == :before ? '%u %n' : '%n %u'
|
|
121
|
-
|
|
122
|
-
warn "[DEPRECATION] `symbol_position: :#{position}` is deprecated - you can replace it with `format: #{template}`"
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
if rules.has_key?(:symbol_before_without_space)
|
|
126
|
-
warn "[DEPRECATION] `symbol_before_without_space:` option is deprecated - you can replace it with `format: '%u%n'`"
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
if rules.has_key?(:symbol_after_without_space)
|
|
130
|
-
warn "[DEPRECATION] `symbol_after_without_space:` option is deprecated - you can replace it with `format: '%n%u'`"
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
if rules.has_key?(:html)
|
|
134
|
-
warn "[DEPRECATION] `html` is deprecated - use `html_wrap` instead. Please note that `html_wrap` will wrap all parts of currency and if you use `with_currency` option, currency element class changes from `currency` to `money-currency`."
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
if rules.has_key?(:html_wrap_symbol)
|
|
138
|
-
warn "[DEPRECATION] `html_wrap_symbol` is deprecated - use `html_wrap` instead. Please note that `html_wrap` will wrap all parts of currency."
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
91
|
end
|
|
142
92
|
end
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'money/locale_backend/errors'
|
|
4
|
-
require 'money/locale_backend/legacy'
|
|
5
4
|
require 'money/locale_backend/i18n'
|
|
6
5
|
require 'money/locale_backend/currency'
|
|
7
6
|
|
|
8
7
|
class Money
|
|
9
8
|
module LocaleBackend
|
|
10
9
|
BACKENDS = {
|
|
11
|
-
legacy: Money::LocaleBackend::Legacy,
|
|
12
10
|
i18n: Money::LocaleBackend::I18n,
|
|
13
11
|
currency: Money::LocaleBackend::Currency
|
|
14
12
|
}.freeze
|
data/lib/money/money.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
require "money/bank/variable_exchange"
|
|
3
4
|
require "money/bank/single_currency"
|
|
4
5
|
require "money/money/arithmetic"
|
|
@@ -15,7 +16,7 @@ require "money/money/locale_backend"
|
|
|
15
16
|
#
|
|
16
17
|
# Money is a value object and should be treated as immutable.
|
|
17
18
|
#
|
|
18
|
-
# @see
|
|
19
|
+
# @see https://en.wikipedia.org/wiki/Money
|
|
19
20
|
class Money
|
|
20
21
|
include Comparable
|
|
21
22
|
include Money::Arithmetic
|
|
@@ -68,15 +69,31 @@ class Money
|
|
|
68
69
|
#
|
|
69
70
|
# @see infinite_precision
|
|
70
71
|
def round_to_nearest_cash_value
|
|
72
|
+
warn "[DEPRECATION] `round_to_nearest_cash_value` is deprecated - use " \
|
|
73
|
+
"`to_nearest_cash_value.fractional` instead"
|
|
74
|
+
|
|
75
|
+
to_nearest_cash_value.fractional
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Round a given amount of money to the nearest possible money in cash value.
|
|
79
|
+
# For example, in Swiss franc (CHF), the smallest possible amount of cash
|
|
80
|
+
# value is CHF 0.05. Therefore, this method rounds CHF 0.07 to CHF 0.05, and
|
|
81
|
+
# CHF 0.08 to CHF 0.10.
|
|
82
|
+
#
|
|
83
|
+
# @return [Money]
|
|
84
|
+
def to_nearest_cash_value
|
|
71
85
|
unless self.currency.smallest_denomination
|
|
72
|
-
raise UndefinedSmallestDenomination,
|
|
86
|
+
raise UndefinedSmallestDenomination,
|
|
87
|
+
"Smallest denomination of this currency is not defined"
|
|
73
88
|
end
|
|
74
89
|
|
|
75
90
|
fractional = as_d(@fractional)
|
|
76
91
|
smallest_denomination = as_d(self.currency.smallest_denomination)
|
|
77
|
-
rounded_value =
|
|
92
|
+
rounded_value =
|
|
93
|
+
(fractional / smallest_denomination)
|
|
94
|
+
.round(0, self.class.rounding_mode) * smallest_denomination
|
|
78
95
|
|
|
79
|
-
return_value(rounded_value)
|
|
96
|
+
dup_with(fractional: return_value(rounded_value))
|
|
80
97
|
end
|
|
81
98
|
|
|
82
99
|
# @!attribute [r] currency
|
|
@@ -116,11 +133,6 @@ class Money
|
|
|
116
133
|
#
|
|
117
134
|
# @return [Hash]
|
|
118
135
|
#
|
|
119
|
-
# @!attribute [rw] use_i18n
|
|
120
|
-
# Used to disable i18n even if it's used by other components of your app.
|
|
121
|
-
#
|
|
122
|
-
# @return [Boolean]
|
|
123
|
-
#
|
|
124
136
|
# @!attribute [rw] default_infinite_precision
|
|
125
137
|
# @return [Boolean] Use this to enable infinite precision cents as the
|
|
126
138
|
# global default
|
|
@@ -129,34 +141,38 @@ class Money
|
|
|
129
141
|
# Used to specify precision for converting Rational to BigDecimal
|
|
130
142
|
#
|
|
131
143
|
# @return [Integer]
|
|
132
|
-
|
|
133
|
-
|
|
144
|
+
#
|
|
145
|
+
# @!attribute [rw] strict_eql_compare
|
|
146
|
+
# Use this to specify how +Money#eql?+ behaves. Opt-in to the new
|
|
147
|
+
# behavior by setting this to +true+ and disable warnings when comparing
|
|
148
|
+
# zero amounts with different currencies.
|
|
149
|
+
#
|
|
150
|
+
# @example
|
|
151
|
+
# Money.strict_eql_compare = false # (default)
|
|
152
|
+
# Money.new(0, "USD").eql?(Money.new(0, "EUR")) # => true
|
|
153
|
+
# # => [DEPRECATION] warning
|
|
154
|
+
#
|
|
155
|
+
# Money.strict_eql_compare = true
|
|
156
|
+
# Money.new(0, "USD").eql?(Money.new(0, "EUR")) # => false
|
|
157
|
+
#
|
|
158
|
+
# @return [Boolean]
|
|
159
|
+
#
|
|
160
|
+
# @see Money#eql
|
|
161
|
+
attr_accessor :default_formatting_rules,
|
|
162
|
+
:default_infinite_precision,
|
|
163
|
+
:conversion_precision,
|
|
164
|
+
:strict_eql_compare
|
|
165
|
+
attr_reader :locale_backend
|
|
134
166
|
attr_writer :default_bank
|
|
135
|
-
|
|
136
|
-
def infinite_precision
|
|
137
|
-
warn '[DEPRECATION] `Money.infinite_precision` is deprecated - use `Money.default_infinite_precision` instead'
|
|
138
|
-
default_infinite_precision
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
def infinite_precision=(value)
|
|
142
|
-
warn '[DEPRECATION] `Money.infinite_precision=` is deprecated - use `Money.default_infinite_precision= ` instead'
|
|
143
|
-
self.default_infinite_precision = value
|
|
144
|
-
end
|
|
145
167
|
end
|
|
146
168
|
|
|
147
169
|
# @!attribute default_currency
|
|
148
170
|
# @return [Money::Currency] The default currency, which is used when
|
|
149
|
-
# +Money.new+ is called without an explicit currency argument.
|
|
150
|
-
# default value is Currency.new("USD"). The value must be a valid
|
|
151
|
-
# +Money::Currency+ instance.
|
|
171
|
+
# +Money.new+ is called without an explicit currency argument.
|
|
152
172
|
def self.default_currency
|
|
153
|
-
if @
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
@using_deprecated_default_currency = false
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
if @default_currency.respond_to?(:call)
|
|
173
|
+
if @default_currency.nil?
|
|
174
|
+
nil
|
|
175
|
+
elsif @default_currency.respond_to?(:call)
|
|
160
176
|
Money::Currency.new(@default_currency.call)
|
|
161
177
|
else
|
|
162
178
|
Money::Currency.new(@default_currency)
|
|
@@ -164,11 +180,14 @@ class Money
|
|
|
164
180
|
end
|
|
165
181
|
|
|
166
182
|
def self.default_currency=(currency)
|
|
167
|
-
@using_deprecated_default_currency = false
|
|
168
183
|
@default_currency = currency
|
|
169
184
|
end
|
|
170
185
|
|
|
186
|
+
# Modified to support thread-local bank override
|
|
171
187
|
def self.default_bank
|
|
188
|
+
# Check for thread-local bank first, then fall back to global default
|
|
189
|
+
return Thread.current[:money_bank] if Thread.current[:money_bank]
|
|
190
|
+
|
|
172
191
|
if @default_bank.respond_to?(:call)
|
|
173
192
|
@default_bank.call
|
|
174
193
|
else
|
|
@@ -176,49 +195,53 @@ class Money
|
|
|
176
195
|
end
|
|
177
196
|
end
|
|
178
197
|
|
|
198
|
+
# Thread-safe bank switching method
|
|
199
|
+
# Temporarily changes the default bank in the current thread only
|
|
200
|
+
#
|
|
201
|
+
# @param [Money::Bank::Base] bank The bank to use within the block
|
|
202
|
+
# @yield The block within which the bank will be changed
|
|
203
|
+
# @return [Object] block results
|
|
204
|
+
#
|
|
205
|
+
# @example
|
|
206
|
+
# Money.with_bank(european_bank) do
|
|
207
|
+
# Money.new(100, "USD").exchange_to("EUR")
|
|
208
|
+
# end
|
|
209
|
+
def self.with_bank(bank)
|
|
210
|
+
original_bank = Thread.current[:money_bank]
|
|
211
|
+
Thread.current[:money_bank] = bank
|
|
212
|
+
yield
|
|
213
|
+
ensure
|
|
214
|
+
Thread.current[:money_bank] = original_bank
|
|
215
|
+
end
|
|
216
|
+
|
|
179
217
|
def self.locale_backend=(value)
|
|
180
218
|
@locale_backend = value ? LocaleBackend.find(value) : nil
|
|
181
219
|
end
|
|
182
220
|
|
|
183
221
|
# @attr_writer rounding_mode Use this to specify the rounding mode
|
|
184
222
|
def self.rounding_mode=(new_rounding_mode)
|
|
185
|
-
@using_deprecated_default_rounding_mode = false
|
|
186
223
|
@rounding_mode = new_rounding_mode
|
|
187
224
|
end
|
|
188
225
|
|
|
189
|
-
def self.use_i18n=(value)
|
|
190
|
-
if value
|
|
191
|
-
warn '[DEPRECATION] `use_i18n` is deprecated - use `Money.locale_backend = :i18n` instead for locale based formatting'
|
|
192
|
-
else
|
|
193
|
-
warn '[DEPRECATION] `use_i18n` is deprecated - use `Money.locale_backend = :currency` instead for currency based formatting'
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
@use_i18n = value
|
|
197
|
-
end
|
|
198
|
-
|
|
199
226
|
def self.setup_defaults
|
|
200
227
|
# Set the default bank for creating new +Money+ objects.
|
|
201
228
|
self.default_bank = Bank::VariableExchange.instance
|
|
202
229
|
|
|
203
|
-
#
|
|
204
|
-
self.
|
|
205
|
-
@using_deprecated_default_currency = true
|
|
206
|
-
|
|
207
|
-
# Default to using i18n
|
|
208
|
-
@use_i18n = true
|
|
209
|
-
|
|
210
|
-
# Default to using legacy locale backend
|
|
211
|
-
self.locale_backend = :legacy
|
|
230
|
+
# Default to using currency backend
|
|
231
|
+
self.locale_backend = :currency
|
|
212
232
|
|
|
213
233
|
# Default to not using infinite precision cents
|
|
214
234
|
self.default_infinite_precision = false
|
|
215
235
|
|
|
216
|
-
# Default
|
|
217
|
-
self.rounding_mode = BigDecimal::
|
|
218
|
-
@using_deprecated_default_rounding_mode = true
|
|
236
|
+
# Default rounding mode toward the nearest neighbor; if the neighbors are equidistant, round away from zero
|
|
237
|
+
self.rounding_mode = BigDecimal::ROUND_HALF_UP
|
|
219
238
|
|
|
220
239
|
# Default the conversion of Rationals precision to 16
|
|
221
240
|
self.conversion_precision = 16
|
|
241
|
+
|
|
242
|
+
# Defaults to the deprecated behavior where
|
|
243
|
+
# `Money.new(0, "USD").eql?(Money.new(0, "EUR"))` is true.
|
|
244
|
+
self.strict_eql_compare = false
|
|
222
245
|
end
|
|
223
246
|
|
|
224
247
|
def self.inherited(base)
|
|
@@ -229,23 +252,10 @@ class Money
|
|
|
229
252
|
|
|
230
253
|
# Use this to return the rounding mode.
|
|
231
254
|
#
|
|
232
|
-
# @param [BigDecimal::ROUND_MODE] mode
|
|
233
|
-
#
|
|
234
255
|
# @return [BigDecimal::ROUND_MODE] rounding mode
|
|
235
|
-
def self.rounding_mode
|
|
236
|
-
if mode
|
|
237
|
-
warn "[DEPRECATION] calling `rounding_mode` with a block is deprecated. Please use `.with_rounding_mode` instead."
|
|
238
|
-
return with_rounding_mode(mode) { yield }
|
|
239
|
-
end
|
|
240
|
-
|
|
256
|
+
def self.rounding_mode
|
|
241
257
|
return Thread.current[:money_rounding_mode] if Thread.current[:money_rounding_mode]
|
|
242
258
|
|
|
243
|
-
if @using_deprecated_default_rounding_mode
|
|
244
|
-
warn '[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in the ' \
|
|
245
|
-
'next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.'
|
|
246
|
-
@using_deprecated_default_rounding_mode = false
|
|
247
|
-
end
|
|
248
|
-
|
|
249
259
|
@rounding_mode
|
|
250
260
|
end
|
|
251
261
|
|
|
@@ -259,14 +269,15 @@ class Money
|
|
|
259
269
|
# @return [Object] block results
|
|
260
270
|
#
|
|
261
271
|
# @example
|
|
262
|
-
# fee = Money.with_rounding_mode(BigDecimal::
|
|
272
|
+
# fee = Money.with_rounding_mode(BigDecimal::ROUND_HALF_DOWN) do
|
|
263
273
|
# Money.new(1200) * BigDecimal('0.029')
|
|
264
274
|
# end
|
|
265
275
|
def self.with_rounding_mode(mode)
|
|
276
|
+
original_mode = Thread.current[:money_rounding_mode]
|
|
266
277
|
Thread.current[:money_rounding_mode] = mode
|
|
267
278
|
yield
|
|
268
279
|
ensure
|
|
269
|
-
Thread.current[:money_rounding_mode] =
|
|
280
|
+
Thread.current[:money_rounding_mode] = original_mode
|
|
270
281
|
end
|
|
271
282
|
|
|
272
283
|
# Adds a new exchange rate to the default bank and return the rate.
|
|
@@ -308,13 +319,24 @@ class Money
|
|
|
308
319
|
raise ArgumentError, "'amount' must be numeric" unless Numeric === amount
|
|
309
320
|
|
|
310
321
|
currency = Currency.wrap(currency) || Money.default_currency
|
|
322
|
+
raise Currency::NoCurrency, 'must provide a currency' if currency.nil?
|
|
323
|
+
|
|
311
324
|
value = amount.to_d * currency.subunit_to_unit
|
|
312
325
|
new(value, currency, options)
|
|
313
326
|
end
|
|
314
327
|
|
|
328
|
+
# DEPRECATED.
|
|
329
|
+
#
|
|
330
|
+
# @see Money.from_amount
|
|
331
|
+
def self.from_dollars(amount, currency = default_currency, options = {})
|
|
332
|
+
warn "[DEPRECATION] `Money.from_dollars` is deprecated in favor of " \
|
|
333
|
+
"`Money.from_amount`."
|
|
334
|
+
|
|
335
|
+
from_amount(amount, currency, options)
|
|
336
|
+
end
|
|
337
|
+
|
|
315
338
|
class << self
|
|
316
339
|
alias_method :from_cents, :new
|
|
317
|
-
alias_method :from_dollars, :from_amount
|
|
318
340
|
end
|
|
319
341
|
|
|
320
342
|
# Creates a new Money object of value given in the
|
|
@@ -338,8 +360,8 @@ class Money
|
|
|
338
360
|
# Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
|
|
339
361
|
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
|
|
340
362
|
#
|
|
341
|
-
def initialize(
|
|
342
|
-
# For backwards
|
|
363
|
+
def initialize(obj, currency = nil, options = {})
|
|
364
|
+
# For backwards compatibility, if options is not a Hash, treat it as a bank parameter
|
|
343
365
|
unless options.is_a?(Hash)
|
|
344
366
|
options = { bank: options }
|
|
345
367
|
end
|
|
@@ -352,66 +374,32 @@ class Money
|
|
|
352
374
|
|
|
353
375
|
# BigDecimal can be Infinity and NaN, money of that amount does not make sense
|
|
354
376
|
raise ArgumentError, 'must be initialized with a finite value' unless @fractional.finite?
|
|
377
|
+
raise Currency::NoCurrency, 'must provide a currency' if @currency.nil?
|
|
355
378
|
end
|
|
356
379
|
|
|
357
|
-
#
|
|
358
|
-
# Returns the value of the money in dollars,
|
|
359
|
-
# instead of in the fractional unit cents.
|
|
360
|
-
#
|
|
361
|
-
# Synonym of #amount
|
|
362
|
-
#
|
|
363
|
-
# @return [BigDecimal]
|
|
364
|
-
#
|
|
365
|
-
# @example
|
|
366
|
-
# Money.new(1_00, "USD").dollars # => BigDecimal("1.00")
|
|
380
|
+
# DEPRECATED.
|
|
367
381
|
#
|
|
368
382
|
# @see #amount
|
|
369
|
-
# @see #to_d
|
|
370
|
-
# @see #cents
|
|
371
|
-
#
|
|
372
383
|
def dollars
|
|
384
|
+
warn "[DEPRECATION] `Money#dollars` is deprecated in favor of " \
|
|
385
|
+
"`Money#amount`."
|
|
386
|
+
|
|
373
387
|
amount
|
|
374
388
|
end
|
|
375
389
|
|
|
376
|
-
# Returns the numerical value of the money
|
|
390
|
+
# Returns the numerical value of the money.
|
|
377
391
|
#
|
|
378
392
|
# @return [BigDecimal]
|
|
379
393
|
#
|
|
380
394
|
# @example
|
|
381
|
-
# Money.new(1_00, "USD").amount
|
|
395
|
+
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
|
|
382
396
|
#
|
|
383
397
|
# @see #to_d
|
|
384
398
|
# @see #fractional
|
|
385
|
-
#
|
|
386
399
|
def amount
|
|
387
400
|
to_d
|
|
388
401
|
end
|
|
389
402
|
|
|
390
|
-
# Return string representation of currency object
|
|
391
|
-
#
|
|
392
|
-
# @return [String]
|
|
393
|
-
#
|
|
394
|
-
# @example
|
|
395
|
-
# Money.new(100, :USD).currency_as_string #=> "USD"
|
|
396
|
-
def currency_as_string
|
|
397
|
-
warn "[DEPRECATION] `currency_as_string` is deprecated. Please use `.currency.to_s` instead."
|
|
398
|
-
currency.to_s
|
|
399
|
-
end
|
|
400
|
-
|
|
401
|
-
# Set currency object using a string
|
|
402
|
-
#
|
|
403
|
-
# @param [String] val The currency string.
|
|
404
|
-
#
|
|
405
|
-
# @return [Money::Currency]
|
|
406
|
-
#
|
|
407
|
-
# @example
|
|
408
|
-
# Money.new(100).currency_as_string("CAD") #=> #<Money::Currency id: cad>
|
|
409
|
-
def currency_as_string=(val)
|
|
410
|
-
warn "[DEPRECATION] `currency_as_string=` is deprecated - Money instances are immutable." \
|
|
411
|
-
" Please use `with_currency` instead."
|
|
412
|
-
@currency = Currency.wrap(val)
|
|
413
|
-
end
|
|
414
|
-
|
|
415
403
|
# Returns a Integer hash value based on the +fractional+ and +currency+ attributes
|
|
416
404
|
# in order to use functions like & (intersection), group_by, etc.
|
|
417
405
|
#
|
|
@@ -610,9 +598,7 @@ class Money
|
|
|
610
598
|
# @example
|
|
611
599
|
# Money.new(10.1, 'USD').round #=> Money.new(10, 'USD')
|
|
612
600
|
#
|
|
613
|
-
# @see
|
|
614
|
-
# Money.default_infinite_precision
|
|
615
|
-
#
|
|
601
|
+
# @see Money.default_infinite_precision
|
|
616
602
|
def round(rounding_mode = self.class.rounding_mode, rounding_precision = 0)
|
|
617
603
|
rounded_amount = as_d(@fractional).round(rounding_precision, rounding_mode)
|
|
618
604
|
dup_with(fractional: rounded_amount)
|
data/lib/money/version.rb
CHANGED
data/lib/money.rb
CHANGED
data/money.gemspec
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
5
|
require "money/version"
|
|
@@ -14,7 +15,8 @@ Gem::Specification.new do |s|
|
|
|
14
15
|
s.description = "A Ruby Library for dealing with money and currency conversion."
|
|
15
16
|
s.license = "MIT"
|
|
16
17
|
|
|
17
|
-
s.add_dependency
|
|
18
|
+
s.add_dependency "bigdecimal"
|
|
19
|
+
s.add_dependency 'i18n', "~> 1.9"
|
|
18
20
|
|
|
19
21
|
s.add_development_dependency "bundler"
|
|
20
22
|
s.add_development_dependency "rake"
|
|
@@ -22,12 +24,15 @@ Gem::Specification.new do |s|
|
|
|
22
24
|
s.add_development_dependency "yard", "~> 0.9.11"
|
|
23
25
|
s.add_development_dependency "kramdown", "~> 2.3"
|
|
24
26
|
|
|
27
|
+
s.required_ruby_version = '>= 3.1'
|
|
28
|
+
|
|
25
29
|
s.files = `git ls-files -z -- config/* lib/* CHANGELOG.md LICENSE money.gemspec README.md`.split("\x0")
|
|
26
30
|
s.require_paths = ["lib"]
|
|
27
31
|
|
|
28
32
|
if s.respond_to?(:metadata)
|
|
29
|
-
s.metadata['changelog_uri'] = 'https://github.com/RubyMoney/money/blob/
|
|
33
|
+
s.metadata['changelog_uri'] = 'https://github.com/RubyMoney/money/blob/main/CHANGELOG.md'
|
|
30
34
|
s.metadata['source_code_uri'] = 'https://github.com/RubyMoney/money/'
|
|
31
35
|
s.metadata['bug_tracker_uri'] = 'https://github.com/RubyMoney/money/issues'
|
|
36
|
+
s.metadata['rubygems_mfa_required'] = 'true'
|
|
32
37
|
end
|
|
33
38
|
end
|
metadata
CHANGED
|
@@ -1,36 +1,43 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: money
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 7.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shane Emmons
|
|
8
8
|
- Anthony Dmitriyev
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
|
-
name:
|
|
14
|
+
name: bigdecimal
|
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
|
17
16
|
requirements:
|
|
18
17
|
- - ">="
|
|
19
18
|
- !ruby/object:Gem::Version
|
|
20
|
-
version: 0
|
|
21
|
-
- - "<="
|
|
22
|
-
- !ruby/object:Gem::Version
|
|
23
|
-
version: '2'
|
|
19
|
+
version: '0'
|
|
24
20
|
type: :runtime
|
|
25
21
|
prerelease: false
|
|
26
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
27
23
|
requirements:
|
|
28
24
|
- - ">="
|
|
29
25
|
- !ruby/object:Gem::Version
|
|
30
|
-
version: 0
|
|
31
|
-
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: i18n
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.9'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
32
39
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
40
|
+
version: '1.9'
|
|
34
41
|
- !ruby/object:Gem::Dependency
|
|
35
42
|
name: bundler
|
|
36
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -126,7 +133,6 @@ files:
|
|
|
126
133
|
- lib/money/locale_backend/currency.rb
|
|
127
134
|
- lib/money/locale_backend/errors.rb
|
|
128
135
|
- lib/money/locale_backend/i18n.rb
|
|
129
|
-
- lib/money/locale_backend/legacy.rb
|
|
130
136
|
- lib/money/money.rb
|
|
131
137
|
- lib/money/money/allocation.rb
|
|
132
138
|
- lib/money/money/arithmetic.rb
|
|
@@ -141,10 +147,10 @@ homepage: https://rubymoney.github.io/money
|
|
|
141
147
|
licenses:
|
|
142
148
|
- MIT
|
|
143
149
|
metadata:
|
|
144
|
-
changelog_uri: https://github.com/RubyMoney/money/blob/
|
|
150
|
+
changelog_uri: https://github.com/RubyMoney/money/blob/main/CHANGELOG.md
|
|
145
151
|
source_code_uri: https://github.com/RubyMoney/money/
|
|
146
152
|
bug_tracker_uri: https://github.com/RubyMoney/money/issues
|
|
147
|
-
|
|
153
|
+
rubygems_mfa_required: 'true'
|
|
148
154
|
rdoc_options: []
|
|
149
155
|
require_paths:
|
|
150
156
|
- lib
|
|
@@ -152,15 +158,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
152
158
|
requirements:
|
|
153
159
|
- - ">="
|
|
154
160
|
- !ruby/object:Gem::Version
|
|
155
|
-
version: '
|
|
161
|
+
version: '3.1'
|
|
156
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
163
|
requirements:
|
|
158
164
|
- - ">="
|
|
159
165
|
- !ruby/object:Gem::Version
|
|
160
166
|
version: '0'
|
|
161
167
|
requirements: []
|
|
162
|
-
rubygems_version: 3.
|
|
163
|
-
signing_key:
|
|
168
|
+
rubygems_version: 3.6.9
|
|
164
169
|
specification_version: 4
|
|
165
170
|
summary: A Ruby Library for dealing with money and currency conversion.
|
|
166
171
|
test_files: []
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require 'money/locale_backend/base'
|
|
2
|
-
require 'money/locale_backend/i18n'
|
|
3
|
-
|
|
4
|
-
class Money
|
|
5
|
-
module LocaleBackend
|
|
6
|
-
class Legacy < Base
|
|
7
|
-
def initialize
|
|
8
|
-
raise NotSupported, 'I18n not found' if Money.use_i18n && !defined?(::I18n)
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
def lookup(key, currency)
|
|
12
|
-
warn '[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation'
|
|
13
|
-
|
|
14
|
-
if Money.use_i18n
|
|
15
|
-
i18n_backend.lookup(key, nil) || currency.public_send(key)
|
|
16
|
-
else
|
|
17
|
-
currency.public_send(key)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
private
|
|
22
|
-
|
|
23
|
-
def i18n_backend
|
|
24
|
-
@i18n_backend ||= Money::LocaleBackend::I18n.new
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|