simple_money 0.2.0 → 0.2.1

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.
Files changed (3) hide show
  1. data/CHANGELOG.md +13 -0
  2. data/lib/simple_money/money.rb +166 -5
  3. metadata +3 -3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ SimpleMoney 0.2.1
2
+ =================
3
+
4
+ Features
5
+ --------
6
+ - Added #%, #<=>, #==, #abs and #divmod
7
+ - Cleanup/Enhanced documentation
8
+
9
+ Bug fixes
10
+ ---------
11
+ - Update all calculations to respect options set on object (rounding method,
12
+ currency, etc) when return a new object as a result of the calculation
13
+
1
14
  SimpleMoney 0.2.0
2
15
  =================
3
16
 
@@ -220,6 +220,8 @@ class Money
220
220
  # representation the object should be created using.
221
221
  #
222
222
  # @raise [ArgumentError] Will raise an ArgumentError if as is not valid.
223
+ # @raise [ArgumentError] Will raise an ArgumentError if rounding method is
224
+ # not valid.
223
225
  #
224
226
  # @example
225
227
  # Money.new #=> #<Money:... @cents: 0>
@@ -287,6 +289,8 @@ class Money
287
289
  #
288
290
  # @raise [ArgumentError] Will raise an ArgumentError unless n is a Money
289
291
  # object.
292
+ # @raise [ArgumentError] Will raise an ArgumentError if currency of n is
293
+ # incompatible.
290
294
  #
291
295
  # @example
292
296
  # Money.new(1) + Money.new(2) #=> #<Money:... @cents: 3>
@@ -296,7 +300,12 @@ class Money
296
300
  n.currency == currency
297
301
  )
298
302
 
299
- Money.new(self.cents + n.cents, :as => :cents)
303
+ Money.new(
304
+ self.cents + n.cents,
305
+ :as => :cents,
306
+ :rounding_method => rounding_method,
307
+ :currency => currency
308
+ )
300
309
  end
301
310
 
302
311
  ##
@@ -308,6 +317,8 @@ class Money
308
317
  #
309
318
  # @raise [ArgumentError] Will raise an ArgumentError unless n is a Money
310
319
  # object.
320
+ # @raise [ArgumentError] Will raise an ArgumentError if currency of n is
321
+ # incompatible.
311
322
  #
312
323
  # @example
313
324
  # Money.new(2) - Money.new(1) #=> #<Money:... @cents: 1>
@@ -317,7 +328,12 @@ class Money
317
328
  n.currency == currency
318
329
  )
319
330
 
320
- Money.new(self.cents - n.cents, :as => :cents)
331
+ Money.new(
332
+ self.cents - n.cents,
333
+ :as => :cents,
334
+ :rounding_method => rounding_method,
335
+ :currency => currency
336
+ )
321
337
  end
322
338
 
323
339
  ##
@@ -338,7 +354,9 @@ class Money
338
354
 
339
355
  Money.new(
340
356
  BigDecimal(self.cents.to_s) * BigDecimal(n.to_s),
341
- :as => :cents
357
+ :as => :cents,
358
+ :rounding_method => rounding_method,
359
+ :currency => currency
342
360
  )
343
361
  end
344
362
 
@@ -352,10 +370,12 @@ class Money
352
370
  #
353
371
  # @raise [ArgumentError] Will raise an ArgumentError unless n is a Money
354
372
  # object or Numeric.
373
+ # @raise [ArgumentError] Will raise an ArgumentError if currency of n is
374
+ # incompatible.
355
375
  #
356
376
  # @example
357
377
  # Money.new(10) / Money.new(5) #=> 2
358
- # Money.new(10) / #=> #<Money:... @cents: 2>
378
+ # Money.new(10) / 5 #=> #<Money:... @cents: 2>
359
379
  def /(n)
360
380
  case n
361
381
  when Money
@@ -367,12 +387,153 @@ class Money
367
387
  when Numeric
368
388
  result, overflow = BigDecimal(self.cents.to_s).divmod(BigDecimal(n.to_s))
369
389
  self.class.overflow = self.class.overflow + overflow
