money 6.7.1 → 6.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -1
- data/.travis.yml +15 -6
- data/AUTHORS +5 -0
- data/CHANGELOG.md +98 -3
- data/Gemfile +13 -4
- data/LICENSE +2 -0
- data/README.md +64 -44
- data/config/currency_backwards_compatible.json +30 -0
- data/config/currency_iso.json +135 -59
- data/config/currency_non_iso.json +66 -2
- data/lib/money.rb +0 -13
- data/lib/money/bank/variable_exchange.rb +9 -22
- data/lib/money/currency.rb +33 -39
- data/lib/money/currency/heuristics.rb +1 -144
- data/lib/money/currency/loader.rb +1 -1
- data/lib/money/locale_backend/base.rb +7 -0
- data/lib/money/locale_backend/errors.rb +6 -0
- data/lib/money/locale_backend/i18n.rb +24 -0
- data/lib/money/locale_backend/legacy.rb +28 -0
- data/lib/money/money.rb +106 -139
- data/lib/money/money/allocation.rb +37 -0
- data/lib/money/money/arithmetic.rb +31 -28
- data/lib/money/money/constructors.rb +1 -2
- data/lib/money/money/formatter.rb +397 -0
- data/lib/money/money/formatting_rules.rb +120 -0
- data/lib/money/money/locale_backend.rb +20 -0
- data/lib/money/rates_store/memory.rb +1 -2
- data/lib/money/version.rb +1 -1
- data/money.gemspec +10 -16
- data/spec/bank/variable_exchange_spec.rb +7 -3
- data/spec/currency/heuristics_spec.rb +2 -153
- data/spec/currency_spec.rb +44 -3
- data/spec/locale_backend/i18n_spec.rb +62 -0
- data/spec/locale_backend/legacy_spec.rb +74 -0
- data/spec/money/allocation_spec.rb +130 -0
- data/spec/money/arithmetic_spec.rb +184 -90
- data/spec/money/constructors_spec.rb +0 -12
- data/spec/money/formatting_spec.rb +296 -179
- data/spec/money/locale_backend_spec.rb +14 -0
- data/spec/money_spec.rb +159 -26
- data/spec/rates_store/memory_spec.rb +13 -2
- data/spec/spec_helper.rb +2 -0
- data/spec/support/shared_examples/money_examples.rb +14 -0
- metadata +32 -40
- data/lib/money/money/formatting.rb +0 -418
@@ -0,0 +1,397 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'money/money/formatting_rules'
|
3
|
+
|
4
|
+
class Money
|
5
|
+
class Formatter
|
6
|
+
DEFAULTS = {
|
7
|
+
thousands_separator: '',
|
8
|
+
decimal_mark: '.'
|
9
|
+
}.freeze
|
10
|
+
|
11
|
+
# Creates a formatted price string according to several rules.
|
12
|
+
#
|
13
|
+
# @param [Hash] rules The options used to format the string.
|
14
|
+
#
|
15
|
+
# @return [String]
|
16
|
+
#
|
17
|
+
# @option rules [Boolean, String] :display_free (false) Whether a zero
|
18
|
+
# amount of money should be formatted of "free" or as the supplied string.
|
19
|
+
#
|
20
|
+
# @example
|
21
|
+
# Money.us_dollar(0).format(display_free: true) #=> "free"
|
22
|
+
# Money.us_dollar(0).format(display_free: "gratis") #=> "gratis"
|
23
|
+
# Money.us_dollar(0).format #=> "$0.00"
|
24
|
+
#
|
25
|
+
# @option rules [Boolean] :with_currency (false) Whether the currency name
|
26
|
+
# should be appended to the result string.
|
27
|
+
#
|
28
|
+
# @example
|
29
|
+
# Money.ca_dollar(100).format #=> "$1.00"
|
30
|
+
# Money.ca_dollar(100).format(with_currency: true) #=> "$1.00 CAD"
|
31
|
+
# Money.us_dollar(85).format(with_currency: true) #=> "$0.85 USD"
|
32
|
+
#
|
33
|
+
# @option rules [Boolean] :rounded_infinite_precision (false) Whether the
|
34
|
+
# amount of money should be rounded when using {infinite_precision}
|
35
|
+
#
|
36
|
+
# @example
|
37
|
+
# Money.us_dollar(100.1).format #=> "$1.001"
|
38
|
+
# Money.us_dollar(100.1).format(rounded_infinite_precision: true) #=> "$1"
|
39
|
+
# Money.us_dollar(100.9).format(rounded_infinite_precision: true) #=> "$1.01"
|
40
|
+
#
|
41
|
+
# @option rules [Boolean] :no_cents (false) Whether cents should be omitted.
|
42
|
+
#
|
43
|
+
# @example
|
44
|
+
# Money.ca_dollar(100).format(no_cents: true) #=> "$1"
|
45
|
+
# Money.ca_dollar(599).format(no_cents: true) #=> "$5"
|
46
|
+
#
|
47
|
+
# @option rules [Boolean] :no_cents_if_whole (false) Whether cents should be
|
48
|
+
# omitted if the cent value is zero
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
# Money.ca_dollar(10000).format(no_cents_if_whole: true) #=> "$100"
|
52
|
+
# Money.ca_dollar(10034).format(no_cents_if_whole: true) #=> "$100.34"
|
53
|
+
#
|
54
|
+
# @option rules [Boolean, String, nil] :symbol (true) Whether a money symbol
|
55
|
+
# should be prepended to the result string. The default is true. This method
|
56
|
+
# attempts to pick a symbol that's suitable for the given currency.
|
57
|
+
#
|
58
|
+
# @example
|
59
|
+
# Money.new(100, "USD") #=> "$1.00"
|
60
|
+
# Money.new(100, "GBP") #=> "£1.00"
|
61
|
+
# Money.new(100, "EUR") #=> "€1.00"
|
62
|
+
#
|
63
|
+
# # Same thing.
|
64
|
+
# Money.new(100, "USD").format(symbol: true) #=> "$1.00"
|
65
|
+
# Money.new(100, "GBP").format(symbol: true) #=> "£1.00"
|
66
|
+
# Money.new(100, "EUR").format(symbol: true) #=> "€1.00"
|
67
|
+
#
|
68
|
+
# # You can specify a false expression or an empty string to disable
|
69
|
+
# # prepending a money symbol.§
|
70
|
+
# Money.new(100, "USD").format(symbol: false) #=> "1.00"
|
71
|
+
# Money.new(100, "GBP").format(symbol: nil) #=> "1.00"
|
72
|
+
# Money.new(100, "EUR").format(symbol: "") #=> "1.00"
|
73
|
+
#
|
74
|
+
# # If the symbol for the given currency isn't known, then it will default
|
75
|
+
# # to "¤" as symbol.
|
76
|
+
# Money.new(100, "AWG").format(symbol: true) #=> "¤1.00"
|
77
|
+
#
|
78
|
+
# # You can specify a string as value to enforce using a particular symbol.
|
79
|
+
# Money.new(100, "AWG").format(symbol: "ƒ") #=> "ƒ1.00"
|
80
|
+
#
|
81
|
+
# # You can specify a indian currency format
|
82
|
+
# Money.new(10000000, "INR").format(south_asian_number_formatting: true) #=> "1,00,000.00"
|
83
|
+
# Money.new(10000000).format(south_asian_number_formatting: true) #=> "$1,00,000.00"
|
84
|
+
#
|
85
|
+
# @option rules [Boolean, nil] :symbol_before_without_space (true) Whether
|
86
|
+
# a space between the money symbol and the amount should be inserted when
|
87
|
+
# +:symbol_position+ is +:before+. The default is true (meaning no space). Ignored
|
88
|
+
# if +:symbol+ is false or +:symbol_position+ is not +:before+.
|
89
|
+
#
|
90
|
+
# @example
|
91
|
+
# # Default is to not insert a space.
|
92
|
+
# Money.new(100, "USD").format #=> "$1.00"
|
93
|
+
#
|
94
|
+
# # Same thing.
|
95
|
+
# Money.new(100, "USD").format(symbol_before_without_space: true) #=> "$1.00"
|
96
|
+
#
|
97
|
+
# # If set to false, will insert a space.
|
98
|
+
# Money.new(100, "USD").format(symbol_before_without_space: false) #=> "$ 1.00"
|
99
|
+
#
|
100
|
+
# @option rules [Boolean, nil] :symbol_after_without_space (false) Whether
|
101
|
+
# a space between the amount and the money symbol should be inserted when
|
102
|
+
# +:symbol_position+ is +:after+. The default is false (meaning space). Ignored
|
103
|
+
# if +:symbol+ is false or +:symbol_position+ is not +:after+.
|
104
|
+
#
|
105
|
+
# @example
|
106
|
+
# # Default is to insert a space.
|
107
|
+
# Money.new(100, "USD").format(symbol_position: :after) #=> "1.00 $"
|
108
|
+
#
|
109
|
+
# # If set to true, will not insert a space.
|
110
|
+
# Money.new(100, "USD").format(symbol_position: :after, symbol_after_without_space: true) #=> "1.00$"
|
111
|
+
#
|
112
|
+
# @option rules [Boolean, String, nil] :decimal_mark (true) Whether the
|
113
|
+
# currency should be separated by the specified character or '.'
|
114
|
+
#
|
115
|
+
# @example
|
116
|
+
# # If a string is specified, it's value is used.
|
117
|
+
# Money.new(100, "USD").format(decimal_mark: ",") #=> "$1,00"
|
118
|
+
#
|
119
|
+
# # If the decimal_mark for a given currency isn't known, then it will default
|
120
|
+
# # to "." as decimal_mark.
|
121
|
+
# Money.new(100, "FOO").format #=> "$1.00"
|
122
|
+
#
|
123
|
+
# @option rules [Boolean, String, nil] :thousands_separator (true) Whether
|
124
|
+
# the currency should be delimited by the specified character or ','
|
125
|
+
#
|
126
|
+
# @example
|
127
|
+
# # If false is specified, no thousands_separator is used.
|
128
|
+
# Money.new(100000, "USD").format(thousands_separator: false) #=> "1000.00"
|
129
|
+
# Money.new(100000, "USD").format(thousands_separator: nil) #=> "1000.00"
|
130
|
+
# Money.new(100000, "USD").format(thousands_separator: "") #=> "1000.00"
|
131
|
+
#
|
132
|
+
# # If a string is specified, it's value is used.
|
133
|
+
# Money.new(100000, "USD").format(thousands_separator: ".") #=> "$1.000.00"
|
134
|
+
#
|
135
|
+
# # If the thousands_separator for a given currency isn't known, then it will
|
136
|
+
# # default to "," as thousands_separator.
|
137
|
+
# Money.new(100000, "FOO").format #=> "$1,000.00"
|
138
|
+
#
|
139
|
+
# @option rules [Boolean] :html (false) Whether the currency should be
|
140
|
+
# HTML-formatted. Only useful in combination with +:with_currency+.
|
141
|
+
#
|
142
|
+
# @example
|
143
|
+
# Money.ca_dollar(570).format(html: true, with_currency: true)
|
144
|
+
# #=> "$5.70 <span class=\"currency\">CAD</span>"
|
145
|
+
#
|
146
|
+
# @option rules [Boolean] :html_wrap (false) Whether all currency parts should be HTML-formatted.
|
147
|
+
#
|
148
|
+
# @example
|
149
|
+
# Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
|
150
|
+
# #=> "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
|
151
|
+
#
|
152
|
+
# @option rules [Boolean] :sign_before_symbol (false) Whether the sign should be
|
153
|
+
# before the currency symbol.
|
154
|
+
#
|
155
|
+
# @example
|
156
|
+
# # You can specify to display the sign before the symbol for negative numbers
|
157
|
+
# Money.new(-100, "GBP").format(sign_before_symbol: true) #=> "-£1.00"
|
158
|
+
# Money.new(-100, "GBP").format(sign_before_symbol: false) #=> "£-1.00"
|
159
|
+
# Money.new(-100, "GBP").format #=> "£-1.00"
|
160
|
+
#
|
161
|
+
# @option rules [Boolean] :sign_positive (false) Whether positive numbers should be
|
162
|
+
# signed, too.
|
163
|
+
#
|
164
|
+
# @example
|
165
|
+
# # You can specify to display the sign with positive numbers
|
166
|
+
# Money.new(100, "GBP").format(sign_positive: true, sign_before_symbol: true) #=> "+£1.00"
|
167
|
+
# Money.new(100, "GBP").format(sign_positive: true, sign_before_symbol: false) #=> "£+1.00"
|
168
|
+
# Money.new(100, "GBP").format(sign_positive: false, sign_before_symbol: true) #=> "£1.00"
|
169
|
+
# Money.new(100, "GBP").format(sign_positive: false, sign_before_symbol: false) #=> "£1.00"
|
170
|
+
# Money.new(100, "GBP").format #=> "£+1.00"
|
171
|
+
#
|
172
|
+
# @option rules [Boolean] :disambiguate (false) Prevents the result from being ambiguous
|
173
|
+
# due to equal symbols for different currencies. Uses the `disambiguate_symbol`.
|
174
|
+
#
|
175
|
+
# @example
|
176
|
+
# Money.new(10000, "USD").format(disambiguate: false) #=> "$100.00"
|
177
|
+
# Money.new(10000, "CAD").format(disambiguate: false) #=> "$100.00"
|
178
|
+
# Money.new(10000, "USD").format(disambiguate: true) #=> "$100.00"
|
179
|
+
# Money.new(10000, "CAD").format(disambiguate: true) #=> "C$100.00"
|
180
|
+
#
|
181
|
+
# @option rules [Boolean] :html_wrap_symbol (false) Wraps the currency symbol
|
182
|
+
# in a html <span> tag.
|
183
|
+
#
|
184
|
+
# @example
|
185
|
+
# Money.new(10000, "USD").format(disambiguate: false)
|
186
|
+
# #=> "<span class=\"currency_symbol\">$100.00</span>
|
187
|
+
#
|
188
|
+
# @option rules [Symbol] :symbol_position (:before) `:before` if the currency
|
189
|
+
# symbol goes before the amount, `:after` if it goes after.
|
190
|
+
#
|
191
|
+
# @example
|
192
|
+
# Money.new(10000, "USD").format(symbol_position: :before) #=> "$100.00"
|
193
|
+
# Money.new(10000, "USD").format(symbol_position: :after) #=> "100.00 $"
|
194
|
+
#
|
195
|
+
# @option rules [Boolean] :translate (true) `true` Checks for custom
|
196
|
+
# symbol definitions using I18n.
|
197
|
+
#
|
198
|
+
# @example
|
199
|
+
# # With the following entry in the translation files:
|
200
|
+
# # en:
|
201
|
+
# # number:
|
202
|
+
# # currency:
|
203
|
+
# # symbol:
|
204
|
+
# # CAD: "CAD$"
|
205
|
+
# Money.new(10000, "CAD").format(translate: true) #=> "CAD$100.00"
|
206
|
+
#
|
207
|
+
# @example
|
208
|
+
# Money.new(89000, :btc).format(drop_trailing_zeros: true) #=> B⃦0.00089
|
209
|
+
# Money.new(110, :usd).format(drop_trailing_zeros: true) #=> $1.1
|
210
|
+
#
|
211
|
+
# @option rules [String] :format (nil) Provide a template for formatting. `%u` will be replaced
|
212
|
+
# with the symbol (if present) and `%n` will be replaced with the number.
|
213
|
+
#
|
214
|
+
# @example
|
215
|
+
# Money.new(10000, "USD").format(format: '%u %n') #=> "$ 100.00"
|
216
|
+
# Money.new(10000, "USD").format(format: '<span>%u%n</span>') #=> "<span>$100.00</span>"
|
217
|
+
#
|
218
|
+
# Note that the default rules can be defined through {Money.default_formatting_rules} hash.
|
219
|
+
#
|
220
|
+
# @see Money.default_formatting_rules Money.default_formatting_rules for more information.
|
221
|
+
def initialize(money, *rules)
|
222
|
+
@money = money
|
223
|
+
@currency = money.currency
|
224
|
+
@rules = FormattingRules.new(@currency, *rules)
|
225
|
+
end
|
226
|
+
|
227
|
+
def to_s
|
228
|
+
return free_text if show_free_text?
|
229
|
+
result = format_number
|
230
|
+
formatted = append_sign(result)
|
231
|
+
append_currency_symbol(formatted)
|
232
|
+
end
|
233
|
+
|
234
|
+
def thousands_separator
|
235
|
+
lookup :thousands_separator
|
236
|
+
end
|
237
|
+
|
238
|
+
def decimal_mark
|
239
|
+
lookup :decimal_mark
|
240
|
+
end
|
241
|
+
|
242
|
+
alias_method :delimiter, :thousands_separator
|
243
|
+
alias_method :separator, :decimal_mark
|
244
|
+
|
245
|
+
private
|
246
|
+
|
247
|
+
attr_reader :money, :currency, :rules
|
248
|
+
|
249
|
+
def format_number
|
250
|
+
whole_part, decimal_part = extract_whole_and_decimal_parts
|
251
|
+
|
252
|
+
# Format whole and decimal parts separately
|
253
|
+
decimal_part = format_decimal_part(decimal_part)
|
254
|
+
whole_part = format_whole_part(whole_part)
|
255
|
+
|
256
|
+
# Assemble the final formatted amount
|
257
|
+
if rules[:html_wrap]
|
258
|
+
if decimal_part.nil?
|
259
|
+
html_wrap(whole_part, "whole")
|
260
|
+
else
|
261
|
+
[
|
262
|
+
html_wrap(whole_part, "whole"),
|
263
|
+
html_wrap(decimal_mark, "decimal-mark"),
|
264
|
+
html_wrap(decimal_part, "decimal")
|
265
|
+
].join
|
266
|
+
end
|
267
|
+
else
|
268
|
+
[whole_part, decimal_part].compact.join(decimal_mark)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
def append_sign(formatted_number)
|
273
|
+
sign = money.negative? ? '-' : ''
|
274
|
+
|
275
|
+
if rules[:sign_positive] == true && money.positive?
|
276
|
+
sign = '+'
|
277
|
+
end
|
278
|
+
|
279
|
+
if rules[:sign_before_symbol] == true
|
280
|
+
sign_before = sign
|
281
|
+
sign = ''
|
282
|
+
end
|
283
|
+
|
284
|
+
symbol_value = symbol_value_from(rules)
|
285
|
+
|
286
|
+
if symbol_value && !symbol_value.empty?
|
287
|
+
if rules[:html_wrap_symbol]
|
288
|
+
symbol_value = "<span class=\"currency_symbol\">#{symbol_value}</span>"
|
289
|
+
elsif rules[:html_wrap]
|
290
|
+
symbol_value = html_wrap(symbol_value, "currency-symbol")
|
291
|
+
end
|
292
|
+
|
293
|
+
rules[:format]
|
294
|
+
.gsub('%u', [sign_before, symbol_value].join)
|
295
|
+
.gsub('%n', [sign, formatted_number].join)
|
296
|
+
else
|
297
|
+
formatted_number = "#{sign_before}#{sign}#{formatted_number}"
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
def append_currency_symbol(formatted_number)
|
302
|
+
if rules[:with_currency]
|
303
|
+
formatted_number << " "
|
304
|
+
|
305
|
+
if rules[:html]
|
306
|
+
formatted_number << "<span class=\"currency\">#{currency.to_s}</span>"
|
307
|
+
elsif rules[:html_wrap]
|
308
|
+
formatted_number << html_wrap(currency.to_s, "currency")
|
309
|
+
else
|
310
|
+
formatted_number << currency.to_s
|
311
|
+
end
|
312
|
+
end
|
313
|
+
formatted_number
|
314
|
+
end
|
315
|
+
|
316
|
+
def show_free_text?
|
317
|
+
money.zero? && rules[:display_free]
|
318
|
+
end
|
319
|
+
|
320
|
+
def html_wrap(string, class_name)
|
321
|
+
"<span class=\"money-#{class_name}\">#{string}</span>"
|
322
|
+
end
|
323
|
+
|
324
|
+
def free_text
|
325
|
+
rules[:display_free].respond_to?(:to_str) ? rules[:display_free] : 'free'
|
326
|
+
end
|
327
|
+
|
328
|
+
def format_whole_part(value)
|
329
|
+
# Apply thousands_separator
|
330
|
+
value.gsub regexp_format, "\\1#{thousands_separator}"
|
331
|
+
end
|
332
|
+
|
333
|
+
def extract_whole_and_decimal_parts
|
334
|
+
fractional = money.fractional.abs
|
335
|
+
|
336
|
+
# Round the infinite precision part if needed
|
337
|
+
fractional = fractional.round if rules[:rounded_infinite_precision]
|
338
|
+
|
339
|
+
# Translate subunits into units
|
340
|
+
fractional_units = BigDecimal(fractional) / currency.subunit_to_unit
|
341
|
+
|
342
|
+
# Split the result and return whole and decimal parts separately
|
343
|
+
fractional_units.to_s('F').split('.')
|
344
|
+
end
|
345
|
+
|
346
|
+
def format_decimal_part(value)
|
347
|
+
return nil if currency.decimal_places == 0 && !Money.infinite_precision
|
348
|
+
return nil if rules[:no_cents]
|
349
|
+
return nil if rules[:no_cents_if_whole] && value.to_i == 0
|
350
|
+
|
351
|
+
# Pad value, making up for missing zeroes at the end
|
352
|
+
value = value.ljust(currency.decimal_places, '0')
|
353
|
+
|
354
|
+
# Drop trailing zeros if needed
|
355
|
+
value.gsub!(/0*$/, '') if rules[:drop_trailing_zeros]
|
356
|
+
|
357
|
+
value.empty? ? nil : value
|
358
|
+
end
|
359
|
+
|
360
|
+
def lookup(key)
|
361
|
+
return rules[key] || DEFAULTS[key] if rules.has_key?(key)
|
362
|
+
|
363
|
+
(Money.locale_backend && Money.locale_backend.lookup(key, currency)) || DEFAULTS[key]
|
364
|
+
end
|
365
|
+
|
366
|
+
def regexp_format
|
367
|
+
if rules[:south_asian_number_formatting]
|
368
|
+
# from http://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
|
369
|
+
/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
|
370
|
+
else
|
371
|
+
/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
def symbol_value_from(rules)
|
376
|
+
if rules.has_key?(:symbol)
|
377
|
+
if rules[:symbol] === true
|
378
|
+
if rules[:disambiguate] && currency.disambiguate_symbol
|
379
|
+
currency.disambiguate_symbol
|
380
|
+
else
|
381
|
+
money.symbol
|
382
|
+
end
|
383
|
+
elsif rules[:symbol]
|
384
|
+
rules[:symbol]
|
385
|
+
else
|
386
|
+
""
|
387
|
+
end
|
388
|
+
elsif rules[:html] || rules[:html_wrap]
|
389
|
+
currency.html_entity == '' ? currency.symbol : currency.html_entity
|
390
|
+
elsif rules[:disambiguate] && currency.disambiguate_symbol
|
391
|
+
currency.disambiguate_symbol
|
392
|
+
else
|
393
|
+
money.symbol
|
394
|
+
end
|
395
|
+
end
|
396
|
+
end
|
397
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class Money
|
4
|
+
class FormattingRules
|
5
|
+
def initialize(currency, *raw_rules)
|
6
|
+
@currency = currency
|
7
|
+
|
8
|
+
# support for old format parameters
|
9
|
+
@rules = normalize_formatting_rules(raw_rules)
|
10
|
+
|
11
|
+
@rules = default_formatting_rules.merge(@rules) unless @rules[:ignore_defaults]
|
12
|
+
@rules = localize_formatting_rules(@rules)
|
13
|
+
@rules = translate_formatting_rules(@rules) if @rules[:translate]
|
14
|
+
@rules[:format] ||= determine_format_from_formatting_rules(@rules)
|
15
|
+
|
16
|
+
warn_about_deprecated_rules(@rules)
|
17
|
+
end
|
18
|
+
|
19
|
+
def [](key)
|
20
|
+
@rules[key]
|
21
|
+
end
|
22
|
+
|
23
|
+
def has_key?(key)
|
24
|
+
@rules.has_key? key
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
attr_reader :currency
|
30
|
+
|
31
|
+
# Cleans up formatting rules.
|
32
|
+
#
|
33
|
+
# @param [Hash] rules
|
34
|
+
#
|
35
|
+
# @return [Hash]
|
36
|
+
def normalize_formatting_rules(rules)
|
37
|
+
if rules.size == 0
|
38
|
+
rules = {}
|
39
|
+
elsif rules.size == 1
|
40
|
+
rules = rules.pop
|
41
|
+
rules = { rules => true } if rules.is_a?(Symbol)
|
42
|
+
end
|
43
|
+
if !rules.include?(:decimal_mark) && rules.include?(:separator)
|
44
|
+
rules[:decimal_mark] = rules[:separator]
|
45
|
+
end
|
46
|
+
if !rules.include?(:thousands_separator) && rules.include?(:delimiter)
|
47
|
+
rules[:thousands_separator] = rules[:delimiter]
|
48
|
+
end
|
49
|
+
rules
|
50
|
+
end
|
51
|
+
|
52
|
+
def default_formatting_rules
|
53
|
+
Money.default_formatting_rules || {}
|
54
|
+
end
|
55
|
+
|
56
|
+
def translate_formatting_rules(rules)
|
57
|
+
begin
|
58
|
+
rules[:symbol] = I18n.t currency.iso_code, scope: "number.currency.symbol", raise: true
|
59
|
+
rescue I18n::MissingTranslationData
|
60
|
+
# Do nothing
|
61
|
+
end
|
62
|
+
rules
|
63
|
+
end
|
64
|
+
|
65
|
+
def localize_formatting_rules(rules)
|
66
|
+
if currency.iso_code == "JPY" && I18n.locale == :ja
|
67
|
+
rules[:symbol] = "円" unless rules[:symbol] == false
|
68
|
+
rules[:format] = '%n%u'
|
69
|
+
end
|
70
|
+
rules
|
71
|
+
end
|
72
|
+
|
73
|
+
def determine_format_from_formatting_rules(rules)
|
74
|
+
symbol_position = symbol_position_from(rules)
|
75
|
+
|
76
|
+
if symbol_position == :before
|
77
|
+
rules.fetch(:symbol_before_without_space, true) ? '%u%n' : '%u %n'
|
78
|
+
else
|
79
|
+
rules[:symbol_after_without_space] ? '%n%u' : '%n %u'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def symbol_position_from(rules)
|
84
|
+
if rules.has_key?(:symbol_position)
|
85
|
+
if [:before, :after].include?(rules[:symbol_position])
|
86
|
+
return rules[:symbol_position]
|
87
|
+
else
|
88
|
+
raise ArgumentError, ":symbol_position must be ':before' or ':after'"
|
89
|
+
end
|
90
|
+
elsif currency.symbol_first?
|
91
|
+
:before
|
92
|
+
else
|
93
|
+
:after
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def warn_about_deprecated_rules(rules)
|
98
|
+
if rules.has_key?(:symbol_position)
|
99
|
+
warn '[DEPRECATION] `symbol_position:` option is deprecated - use `format` to specify the formatting template.'
|
100
|
+
end
|
101
|
+
|
102
|
+
if rules.has_key?(:symbol_before_without_space)
|
103
|
+
warn '[DEPRECATION] `symbol_before_without_space:` option is deprecated - use `format` to specify the formatting template.'
|
104
|
+
end
|
105
|
+
|
106
|
+
if rules.has_key?(:symbol_after_without_space)
|
107
|
+
warn '[DEPRECATION] `symbol_after_without_space:` option is deprecated - use `format` to specify the formatting template.'
|
108
|
+
end
|
109
|
+
|
110
|
+
if rules.has_key?(:html)
|
111
|
+
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`."
|
112
|
+
end
|
113
|
+
|
114
|
+
if rules.has_key?(:html_wrap_symbol)
|
115
|
+
warn "[DEPRECATION] `html_wrap_symbol` is deprecated - use `html_wrap` instead. Please note that `html_wrap` will wrap all parts of currency."
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|