money 6.0.1.beta2 → 6.0.1.beta3

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.
@@ -45,19 +45,21 @@ class Money
45
45
  self == other_money
46
46
  end
47
47
 
48
- def <=>(other_money)
49
- if other_money.respond_to?(:to_money)
50
- unless other_money.is_a?(Money)
51
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to compare Money to core classes." unless Money.silence_core_extensions_deprecations
52
- end
53
- other_money = other_money.to_money
54
- if fractional == 0 || other_money.fractional == 0 || currency == other_money.currency
55
- fractional <=> other_money.fractional
56
- else
57
- fractional <=> other_money.exchange_to(currency).fractional
58
- end
59
- else
60
- raise ArgumentError, "Comparison of #{self.class} with #{other_money.inspect} failed"
48
+ def <=>(val)
49
+ check_compare_deprecate(val)
50
+
51
+ val = val.to_money
52
+ unless fractional == 0 || val.fractional == 0 || currency == val.currency
53
+ val = val.exchange_to(currency)
54
+ end
55
+ fractional <=> val.fractional
56
+ rescue NoMethodError
57
+ raise ArgumentError, "Comparison of #{self.class} with #{val.inspect} failed"
58
+ end
59
+
60
+ def check_compare_deprecate(val)
61
+ unless val.is_a?(Money) || Money.silence_core_extensions_deprecations
62
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to compare Money to core classes."
61
63
  end
62
64
  end
63
65
 
@@ -188,20 +190,29 @@ class Money
188
190
  # Money.new(100).divmod(Money.new(9)) #=> [11, #<Money @fractional=1>]
189
191
  def divmod(val)
190
192
  if val.is_a?(Money)
191
- a = fractional
192
- b = val.exchange_to(currency).cents
193
- q, m = a.divmod(b)
194
- return [q, Money.new(m, currency)]
193
+ divmod_money(val)
195
194
  else
196
- if self.class.infinite_precision
197
- q, m = fractional.divmod(as_d(val))
198
- return [Money.new(q, currency), Money.new(m, currency)]
199
- else
200
- return [div(val), Money.new(fractional.modulo(val), currency)]
201
- end
195
+ divmod_other(val)
202
196
  end
203
197
  end
204
198
 
199
+ def divmod_money(val)
200
+ cents = val.exchange_to(currency).cents
201
+ quotient, remainder = fractional.divmod(cents)
202
+ [quotient, Money.new(remainder, currency)]
203
+ end
204
+ private :divmod_money
205
+
206
+ def divmod_other(val)
207
+ if self.class.infinite_precision
208
+ quotient, remainder = fractional.divmod(as_d(val))
209
+ [Money.new(quotient, currency), Money.new(remainder, currency)]
210
+ else
211
+ [div(val), Money.new(fractional.modulo(val), currency)]
212
+ end
213
+ end
214
+ private :divmod_other
215
+
205
216
  # Equivalent to +self.divmod(val)[1]+
206
217
  #
207
218
  # @param [Money, Fixnum] val Number take modulo with.
@@ -235,15 +246,15 @@ class Money
235
246
  # @example
236
247
  # Money.new(100).remainder(9) #=> #<Money @fractional=1>
237
248
  def remainder(val)
238
- a, b = self, val
239
- b = b.exchange_to(a.currency) if b.is_a?(Money) and a.currency != b.currency
240
-
241
- a_sign, b_sign = :pos, :pos
242
- a_sign = :neg if a.fractional < 0
243
- b_sign = :neg if (b.is_a?(Money) and b.fractional < 0) or (b < 0)
249
+ if val.is_a?(Money) && currency != val.currency
250
+ val = val.exchange_to(currency)
251
+ end
244
252
 
245
- return a.modulo(b) if a_sign == b_sign
246
- a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b, a.currency))
253
+ if (fractional < 0 && val < 0) || (fractional > 0 && val > 0)
254
+ self.modulo(val)
255
+ else
256
+ self.modulo(val) - (val.is_a?(Money) ? val : Money.new(val, currency))
257
+ end
247
258
  end
248
259
 
249
260
  # Return absolute value of self as a new Money object.
@@ -3,47 +3,35 @@ class Money
3
3
  module Formatting
4
4
 
5
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 || ",")
6
+ [
7
+ [:thousands_separator, :delimiter, "."],
8
+ [:decimal_mark, :separator, ","]
9
+ ].each do |method, name, character|
10
+ define_method(method) do
11
+ if self.class.use_i18n
12
+ I18n.t(
13
+ :"number.currency.format.#{name}",
14
+ :default => I18n.t(
15
+ :"number.format.#{name}",
16
+ :default => (currency.send(method) || character)
17
+ )
13
18
  )
14
- )
15
- else
16
- currency.thousands_separator || ","
19
+ else
20
+ currency.send(method) || character
21
+ end
17
22
  end
18
23
  end
19
24
  else
20
25
  def thousands_separator
21
26
  currency.thousands_separator || ","
22
27
  end
23
- end
24
- alias :delimiter :thousands_separator
25
-
26
28
 
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
29
  def decimal_mark
43
30
  currency.decimal_mark || "."
44
31
  end
45
32
  end
46
- alias :separator :decimal_mark
33
+ alias_method :delimiter, :thousands_separator
34
+ alias_method :separator, :decimal_mark
47
35
 
48
36
  # Creates a formatted price string according to several rules.
49
37
  #
@@ -213,20 +201,7 @@ class Money
213
201
  end
214
202
  end
215
203
 
