money 3.0.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ == Money 3.0.2
2
+ * Added #div, #divmod, #modulo, #% and #remainder methods
3
+
1
4
  == Money 3.0.1
2
5
  * Added #eql? method
3
6
  * Updated Numeric#to_money to work with all children of Numeric
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.1
1
+ 3.0.2
data/lib/money/money.rb CHANGED
@@ -167,6 +167,47 @@ class Money
167
167
  end
168
168
  end
169
169
 
170
+ # synonym for #/
171
+ def div(val)
172
+ self / val
173
+ end
174
+
175
+ # divide money by money or fixnum and return array containing quotient and
176
+ # modulus
177
+ def divmod(val)
178
+ if val.is_a?(Money)
179
+ a = self.cents
180
+ b = self.currency == val.currency ? val.cents : val.exchange_to(self.currency).cents
181
+ q, m = a.divmod(b)
182
+ return [q, Money.new(m, self.currency)]
183
+ else
184
+ return [self.div(val), Money.new(self.cents.modulo(val), self.currency)]
185
+ end
186
+ end
187
+
188
+ # equivalent to `self.divmod(val)[1]`
189
+ def modulo(val)
190
+ self.divmod(val)[1]
191
+ end
192
+
193
+ # synonym for #modulo
194
+ def %(val)
195
+ self.modulo(val)
196
+ end
197
+
198
+ # if different signs `self.modulo(val) - val` otherwise `self.modulo(val)`
199
+ def remainder(val)
200
+ a, b = self, val
201
+ b = b.exchange_to(a.currency) if b.is_a?(Money) and a.currency != b.currency
202
+
203
+ a_sign, b_sign = :pos, :pos
204
+ a_sign = :neg if a.cents < 0
205
+ b_sign = :neg if (b.is_a?(Money) and b.cents < 0) or (b < 0)
206
+
207
+ return a.modulo(b) if a_sign == b_sign
208
+ a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b, a.currency))
209
+ end
210
+
170
211
  # Test if the money amount is zero
171
212
  def zero?
172
213
  cents == 0
data/money.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{money}
8
- s.version = "3.0.1"
8
+ s.version = "3.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Luetke", "Hongli Lai", "Jeremy McNevin", "Shane Emmons"]
12
- s.date = %q{2010-05-26}
12
+ s.date = %q{2010-06-03}
13
13
  s.description = %q{Money and currency exchange support library.}
14
14
  s.email = %q{hongli@phusion.nl}
15
15
  s.extra_rdoc_files = [
data/test/money_spec.rb CHANGED
@@ -191,6 +191,228 @@ describe Money do
191
191
  (Money.new(10_00, "USD") / 10).should == Money.new(1_00, "USD")
192
192
  end
193
193
 
194
+ specify "#/ -> money / fixnum" do
195
+ ts = [
196
+ {:a => Money.new( 13, :USD), :b => 4, :c => Money.new( 3, :USD)},
197
+ {:a => Money.new( 13, :USD), :b => -4, :c => Money.new(-4, :USD)},
198
+ {:a => Money.new(-13, :USD), :b => 4, :c => Money.new(-4, :USD)},
199
+ {:a => Money.new(-13, :USD), :b => -4, :c => Money.new( 3, :USD)},
200
+ ]
201
+ ts.each do |t|
202
+ (t[:a] / t[:b]).should == t[:c]
203
+ end
204
+ end
205
+
206
+ specify "#/ -> money / money (same currency)" do
207
+ ts = [
208
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :USD), :c => 3.25},
209
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :USD), :c => -3.25},
210
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :USD), :c => -3.25},
211
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => 3.25},
212
+ ]
213
+ ts.each do |t|
214
+ (t[:a] / t[:b]).should == t[:c]
215
+ end
216
+ end
217
+
218
+ specify "#/ -> money / money (difference currency)" do
219
+ ts = [
220
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :EUR), :c => 1.625},
221
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :EUR), :c => -1.625},
222
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :EUR), :c => -1.625},
223
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => 1.625},
224
+ ]
225
+ ts.each do |t|
226
+ t[:b].should_receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
227
+ (t[:a] / t[:b]).should == t[:c]
228
+ end
229
+ end
230
+
231
+ specify "#div -> money / fixnum" do
232
+ ts = [
233
+ {:a => Money.new( 13, :USD), :b => 4, :c => Money.new( 3, :USD)},
234
+ {:a => Money.new( 13, :USD), :b => -4, :c => Money.new(-4, :USD)},
235
+ {:a => Money.new(-13, :USD), :b => 4, :c => Money.new(-4, :USD)},
236
+ {:a => Money.new(-13, :USD), :b => -4, :c => Money.new( 3, :USD)},
237
+ ]
238
+ ts.each do |t|
239
+ t[:a].div(t[:b]).should == t[:c]
240
+ end
241
+ end
242
+
243
+ specify "#div -> money / money (same currency)" do
244
+ ts = [
245
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :USD), :c => 3.25},
246
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :USD), :c => -3.25},
247
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :USD), :c => -3.25},
248
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => 3.25},
249
+ ]
250
+ ts.each do |t|
251
+ t[:a].div(t[:b]).should == t[:c]
252
+ end
253
+ end
254
+
255
+ specify "#div -> money / money (different currency)" do
256
+ ts = [
257
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :EUR), :c => 1.625},
258
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :EUR), :c => -1.625},
259
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :EUR), :c => -1.625},
260
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => 1.625},
261
+ ]
262
+ ts.each do |t|
263
+ t[:b].should_receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
264
+ t[:a].div(t[:b]).should == t[:c]
265
+ end
266
+ end
267
+
268
+ specify "#divmod -> money `divmod` fixnum" do
269
+ ts = [
270
+ {:a => Money.new( 13, :USD), :b => 4, :c => [Money.new( 3, :USD), Money.new( 1, :USD)]},
271
+ {:a => Money.new( 13, :USD), :b => -4, :c => [Money.new(-4, :USD), Money.new(-3, :USD)]},
272
+ {:a => Money.new(-13, :USD), :b => 4, :c => [Money.new(-4, :USD), Money.new( 3, :USD)]},
273
+ {:a => Money.new(-13, :USD), :b => -4, :c => [Money.new( 3, :USD), Money.new(-1, :USD)]},
274
+ ]
275
+ ts.each do |t|
276
+ t[:a].divmod(t[:b]).should == t[:c]
277
+ end
278
+ end
279
+
280
+ specify "#divmod -> money `divmod` money (same currency)" do
281
+ ts = [
282
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :USD), :c => [ 3, Money.new( 1, :USD)]},
283
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :USD), :c => [-4, Money.new(-3, :USD)]},
284
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :USD), :c => [-4, Money.new( 3, :USD)]},
285
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => [ 3, Money.new(-1, :USD)]},
286
+ ]
287
+ ts.each do |t|
288
+ t[:a].divmod(t[:b]).should == t[:c]
289
+ end
290
+ end
291
+
292
+ specify "#divmod -> money `divmod` money (different currency)" do
293
+ ts = [
294
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :EUR), :c => [ 1, Money.new( 5, :USD)]},
295
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :EUR), :c => [-2, Money.new(-3, :USD)]},
296
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :EUR), :c => [-2, Money.new( 3, :USD)]},
297
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => [ 1, Money.new(-5, :USD)]},
298
+ ]
299
+ ts.each do |t|
300
+ t[:b].should_receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
301
+ t[:a].divmod(t[:b]).should == t[:c]
302
+ end
303
+ end
304
+
305
+ specify "#modulo -> money `modulo` fixnum" do
306
+ ts = [
307
+ {:a => Money.new( 13, :USD), :b => 4, :c => Money.new( 1, :USD)},
308
+ {:a => Money.new( 13, :USD), :b => -4, :c => Money.new(-3, :USD)},
309
+ {:a => Money.new(-13, :USD), :b => 4, :c => Money.new( 3, :USD)},
310
+ {:a => Money.new(-13, :USD), :b => -4, :c => Money.new(-1, :USD)},
311
+ ]
312
+ ts.each do |t|
313
+ t[:a].modulo(t[:b]).should == t[:c]
314
+ end
315
+ end
316
+
317
+ specify "#modulo -> money `modulo` money (same currency)" do
318
+ ts = [
319
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :USD), :c => Money.new( 1, :USD)},
320
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :USD), :c => Money.new(-3, :USD)},
321
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :USD), :c => Money.new( 3, :USD)},
322
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => Money.new(-1, :USD)},
323
+ ]
324
+ ts.each do |t|
325
+ t[:a].modulo(t[:b]).should == t[:c]
326
+ end
327
+ end
328
+
329
+ specify "#modulo -> money `modulo` money (different currency)" do
330
+ ts = [
331
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :EUR), :c => Money.new( 5, :USD)},
332
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :EUR), :c => Money.new(-3, :USD)},
333
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :EUR), :c => Money.new( 3, :USD)},
334
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => Money.new(-5, :USD)},
335
+ ]
336
+ ts.each do |t|
337
+ t[:b].should_receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
338
+ t[:a].modulo(t[:b]).should == t[:c]
339
+ end
340
+ end
341
+
342
+ specify "#% -> money % fixnum" do
343
+ ts = [
344
+ {:a => Money.new( 13, :USD), :b => 4, :c => Money.new( 1, :USD)},
345
+ {:a => Money.new( 13, :USD), :b => -4, :c => Money.new(-3, :USD)},
346
+ {:a => Money.new(-13, :USD), :b => 4, :c => Money.new( 3, :USD)},
347
+ {:a => Money.new(-13, :USD), :b => -4, :c => Money.new(-1, :USD)},
348
+ ]
349
+ ts.each do |t|
350
+ (t[:a] % t[:b]).should == t[:c]
351
+ end
352
+ end
353
+
354
+ specify "#% -> money % money (same currency)" do
355
+ ts = [
356
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :USD), :c => Money.new( 1, :USD)},
357
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :USD), :c => Money.new(-3, :USD)},
358
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :USD), :c => Money.new( 3, :USD)},
359
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => Money.new(-1, :USD)},
360
+ ]
361
+ ts.each do |t|
362
+ (t[:a] % t[:b]).should == t[:c]
363
+ end
364
+ end
365
+
366
+ specify "#% -> money % money (different currency)" do
367
+ ts = [
368
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :EUR), :c => Money.new( 5, :USD)},
369
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :EUR), :c => Money.new(-3, :USD)},
370
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :EUR), :c => Money.new( 3, :USD)},
371
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => Money.new(-5, :USD)},
372
+ ]
373
+ ts.each do |t|
374
+ t[:b].should_receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
375
+ (t[:a] % t[:b]).should == t[:c]
376
+ end
377
+ end
378
+
379
+ specify "#remainder -> money `remainder` fixnum" do
380
+ ts = [
381
+ {:a => Money.new( 13, :USD), :b => 4, :c => Money.new( 1, :USD)},
382
+ {:a => Money.new( 13, :USD), :b => -4, :c => Money.new( 1, :USD)},
383
+ {:a => Money.new(-13, :USD), :b => 4, :c => Money.new(-1, :USD)},
384
+ {:a => Money.new(-13, :USD), :b => -4, :c => Money.new(-1, :USD)},
385
+ ]
386
+ ts.each do |t|
387
+ t[:a].remainder(t[:b]).should == t[:c]
388
+ end
389
+ end
390
+
391
+ specify "#remainder -> money `remainder` money (same currency)" do
392
+ ts = [
393
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :USD), :c => Money.new( 1, :USD)},
394
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :USD), :c => Money.new( 1, :USD)},
395
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :USD), :c => Money.new(-1, :USD)},
396
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => Money.new(-1, :USD)},
397
+ ]
398
+ ts.each do |t|
399
+ t[:a].remainder(t[:b]).should == t[:c]
400
+ end
401
+ end
402
+
403
+ specify "#remainder -> money `remainder` money (different currency)" do
404
+ ts = [
405
+ {:a => Money.new( 13, :USD), :b => Money.new( 4, :EUR), :c => Money.new( 5, :USD)},
406
+ {:a => Money.new( 13, :USD), :b => Money.new(-4, :EUR), :c => Money.new( 5, :USD)},
407
+ {:a => Money.new(-13, :USD), :b => Money.new( 4, :EUR), :c => Money.new(-5, :USD)},
408
+ {:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => Money.new(-5, :USD)},
409
+ ]
410
+ ts.each do |t|
411
+ t[:b].should_receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
412
+ t[:a].remainder(t[:b]).should == t[:c]
413
+ end
414
+ end
415
+
194
416
  specify "Money.empty creates a new Money object of 0 cents" do
195
417
  Money.empty.should == Money.new(0)
196
418
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
- - 1
10
- version: 3.0.1
9
+ - 2
10
+ version: 3.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Luetke
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2010-05-26 00:00:00 -04:00
21
+ date: 2010-06-03 00:00:00 -04:00
22
22
  default_executable:
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency