money 3.7.1 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +384 -351
- data/LICENSE +21 -21
- data/README.md +243 -214
- data/Rakefile +49 -49
- data/lib/money.rb +28 -27
- data/lib/money/bank/base.rb +131 -131
- data/lib/money/bank/variable_exchange.rb +252 -252
- data/lib/money/core_extensions.rb +82 -82
- data/lib/money/currency.rb +263 -422
- data/lib/money/currency_loader.rb +19 -0
- data/lib/money/money.rb +405 -405
- data/lib/money/money/arithmetic.rb +246 -246
- data/lib/money/money/formatting.rb +260 -244
- data/lib/money/money/parsing.rb +350 -350
- data/money.gemspec +29 -35
- data/spec/bank/base_spec.rb +72 -72
- data/spec/bank/variable_exchange_spec.rb +238 -238
- data/spec/core_extensions_spec.rb +158 -158
- data/spec/currency_spec.rb +120 -133
- data/spec/money/arithmetic_spec.rb +479 -479
- data/spec/money/formatting_spec.rb +383 -375
- data/spec/money/parsing_spec.rb +197 -197
- data/spec/money_spec.rb +292 -292
- data/spec/spec_helper.rb +28 -28
- metadata +54 -126
- data/lib/money.rbc +0 -184
- data/lib/money/bank/base.rbc +0 -818
- data/lib/money/bank/variable_exchange.rbc +0 -2550
- data/lib/money/core_extensions.rbc +0 -664
- data/lib/money/currency.rbc +0 -22708
- data/lib/money/money.rbc +0 -3861
- data/lib/money/money/arithmetic.rbc +0 -2778
- data/lib/money/money/formatting.rbc +0 -2265
- data/lib/money/money/parsing.rbc +0 -2737
- data/spec/bank/base_spec.rbc +0 -2461
- data/spec/bank/variable_exchange_spec.rbc +0 -7541
- data/spec/core_extensions_spec.rbc +0 -5921
- data/spec/currency_spec.rbc +0 -4535
- data/spec/money/arithmetic_spec.rbc +0 -25140
- data/spec/money/formatting_spec.rbc +0 -12545
- data/spec/money/parsing_spec.rbc +0 -6511
- data/spec/money_spec.rbc +0 -9824
- data/spec/spec_helper.rbc +0 -575
@@ -1,244 +1,260 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
:
|
10
|
-
|
11
|
-
:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
:
|
32
|
-
|
33
|
-
:
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
# Money.us_dollar(0).format(:display_free =>
|
59
|
-
# Money.us_dollar(0).format
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
# Money.ca_dollar(100).format
|
67
|
-
# Money.
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
# Money.ca_dollar(
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
# Money.ca_dollar(
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
# Money.new(100, "
|
89
|
-
# Money.new(100, "
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
# Money.new(100, "
|
94
|
-
# Money.new(100, "
|
95
|
-
#
|
96
|
-
#
|
97
|
-
# #
|
98
|
-
#
|
99
|
-
# Money.new(100, "
|
100
|
-
# Money.new(100, "
|
101
|
-
#
|
102
|
-
#
|
103
|
-
# #
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
# #
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
# Money.new(100000, "USD").format(:thousands_separator =>
|
127
|
-
# Money.new(100000, "USD").format(:thousands_separator =>
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
133
|
-
# #
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
# s
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
return
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
"#{self.to_s}"
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
:
|
185
|
-
|
186
|
-
:
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
if rules.has_key?(:
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
rules
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
class Money
|
3
|
+
module Formatting
|
4
|
+
|
5
|
+
if Object.const_defined?("I18n")
|
6
|
+
def thousands_separator
|
7
|
+
if self.class.use_i18n
|
8
|
+
I18n.t(
|
9
|
+
:"number.currency.format.delimiter",
|
10
|
+
:default => I18n.t(
|
11
|
+
:"number.format.delimiter",
|
12
|
+
:default => (currency.thousands_separator || ",")
|
13
|
+
)
|
14
|
+
)
|
15
|
+
else
|
16
|
+
currency.thousands_separator || ","
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
def thousands_separator
|
21
|
+
currency.thousands_separator || ","
|
22
|
+
end
|
23
|
+
end
|
24
|
+
alias :delimiter :thousands_separator
|
25
|
+
|
26
|
+
|
27
|
+
if Object.const_defined?("I18n")
|
28
|
+
def decimal_mark
|
29
|
+
if self.class.use_i18n
|
30
|
+
I18n.t(
|
31
|
+
:"number.currency.format.separator",
|
32
|
+
:default => I18n.t(
|
33
|
+
:"number.format.separator",
|
34
|
+
:default => (currency.decimal_mark || ".")
|
35
|
+
)
|
36
|
+
)
|
37
|
+
else
|
38
|
+
currency.decimal_mark || "."
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
def decimal_mark
|
43
|
+
currency.decimal_mark || "."
|
44
|
+
end
|
45
|
+
end
|
46
|
+
alias :separator :decimal_mark
|
47
|
+
|
48
|
+
# Creates a formatted price string according to several rules.
|
49
|
+
#
|
50
|
+
# @param [Hash] *rules The options used to format the string.
|
51
|
+
#
|
52
|
+
# @return [String]
|
53
|
+
#
|
54
|
+
# @option *rules [Boolean, String] :display_free (false) Whether a zero
|
55
|
+
# amount of money should be formatted of "free" or as the supplied string.
|
56
|
+
#
|
57
|
+
# @example
|
58
|
+
# Money.us_dollar(0).format(:display_free => true) #=> "free"
|
59
|
+
# Money.us_dollar(0).format(:display_free => "gratis") #=> "gratis"
|
60
|
+
# Money.us_dollar(0).format #=> "$0.00"
|
61
|
+
#
|
62
|
+
# @option *rules [Boolean] :with_currency (false) Whether the currency name
|
63
|
+
# should be appended to the result string.
|
64
|
+
#
|
65
|
+
# @example
|
66
|
+
# Money.ca_dollar(100).format => "$1.00"
|
67
|
+
# Money.ca_dollar(100).format(:with_currency => true) #=> "$1.00 CAD"
|
68
|
+
# Money.us_dollar(85).format(:with_currency => true) #=> "$0.85 USD"
|
69
|
+
#
|
70
|
+
# @option *rules [Boolean] :no_cents (false) Whether cents should be omitted.
|
71
|
+
#
|
72
|
+
# @example
|
73
|
+
# Money.ca_dollar(100).format(:no_cents => true) #=> "$1"
|
74
|
+
# Money.ca_dollar(599).format(:no_cents => true) #=> "$5"
|
75
|
+
#
|
76
|
+
# @option *rules [Boolean] :no_cents_if_whole (false) Whether cents should be
|
77
|
+
# omitted if the cent value is zero
|
78
|
+
#
|
79
|
+
# @example
|
80
|
+
# Money.ca_dollar(10000).format(:no_cents_if_whole => true) #=> "$100"
|
81
|
+
# Money.ca_dollar(10034).format(:no_cents_if_whole => true) #=> "$100.34"
|
82
|
+
#
|
83
|
+
# @option *rules [Boolean, String, nil] :symbol (true) Whether a money symbol
|
84
|
+
# should be prepended to the result string. The default is true. This method
|
85
|
+
# attempts to pick a symbol that's suitable for the given currency.
|
86
|
+
#
|
87
|
+
# @example
|
88
|
+
# Money.new(100, "USD") #=> "$1.00"
|
89
|
+
# Money.new(100, "GBP") #=> "£1.00"
|
90
|
+
# Money.new(100, "EUR") #=> "€1.00"
|
91
|
+
#
|
92
|
+
# # Same thing.
|
93
|
+
# Money.new(100, "USD").format(:symbol => true) #=> "$1.00"
|
94
|
+
# Money.new(100, "GBP").format(:symbol => true) #=> "£1.00"
|
95
|
+
# Money.new(100, "EUR").format(:symbol => true) #=> "€1.00"
|
96
|
+
#
|
97
|
+
# # You can specify a false expression or an empty string to disable
|
98
|
+
# # prepending a money symbol.§
|
99
|
+
# Money.new(100, "USD").format(:symbol => false) #=> "1.00"
|
100
|
+
# Money.new(100, "GBP").format(:symbol => nil) #=> "1.00"
|
101
|
+
# Money.new(100, "EUR").format(:symbol => "") #=> "1.00"
|
102
|
+
#
|
103
|
+
# # If the symbol for the given currency isn't known, then it will default
|
104
|
+
# # to "¤" as symbol.
|
105
|
+
# Money.new(100, "AWG").format(:symbol => true) #=> "¤1.00"
|
106
|
+
#
|
107
|
+
# # You can specify a string as value to enforce using a particular symbol.
|
108
|
+
# Money.new(100, "AWG").format(:symbol => "ƒ") #=> "ƒ1.00"
|
109
|
+
#
|
110
|
+
# @option *rules [Boolean, String, nil] :decimal_mark (true) Whether the
|
111
|
+
# currency should be separated by the specified character or '.'
|
112
|
+
#
|
113
|
+
# @example
|
114
|
+
# # If a string is specified, it's value is used.
|
115
|
+
# Money.new(100, "USD").format(:decimal_mark => ",") #=> "$1,00"
|
116
|
+
#
|
117
|
+
# # If the decimal_mark for a given currency isn't known, then it will default
|
118
|
+
# # to "." as decimal_mark.
|
119
|
+
# Money.new(100, "FOO").format #=> "$1.00"
|
120
|
+
#
|
121
|
+
# @option *rules [Boolean, String, nil] :thousands_separator (true) Whether
|
122
|
+
# the currency should be delimited by the specified character or ','
|
123
|
+
#
|
124
|
+
# @example
|
125
|
+
# # If false is specified, no thousands_separator is used.
|
126
|
+
# Money.new(100000, "USD").format(:thousands_separator => false) #=> "1000.00"
|
127
|
+
# Money.new(100000, "USD").format(:thousands_separator => nil) #=> "1000.00"
|
128
|
+
# Money.new(100000, "USD").format(:thousands_separator => "") #=> "1000.00"
|
129
|
+
#
|
130
|
+
# # If a string is specified, it's value is used.
|
131
|
+
# Money.new(100000, "USD").format(:thousands_separator => ".") #=> "$1.000.00"
|
132
|
+
#
|
133
|
+
# # If the thousands_separator for a given currency isn't known, then it will
|
134
|
+
# # default to "," as thousands_separator.
|
135
|
+
# Money.new(100000, "FOO").format #=> "$1,000.00"
|
136
|
+
#
|
137
|
+
# @option *rules [Boolean] :html (false) Whether the currency should be
|
138
|
+
# HTML-formatted. Only useful in combination with +:with_currency+.
|
139
|
+
#
|
140
|
+
# @example
|
141
|
+
# s = Money.ca_dollar(570).format(:html => true, :with_currency => true)
|
142
|
+
# s #=> "$5.70 <span class=\"currency\">CAD</span>"
|
143
|
+
def format(*rules)
|
144
|
+
# support for old format parameters
|
145
|
+
rules = normalize_formatting_rules(rules)
|
146
|
+
rules = localize_formatting_rules(rules)
|
147
|
+
|
148
|
+
if cents == 0
|
149
|
+
if rules[:display_free].respond_to?(:to_str)
|
150
|
+
return rules[:display_free]
|
151
|
+
elsif rules[:display_free]
|
152
|
+
return "free"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
symbol_value =
|
157
|
+
if rules.has_key?(:symbol)
|
158
|
+
if rules[:symbol] === true
|
159
|
+
symbol
|
160
|
+
elsif rules[:symbol]
|
161
|
+
rules[:symbol]
|
162
|
+
else
|
163
|
+
""
|
164
|
+
end
|
165
|
+
elsif rules[:html]
|
166
|
+
currency.html_entity
|
167
|
+
else
|
168
|
+
symbol
|
169
|
+
end
|
170
|
+
|
171
|
+
formatted = case rules[:no_cents]
|
172
|
+
when true
|
173
|
+
"#{self.to_s.to_i}"
|
174
|
+
else
|
175
|
+
"#{self.to_s}"
|
176
|
+
end
|
177
|
+
|
178
|
+
if rules[:no_cents_if_whole] && cents % currency.subunit_to_unit == 0
|
179
|
+
formatted = "#{self.to_s.to_i}"
|
180
|
+
end
|
181
|
+
|
182
|
+
symbol_position =
|
183
|
+
if rules.has_key?(:symbol_position)
|
184
|
+
rules[:symbol_position]
|
185
|
+
elsif currency.symbol_first?
|
186
|
+
:before
|
187
|
+
else
|
188
|
+
:after
|
189
|
+
end
|
190
|
+
|
191
|
+
if symbol_value && !symbol_value.empty?
|
192
|
+
formatted = if symbol_position == :before
|
193
|
+
"#{symbol_value}#{formatted}"
|
194
|
+
else
|
195
|
+
symbol_space = rules[:symbol_after_without_space] ? "" : " "
|
196
|
+
"#{formatted}#{symbol_space}#{symbol_value}"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
if rules.has_key?(:decimal_mark) and rules[:decimal_mark] and
|
201
|
+
rules[:decimal_mark] != decimal_mark
|
202
|
+
formatted.sub!(decimal_mark, rules[:decimal_mark])
|
203
|
+
end
|
204
|
+
|
205
|
+
thousands_separator_value = thousands_separator
|
206
|
+
# Determine thousands_separator
|
207
|
+
if rules.has_key?(:thousands_separator)
|
208
|
+
if rules[:thousands_separator] === false or rules[:thousands_separator].nil?
|
209
|
+
thousands_separator_value = ""
|
210
|
+
elsif rules[:thousands_separator]
|
211
|
+
thousands_separator_value = rules[:thousands_separator]
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# Apply thousands_separator
|
216
|
+
formatted.gsub!(/(\d)(?=(?:\d{3})+(?:[^\d]|$))/, "\\1#{thousands_separator_value}")
|
217
|
+
|
218
|
+
if rules[:with_currency]
|
219
|
+
formatted << " "
|
220
|
+
formatted << '<span class="currency">' if rules[:html]
|
221
|
+
formatted << currency.to_s
|
222
|
+
formatted << '</span>' if rules[:html]
|
223
|
+
end
|
224
|
+
formatted
|
225
|
+
end
|
226
|
+
|
227
|
+
|
228
|
+
private
|
229
|
+
|
230
|
+
# Cleans up formatting rules.
|
231
|
+
#
|
232
|
+
# @param [Hash]
|
233
|
+
#
|
234
|
+
# @return [Hash]
|
235
|
+
def normalize_formatting_rules(rules)
|
236
|
+
if rules.size == 0
|
237
|
+
rules = {}
|
238
|
+
elsif rules.size == 1
|
239
|
+
rules = rules.pop
|
240
|
+
rules = { rules => true } if rules.is_a?(Symbol)
|
241
|
+
end
|
242
|
+
if not rules.include?(:decimal_mark) and rules.include?(:separator)
|
243
|
+
rules[:decimal_mark] = rules[:separator]
|
244
|
+
end
|
245
|
+
if not rules.include?(:thousands_separator) and rules.include?(:delimiter)
|
246
|
+
rules[:thousands_separator] = rules[:delimiter]
|
247
|
+
end
|
248
|
+
rules
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
def localize_formatting_rules(rules)
|
253
|
+
if currency.iso_code == "JPY" && I18n.locale == :ja
|
254
|
+
rules[:symbol] = "円"
|
255
|
+
rules[:symbol_position] = :after
|
256
|
+
rules[:symbol_after_without_space] = true
|
257
|
+
end
|
258
|
+
rules
|
259
|
+
end
|
260
|
+
end
|