money 2.1.4 → 2.1.5
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.
- data/lib/money/money.rb +25 -15
- data/lib/money/symbols.rb +2 -1
- data/money.gemspec +1 -1
- data/test/money_spec.rb +31 -7
- metadata +6 -4
data/lib/money/money.rb
CHANGED
@@ -73,7 +73,17 @@ class Money
|
|
73
73
|
# Money.ca_dollar and Money.us_dollar
|
74
74
|
def initialize(cents, currency = Money.default_currency, bank = Money.default_bank)
|
75
75
|
@cents = cents.round
|
76
|
-
|
76
|
+
if currency.is_a?(Hash)
|
77
|
+
# Earlier versions of Money wrongly documented the constructor as being able
|
78
|
+
# to accept something like this:
|
79
|
+
#
|
80
|
+
# Money.new(50, :currency => "USD")
|
81
|
+
#
|
82
|
+
# We retain compatibility here.
|
83
|
+
@currency = currency[:currency] || Money.default_currency
|
84
|
+
else
|
85
|
+
@currency = currency
|
86
|
+
end
|
77
87
|
@bank = bank
|
78
88
|
end
|
79
89
|
|
@@ -94,7 +104,7 @@ class Money
|
|
94
104
|
if currency == other_money.currency
|
95
105
|
Money.new(cents + other_money.cents, other_money.currency)
|
96
106
|
else
|
97
|
-
Money.new(cents + other_money.exchange_to(currency).cents,currency)
|
107
|
+
Money.new(cents + other_money.exchange_to(currency).cents, currency)
|
98
108
|
end
|
99
109
|
end
|
100
110
|
|
@@ -163,31 +173,31 @@ class Money
|
|
163
173
|
# Whether a money symbol should be prepended to the result string. The default is true.
|
164
174
|
# This method attempts to pick a symbol that's suitable for the given currency.
|
165
175
|
#
|
166
|
-
# Money.new(100,
|
167
|
-
# Money.new(100,
|
168
|
-
# Money.new(100,
|
176
|
+
# Money.new(100, "USD") => "$1.00"
|
177
|
+
# Money.new(100, "GBP") => "£1.00"
|
178
|
+
# Money.new(100, "EUR") => "€1.00"
|
169
179
|
#
|
170
180
|
# # Same thing.
|
171
|
-
# Money.new(100,
|
172
|
-
# Money.new(100,
|
173
|
-
# Money.new(100,
|
181
|
+
# Money.new(100, "USD").format(:symbol => true) => "$1.00"
|
182
|
+
# Money.new(100, "GBP").format(:symbol => true) => "£1.00"
|
183
|
+
# Money.new(100, "EUR").format(:symbol => true) => "€1.00"
|
174
184
|
#
|
175
185
|
# You can specify a false expression or an empty string to disable prepending
|
176
186
|
# a money symbol:
|
177
187
|
#
|
178
|
-
# Money.new(100,
|
179
|
-
# Money.new(100,
|
180
|
-
# Money.new(100,
|
188
|
+
# Money.new(100, "USD").format(:symbol => false) => "1.00"
|
189
|
+
# Money.new(100, "GBP").format(:symbol => nil) => "1.00"
|
190
|
+
# Money.new(100, "EUR").format(:symbol => "") => "1.00"
|
181
191
|
#
|
182
192
|
#
|
183
193
|
# If the symbol for the given currency isn't known, then it will default
|
184
194
|
# to "$" as symbol:
|
185
195
|
#
|
186
|
-
# Money.new(100,
|
196
|
+
# Money.new(100, "AWG").format(:symbol => true) => "$1.00"
|
187
197
|
#
|
188
198
|
# You can specify a string as value to enforce using a particular symbol:
|
189
199
|
#
|
190
|
-
# Money.new(100,
|
200
|
+
# Money.new(100, "AWG").format(:symbol => "ƒ") => "ƒ1.00"
|
191
201
|
#
|
192
202
|
# === +:html+
|
193
203
|
#
|
@@ -209,14 +219,14 @@ class Money
|
|
209
219
|
|
210
220
|
if rules.has_key?(:symbol)
|
211
221
|
if rules[:symbol] === true
|
212
|
-
symbol = SYMBOLS[currency
|
222
|
+
symbol = SYMBOLS[currency] || "$"
|
213
223
|
elsif rules[:symbol]
|
214
224
|
symbol = rules[:symbol]
|
215
225
|
else
|
216
226
|
symbol = ""
|
217
227
|
end
|
218
228
|
else
|
219
|
-
symbol = "$"
|
229
|
+
symbol = SYMBOLS[currency] || "$"
|
220
230
|
end
|
221
231
|
|
222
232
|
if rules[:no_cents]
|
data/lib/money/symbols.rb
CHANGED
data/money.gemspec
CHANGED
data/test/money_spec.rb
CHANGED
@@ -73,6 +73,14 @@ describe Money do
|
|
73
73
|
Money.euro(50).should == Money.new(50, "EUR")
|
74
74
|
end
|
75
75
|
|
76
|
+
specify "Money.new accepts { :currency => 'foo' } as the value for the 'currency' argument" do
|
77
|
+
money = Money.new(20, :currency => "EUR")
|
78
|
+
money.currency.should == "EUR"
|
79
|
+
|
80
|
+
money = Money.new(20, :currency => nil)
|
81
|
+
money.currency.should == Money.default_currency
|
82
|
+
end
|
83
|
+
|
76
84
|
specify "Money.add_rate works" do
|
77
85
|
Money.add_rate("EUR", "USD", 10)
|
78
86
|
Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
|
@@ -129,11 +137,13 @@ describe Money do
|
|
129
137
|
end
|
130
138
|
|
131
139
|
specify "#format(:symbol => a symbol string) uses the given value as the money symbol" do
|
132
|
-
Money.new(100,
|
140
|
+
Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
|
133
141
|
end
|
134
142
|
|
135
143
|
specify "#format(:symbol => true) returns symbol based on the given currency code" do
|
136
|
-
one = Proc.new
|
144
|
+
one = Proc.new do |currency|
|
145
|
+
Money.new(100, currency).format(:symbol => true)
|
146
|
+
end
|
137
147
|
|
138
148
|
# Pounds
|
139
149
|
one["GBP"].should == "£1.00"
|
@@ -158,28 +168,42 @@ describe Money do
|
|
158
168
|
one["SCR"].should == "₨1.00"
|
159
169
|
one["LKR"].should == "₨1.00"
|
160
170
|
|
171
|
+
# Brazilian Real
|
172
|
+
one["BRL"].should == "R$1.00"
|
173
|
+
|
161
174
|
# Other
|
162
175
|
one["SEK"].should == "kr1.00"
|
163
176
|
one["GHC"].should == "¢1.00"
|
164
177
|
end
|
165
178
|
|
166
179
|
specify "#format(:symbol => true) returns $ when currency code is not recognized" do
|
167
|
-
Money.new(100,
|
180
|
+
Money.new(100, "XYZ").format(:symbol => true).should == "$1.00"
|
168
181
|
end
|
169
182
|
|
170
183
|
specify "#format(:symbol => some non-Boolean value that evaluates to true) returs symbol based on the given currency code" do
|
171
|
-
Money.new(100,
|
172
|
-
Money.new(100,
|
173
|
-
Money.new(100,
|
184
|
+
Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
|
185
|
+
Money.new(100, "EUR").format(:symbol => true).should == "€1.00"
|
186
|
+
Money.new(100, "SEK").format(:symbol => true).should == "kr1.00"
|
174
187
|
end
|
175
188
|
|
176
189
|
specify "#format with :symbol == "", nil or false returns the amount without a symbol" do
|
177
|
-
money = Money.new(100,
|
190
|
+
money = Money.new(100, "GBP")
|
178
191
|
money.format(:symbol => "").should == "1.00"
|
179
192
|
money.format(:symbol => nil).should == "1.00"
|
180
193
|
money.format(:symbol => false).should == "1.00"
|
181
194
|
end
|
182
195
|
|
196
|
+
specify "#format without :symbol assumes that :symbol is set to true" do
|
197
|
+
money = Money.new(100)
|
198
|
+
money.format.should == "$1.00"
|
199
|
+
|
200
|
+
money = Money.new(100, "GBP")
|
201
|
+
money.format.should == "£1.00"
|
202
|
+
|
203
|
+
money = Money.new(100, "XYZ")
|
204
|
+
money.format.should == "$1.00"
|
205
|
+
end
|
206
|
+
|
183
207
|
specify "#format(:html => true) works as documented" do
|
184
208
|
string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
|
185
209
|
string.should == "$5.70 <span class=\"currency\">CAD</span>"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Luetke
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2009-
|
14
|
+
date: 2009-11-05 00:00:00 +01:00
|
15
15
|
default_executable:
|
16
16
|
dependencies: []
|
17
17
|
|
@@ -39,6 +39,8 @@ files:
|
|
39
39
|
- test/money_spec.rb
|
40
40
|
has_rdoc: true
|
41
41
|
homepage: http://money.rubyforge.org/
|
42
|
+
licenses: []
|
43
|
+
|
42
44
|
post_install_message:
|
43
45
|
rdoc_options: []
|
44
46
|
|
@@ -59,9 +61,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
61
|
requirements: []
|
60
62
|
|
61
63
|
rubyforge_project: money
|
62
|
-
rubygems_version: 1.3.
|
64
|
+
rubygems_version: 1.3.5
|
63
65
|
signing_key:
|
64
|
-
specification_version:
|
66
|
+
specification_version: 3
|
65
67
|
summary: Money and currency exchange support library
|
66
68
|
test_files: []
|
67
69
|
|