216
- symbol_value =
217
- if rules.has_key?(:symbol)
218
- if rules[:symbol] === true
219
- symbol
220
- elsif rules[:symbol]
221
- rules[:symbol]
222
- else
223
- ""
224
- end
225
- elsif rules[:html]
226
- currency.html_entity == '' ? currency.symbol : currency.html_entity
227
- else
228
- symbol
229
- end
204
+ symbol_value = symbol_value_from(rules)
230
205
 
231
206
  formatted = self.abs.to_s
232
207
 
@@ -254,14 +229,7 @@ class Money
254
229
  formatted.gsub!(regexp_format(formatted, rules, decimal_mark, symbol_value),
255
230
  "\\1#{thousands_separator_value}")
256
231
 
257
- symbol_position =
258
- if rules.has_key?(:symbol_position)
259
- rules[:symbol_position]
260
- elsif currency.symbol_first?
261
- :before
262
- else
263
- :after
264
- end
232
+ symbol_position = symbol_position_from(rules)
265
233
 
266
234
  if rules[:sign_positive] == true && self.positive?
267
235
  sign = '+'
@@ -298,7 +266,6 @@ class Money
298
266
  formatted
299
267
  end
300
268
 
301
-
302
269
  private
303
270
 
304
271
  # Cleans up formatting rules.
@@ -345,4 +312,30 @@ class Money
345
312
  end
346
313
  rules
347
314
  end
315
+
316
+ def symbol_value_from(rules)
317
+ if rules.has_key?(:symbol)
318
+ if rules[:symbol] === true
319
+ symbol
320
+ elsif rules[:symbol]
321
+ rules[:symbol]
322
+ else
323
+ ""
324
+ end
325
+ elsif rules[:html]
326
+ currency.html_entity == '' ? currency.symbol : currency.html_entity
327
+ else
328
+ symbol
329
+ end
330
+ end
331
+
332
+ def symbol_position_from(rules)
333
+ if rules.has_key?(:symbol_position)
334
+ rules[:symbol_position]
335
+ elsif currency.symbol_first?
336
+ :before
337
+ else
338
+ :after
339
+ end
340
+ end
348
341
  end
@@ -1,3 +1,3 @@
1
1
  class Money
2
- VERSION = "6.0.1.beta2"
2
+ VERSION = "6.0.1.beta3"
3
3
  end
@@ -96,6 +96,16 @@ describe Money::Bank::VariableExchange do
96
96
  it "raises an UnknownCurrency exception when an unknown currency is passed" do
97
97
  expect { subject.set_rate('AAA', 'BBB', 1.25) }.to raise_exception(Money::Currency::UnknownCurrency)
98
98
  end
99
+
100
+ it "uses a mutex by default" do
101
+ subject.instance_variable_get(:@mutex).should_receive(:synchronize)
102
+ subject.set_rate('USD', 'EUR', 1.25)
103
+ end
104
+
105
+ it "doesn't use mutex if requested not to" do
106
+ subject.instance_variable_get(:@mutex).should_not_receive(:synchronize)
107
+ subject.set_rate('USD', 'EUR', 1.25, :without_mutex => true)
108
+ end
99
109
  end
100
110
 
101
111
  describe "#get_rate" do
@@ -107,6 +117,16 @@ describe Money::Bank::VariableExchange do
107
117
  it "raises an UnknownCurrency exception when an unknown currency is passed" do
108
118
  expect { subject.get_rate('AAA', 'BBB') }.to raise_exception(Money::Currency::UnknownCurrency)
109
119
  end
120
+
121
+ it "uses a mutex by default" do
122
+ subject.instance_variable_get(:@mutex).should_receive(:synchronize)
123
+ subject.get_rate('USD', 'EUR')
124
+ end
125
+
126
+ it "doesn't use mutex if requested not to" do
127
+ subject.instance_variable_get(:@mutex).should_not_receive(:synchronize)
128
+ subject.get_rate('USD', 'EUR', :without_mutex => true)
129
+ end
110
130
  end
111
131
 
112
132
  describe "#export_rates" do
@@ -152,6 +172,16 @@ describe Money::Bank::VariableExchange do
152
172
  subject.export_rates(:json, 'null')
153
173
  end
154
174
  end
175
+
176
+ it "uses a mutex by default" do
177
+ subject.instance_variable_get(:@mutex).should_receive(:synchronize)
178
+ subject.export_rates(:yaml)
179
+ end
180
+
181
+ it "doesn't use mutex if requested not to" do
182
+ subject.instance_variable_get(:@mutex).should_not_receive(:synchronize)
183
+ subject.export_rates(:yaml, nil, :without_mutex => true)
184
+ end
155
185
  end
156
186
 
157
187
  describe "#import_rates" do
@@ -187,6 +217,18 @@ describe Money::Bank::VariableExchange do
187
217
  expect { subject.import_rates(:foo, "")}.to raise_error Money::Bank::UnknownRateFormat
188
218
  end
189
219
  end
220
+
221
+ it "uses a mutex by default" do
222
+ subject.instance_variable_get(:@mutex).should_receive(:synchronize)
223
+ s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
224
+ subject.import_rates(:yaml, s)
225
+ end
226
+
227
+ it "doesn't use mutex if requested not to" do
228
+ subject.instance_variable_get(:@mutex).should_not_receive(:synchronize)
229
+ s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
230
+ subject.import_rates(:yaml, s, :without_mutex => true)
231
+ end
190
232
  end
191
233
 
192
234
  describe "#rate_key_for" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.1.beta2
4
+ version: 6.0.1.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-03 00:00:00.000000000 Z
11
+ date: 2014-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n