money 6.5.1 → 6.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +209 -5
- data/LICENSE +18 -16
- data/README.md +321 -70
- data/config/currency_backwards_compatible.json +65 -0
- data/config/currency_iso.json +280 -94
- data/config/currency_non_iso.json +101 -3
- data/lib/money/bank/base.rb +1 -3
- data/lib/money/bank/variable_exchange.rb +88 -96
- data/lib/money/currency/heuristics.rb +1 -143
- data/lib/money/currency/loader.rb +15 -13
- data/lib/money/currency.rb +98 -81
- data/lib/money/locale_backend/base.rb +7 -0
- data/lib/money/locale_backend/currency.rb +11 -0
- data/lib/money/locale_backend/errors.rb +6 -0
- data/lib/money/locale_backend/i18n.rb +25 -0
- data/lib/money/locale_backend/legacy.rb +28 -0
- data/lib/money/money/allocation.rb +46 -0
- data/lib/money/money/arithmetic.rb +97 -52
- data/lib/money/money/constructors.rb +5 -6
- data/lib/money/money/formatter.rb +399 -0
- data/lib/money/money/formatting_rules.rb +142 -0
- data/lib/money/money/locale_backend.rb +22 -0
- data/lib/money/money.rb +268 -194
- data/lib/money/rates_store/memory.rb +120 -0
- data/lib/money/version.rb +1 -1
- data/money.gemspec +15 -20
- metadata +36 -59
- data/.coveralls.yml +0 -1
- data/.gitignore +0 -22
- data/.travis.yml +0 -13
- data/AUTHORS +0 -116
- data/CONTRIBUTING.md +0 -17
- data/Gemfile +0 -7
- data/Rakefile +0 -17
- data/lib/money/money/formatting.rb +0 -386
- data/spec/bank/base_spec.rb +0 -77
- data/spec/bank/single_currency_spec.rb +0 -11
- data/spec/bank/variable_exchange_spec.rb +0 -275
- data/spec/currency/heuristics_spec.rb +0 -84
- data/spec/currency_spec.rb +0 -321
- data/spec/money/arithmetic_spec.rb +0 -568
- data/spec/money/constructors_spec.rb +0 -75
- data/spec/money/formatting_spec.rb +0 -667
- data/spec/money_spec.rb +0 -745
- data/spec/spec_helper.rb +0 -23
@@ -0,0 +1,399 @@
|
|
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
|
+
# @option rules [Boolean] :drop_trailing_zeros (false) Ignore trailing zeros after
|
208
|
+
# the decimal mark
|
209
|
+
#
|
210
|
+
# @example
|
211
|
+
# Money.new(89000, :btc).format(drop_trailing_zeros: true) #=> B⃦0.00089
|
212
|
+
# Money.new(110, :usd).format(drop_trailing_zeros: true) #=> $1.1
|
213
|
+
#
|
214
|
+
# @option rules [Boolean] :delimiter_pattern (/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/) Regular expression to set the placement
|
215
|
+
# for the thousands delimiter
|
216
|
+
#
|
217
|
+
# @example
|
218
|
+
# Money.new(89000, :btc).format(delimiter_pattern: /(\d)(?=\d)/) #=> B⃦8,9,0.00
|
219
|
+
#
|
220
|
+
# @option rules [String] :format (nil) Provide a template for formatting. `%u` will be replaced
|
221
|
+
# with the symbol (if present) and `%n` will be replaced with the number.
|
222
|
+
#
|
223
|
+
# @example
|
224
|
+
# Money.new(10000, "USD").format(format: '%u %n') #=> "$ 100.00"
|
225
|
+
# Money.new(10000, "USD").format(format: '<span>%u%n</span>') #=> "<span>$100.00</span>"
|
226
|
+
#
|
227
|
+
# Note that the default rules can be defined through {Money.default_formatting_rules} hash.
|
228
|
+
#
|
229
|
+
# @see Money.default_formatting_rules Money.default_formatting_rules for more information.
|
230
|
+
def initialize(money, *rules)
|
231
|
+
@money = money
|
232
|
+
@currency = money.currency
|
233
|
+
@rules = FormattingRules.new(@currency, *rules)
|
234
|
+
end
|
235
|
+
|
236
|
+
def to_s
|
237
|
+
return free_text if show_free_text?
|
238
|
+
result = format_number
|
239
|
+
formatted = append_sign(result)
|
240
|
+
append_currency_symbol(formatted)
|
241
|
+
end
|
242
|
+
|
243
|
+
def thousands_separator
|
244
|
+
lookup :thousands_separator
|
245
|
+
end
|
246
|
+
|
247
|
+
def decimal_mark
|
248
|
+
lookup :decimal_mark
|
249
|
+
end
|
250
|
+
|
251
|
+
alias_method :delimiter, :thousands_separator
|
252
|
+
alias_method :separator, :decimal_mark
|
253
|
+
|
254
|
+
private
|
255
|
+
|
256
|
+
attr_reader :money, :currency, :rules
|
257
|
+
|
258
|
+
def format_number
|
259
|
+
whole_part, decimal_part = extract_whole_and_decimal_parts
|
260
|
+
|
261
|
+
# Format whole and decimal parts separately
|
262
|
+
decimal_part = format_decimal_part(decimal_part)
|
263
|
+
whole_part = format_whole_part(whole_part)
|
264
|
+
|
265
|
+
# Assemble the final formatted amount
|
266
|
+
if rules[:html_wrap]
|
267
|
+
if decimal_part.nil?
|
268
|
+
html_wrap(whole_part, "whole")
|
269
|
+
else
|
270
|
+
[
|
271
|
+
html_wrap(whole_part, "whole"),
|
272
|
+
html_wrap(decimal_mark, "decimal-mark"),
|
273
|
+
html_wrap(decimal_part, "decimal")
|
274
|
+
].join
|
275
|
+
end
|
276
|
+
else
|
277
|
+
[whole_part, decimal_part].compact.join(decimal_mark)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
def append_sign(formatted_number)
|
282
|
+
sign = money.negative? ? '-' : ''
|
283
|
+
|
284
|
+
if rules[:sign_positive] == true && money.positive?
|
285
|
+
sign = '+'
|
286
|
+
end
|
287
|
+
|
288
|
+
if rules[:sign_before_symbol] == true
|
289
|
+
sign_before = sign
|
290
|
+
sign = ''
|
291
|
+
end
|
292
|
+
|
293
|
+
symbol_value = symbol_value_from(rules)
|
294
|
+
|
295
|
+
if symbol_value && !symbol_value.empty?
|
296
|
+
if rules[:html_wrap_symbol]
|
297
|
+
symbol_value = "<span class=\"currency_symbol\">#{symbol_value}</span>"
|
298
|
+
elsif rules[:html_wrap]
|
299
|
+
symbol_value = html_wrap(symbol_value, "currency-symbol")
|
300
|
+
end
|
301
|
+
|
302
|
+
rules[:format]
|
303
|
+
.gsub('%u', [sign_before, symbol_value].join)
|
304
|
+
.gsub('%n', [sign, formatted_number].join)
|
305
|
+
else
|
306
|
+
formatted_number = "#{sign_before}#{sign}#{formatted_number}"
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
def append_currency_symbol(formatted_number)
|
311
|
+
if rules[:with_currency]
|
312
|
+
formatted_number << " "
|
313
|
+
|
314
|
+
if rules[:html]
|
315
|
+
formatted_number << "<span class=\"currency\">#{currency.to_s}</span>"
|
316
|
+
elsif rules[:html_wrap]
|
317
|
+
formatted_number << html_wrap(currency.to_s, "currency")
|
318
|
+
else
|
319
|
+
formatted_number << currency.to_s
|
320
|
+
end
|
321
|
+
end
|
322
|
+
formatted_number
|
323
|
+
end
|
324
|
+
|
325
|
+
def show_free_text?
|
326
|
+
money.zero? && rules[:display_free]
|
327
|
+
end
|
328
|
+
|
329
|
+
def html_wrap(string, class_name)
|
330
|
+
"<span class=\"money-#{class_name}\">#{string}</span>"
|
331
|
+
end
|
332
|
+
|
333
|
+
def free_text
|
334
|
+
rules[:display_free].respond_to?(:to_str) ? rules[:display_free] : 'free'
|
335
|
+
end
|
336
|
+
|
337
|
+
def format_whole_part(value)
|
338
|
+
# Apply thousands_separator
|
339
|
+
value.gsub(rules[:delimiter_pattern]) do |digit_to_delimit|
|
340
|
+
"#{digit_to_delimit}#{thousands_separator}"
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
def extract_whole_and_decimal_parts
|
345
|
+
fractional = money.fractional.abs
|
346
|
+
|
347
|
+
# Round the infinite precision part if needed
|
348
|
+
fractional = fractional.round if rules[:rounded_infinite_precision]
|
349
|
+
|
350
|
+
# Translate subunits into units
|
351
|
+
fractional_units = BigDecimal(fractional) / currency.subunit_to_unit
|
352
|
+
|
353
|
+
# Split the result and return whole and decimal parts separately
|
354
|
+
fractional_units.to_s('F').split('.')
|
355
|
+
end
|
356
|
+
|
357
|
+
def format_decimal_part(value)
|
358
|
+
return nil if currency.decimal_places == 0 && !Money.default_infinite_precision
|
359
|
+
return nil if rules[:no_cents]
|
360
|
+
return nil if rules[:no_cents_if_whole] && value.to_i == 0
|
361
|
+
|
362
|
+
# Pad value, making up for missing zeroes at the end
|
363
|
+
value = value.ljust(currency.decimal_places, '0')
|
364
|
+
|
365
|
+
# Drop trailing zeros if needed
|
366
|
+
value.gsub!(/0*$/, '') if rules[:drop_trailing_zeros]
|
367
|
+
|
368
|
+
value.empty? ? nil : value
|
369
|
+
end
|
370
|
+
|
371
|
+
def lookup(key)
|
372
|
+
return rules[key] || DEFAULTS[key] if rules.has_key?(key)
|
373
|
+
|
374
|
+
(Money.locale_backend && Money.locale_backend.lookup(key, currency)) || DEFAULTS[key]
|
375
|
+
end
|
376
|
+
|
377
|
+
def symbol_value_from(rules)
|
378
|
+
if rules.has_key?(:symbol)
|
379
|
+
if rules[:symbol] === true
|
380
|
+
if rules[:disambiguate] && currency.disambiguate_symbol
|
381
|
+
currency.disambiguate_symbol
|
382
|
+
else
|
383
|
+
money.symbol
|
384
|
+
end
|
385
|
+
elsif rules[:symbol]
|
386
|
+
rules[:symbol]
|
387
|
+
else
|
388
|
+
""
|
389
|
+
end
|
390
|
+
elsif rules[:html] || rules[:html_wrap]
|
391
|
+
currency.html_entity == '' ? currency.symbol : currency.html_entity
|
392
|
+
elsif rules[:disambiguate] && currency.disambiguate_symbol
|
393
|
+
currency.disambiguate_symbol
|
394
|
+
else
|
395
|
+
money.symbol
|
396
|
+
end
|
397
|
+
end
|
398
|
+
end
|
399
|
+
end
|
@@ -0,0 +1,142 @@
|
|
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
|
+
@rules[:delimiter_pattern] ||= delimiter_pattern_rule(@rules)
|
16
|
+
|
17
|
+
warn_about_deprecated_rules(@rules)
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
@rules[key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def has_key?(key)
|
25
|
+
@rules.has_key? key
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
attr_reader :currency
|
31
|
+
|
32
|
+
# Cleans up formatting rules.
|
33
|
+
#
|
34
|
+
# @param [Hash] rules
|
35
|
+
#
|
36
|
+
# @return [Hash]
|
37
|
+
def normalize_formatting_rules(rules)
|
38
|
+
if rules.size == 0
|
39
|
+
rules = {}
|
40
|
+
elsif rules.size == 1
|
41
|
+
rules = rules.pop
|
42
|
+
rules = rules.dup if rules.is_a?(Hash)
|
43
|
+
|
44
|
+
if rules.is_a?(Symbol)
|
45
|
+
warn '[DEPRECATION] Use Hash when passing rules to Money#format.'
|
46
|
+
rules = { rules => true }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
if !rules.include?(:decimal_mark) && rules.include?(:separator)
|
51
|
+
rules[:decimal_mark] = rules[:separator]
|
52
|
+
end
|
53
|
+
|
54
|
+
if !rules.include?(:thousands_separator) && rules.include?(:delimiter)
|
55
|
+
rules[:thousands_separator] = rules[:delimiter]
|
56
|
+
end
|
57
|
+
|
58
|
+
rules
|
59
|
+
end
|
60
|
+
|
61
|
+
def default_formatting_rules
|
62
|
+
Money.default_formatting_rules || {}
|
63
|
+
end
|
64
|
+
|
65
|
+
def translate_formatting_rules(rules)
|
66
|
+
begin
|
67
|
+
rules[:symbol] = I18n.t currency.iso_code, scope: "number.currency.symbol", raise: true
|
68
|
+
rescue I18n::MissingTranslationData
|
69
|
+
# Do nothing
|
70
|
+
end
|
71
|
+
rules
|
72
|
+
end
|
73
|
+
|
74
|
+
def localize_formatting_rules(rules)
|
75
|
+
if currency.iso_code == "JPY" && I18n.locale == :ja
|
76
|
+
rules[:symbol] = "円" unless rules[:symbol] == false
|
77
|
+
rules[:format] = '%n%u'
|
78
|
+
end
|
79
|
+
rules
|
80
|
+
end
|
81
|
+
|
82
|
+
def determine_format_from_formatting_rules(rules)
|
83
|
+
return currency.format if currency.format && !rules.has_key?(:symbol_position)
|
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'
|
89
|
+
else
|
90
|
+
rules[:symbol_after_without_space] ? '%n%u' : '%n %u'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def delimiter_pattern_rule(rules)
|
95
|
+
if rules[:south_asian_number_formatting]
|
96
|
+
# from http://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
|
97
|
+
/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
|
98
|
+
else
|
99
|
+
/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
|
100
|
+
end
|
101
|
+
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
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'money/locale_backend/errors'
|
4
|
+
require 'money/locale_backend/legacy'
|
5
|
+
require 'money/locale_backend/i18n'
|
6
|
+
require 'money/locale_backend/currency'
|
7
|
+
|
8
|
+
class Money
|
9
|
+
module LocaleBackend
|
10
|
+
BACKENDS = {
|
11
|
+
legacy: Money::LocaleBackend::Legacy,
|
12
|
+
i18n: Money::LocaleBackend::I18n,
|
13
|
+
currency: Money::LocaleBackend::Currency
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
def self.find(name)
|
17
|
+
raise Unknown, "Unknown locale backend: #{name}" unless BACKENDS.key?(name)
|
18
|
+
|
19
|
+
BACKENDS[name].new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|