370
- Money.new(result, :as => :cents)
390
+ Money.new(
391
+ result,
392
+ :as => :cents,
393
+ :rounding_method => rounding_method,
394
+ :currency => currency
395
+ )
396
+ else
397
+ raise ArgumentError, "n must be a Money or Numeric"
398
+ end
399
+ end
400
+
401
+ ##
402
+ # Modulo self by a Money/Numeric; return the results as a new Numeric/Money.
403
+ #
404
+ # @param [Money,Numeric] n The object to modulo. If n is Numeric, it will be
405
+ # coerced to a BigDecimal before any calculations are done.
406
+ #
407
+ # @return [Numeric,Money]
408
+ #
409
+ # @raise [ArgumentError] Will raise an ArgumentError unless n is a Money
410
+ # object or Numeric.
411
+ # @raise [ArgumentError] Will raise an ArgumentError if n is an incompatible
412
+ # currency.
413
+ #
414
+ # @example
415
+ # Money.new(10) % Money.new(5) #=> 2
416
+ # Money.new(10) % 5 #=> #<Money:... @cents: 2>
417
+ def %(n)
418
+ case n
419
+ when Money
420
+ raise ArgumentError, "n is an incompatible currency" unless (
421
+ n.currency == currency
422
+ )
423
+
424
+ BigDecimal(self.cents.to_s) % BigDecimal(n.cents.to_s)
425
+ when Numeric
426
+ Money.new(
427
+ BigDecimal(self.cents.to_s) % BigDecimal(n.to_s),
428
+ :as => :cents,
429
+ :rounding_method => rounding_method,
430
+ :currency => currency
431
+ )
432
+ else
433
+ raise ArgumentError, "n must be a Money or Numeric"
434
+ end
435
+ end
436
+
437
+ def divmod(n)
438
+ case n
439
+ when Money
440
+ raise ArgumentError, "n is an incompatible currency" unless (
441
+ n.currency == currency
442
+ )
443
+
444
+ a, b = BigDecimal(self.cents.to_s).divmod(BigDecimal(n.cents.to_s))
445
+ [
446
+ a,
447
+ Money.new(
448
+ b,
449
+ :as => :cents,
450
+ :rounding_method => rounding_method,
451
+ :currency => currency
452
+ )
453
+ ]
454
+ when Numeric
455
+ a, b = BigDecimal(self.cents.to_s).divmod(BigDecimal(n.to_s))
456
+ [
457
+ Money.new(
458
+ a,
459
+ :as => :cents,
460
+ :rounding_method => rounding_method,
461
+ :currency => currency
462
+ ),
463
+ Money.new(
464
+ b,
465
+ :as => :cents,
466
+ :rounding_method => rounding_method,
467
+ :currency => currency
468
+ )
469
+ ]
371
470
  else
372
471
  raise ArgumentError, "n must be a Money or Numeric"
373
472
  end
374
473
  end
375
474
 
475
+ ##
476
+ # Compare self to n. When self < n, return -1. When self > n, return 1. When
477
+ # self == n, return 0.
478
+ #
479
+ # @param [Money] n The object to compare with.
480
+ #
481
+ # @raise [ArgumentError] Will raise an ArgumentError unless n is a Money
482
+ # object or Numeric.
483
+ # @raise [ArgumentError] Will raise an ArgumentError if n is an incompatible
484
+ # currency.
485
+ #
486
+ # @example
487
+ # Money.new(1_00) <=> Money.new(2_00) #=> -1
488
+ # Money.new(2_00) <=> Money.new(1_00) #=> 1
489
+ # Money.new(1_00) <=> Money.new(1_00) #=> 0
490
+ def <=>(n)
491
+ raise ArgumentError, "n must be a Money" unless n.is_a? Money
492
+ raise ArgumentError, "n is an incompatible currency" unless (
493
+ n.currency == currency
494
+ )
495
+
496
+ self.cents <=> n.cents
497
+ end
498
+
499
+ ##
500
+ # Compare self to n. When self == n, return true, otherwise false.
501
+ #
502
+ # @param [Money] n The object to compare with.
503
+ #
504
+ # @raise [ArgumentError] Will raise an ArgumentError unless n is a Money
505
+ # object or Numeric.
506
+ # @raise [ArgumentError] Will raise an ArgumentError if n is an incompatible
507
+ # currency.
508
+ #
509
+ # @example
510
+ # Money.new(1_00) == Money.new(2_00) #=> false
511
+ # Money.new(2_00) == Money.new(2_00) #=> true
512
+ def ==(n)
513
+ raise ArgumentError, "n must be a Money" unless n.is_a? Money
514
+ raise ArgumentError, "n is an incompatible currency" unless (
515
+ n.currency == currency
516
+ )
517
+
518
+ self.cents == n.cents
519
+ end
520
+
521
+ ##
522
+ # Return a new Money object using the absolute value of cents.
523
+ #
524
+ # @return [Money]
525
+ #
526
+ # @example
527
+ # Money.new(-1_00).abs #=> #<Money:... cents: 100>
528
+ def abs
529
+ Money.new(
530
+ self.cents.abs,
531
+ :as => :cents,
532
+ :rounding_method => rounding_method,
533
+ :currency => currency
534
+ )
535
+ end
536
+
376
537
  ##
377
538
  # Returns cents formatted as a string, based on any options passed.
378
539
  #
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Shane Emmons
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-11 00:00:00 -05:00
17
+ date: 2011-01-12 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency