class SafeMoney

Constants

ArithmeticError
CoercionError
VERSION

Public Class Methods

[](number)
Alias for: cents
cents(number) click to toggle source
# File lib/safe_money.rb, line 10
def cents number
  return number if number.is_a?(self)
  raise ArgumentError, 'cents must be Numeric' unless number.is_a?(Numeric)
  cents = if number.is_a?(Float)
    raise CoercionError, "#{number} cannot safely be converted into cents" unless number.to_i == number
    number.to_i
  else
    number
  end
  new cents
end
Also aliased as: []
dollars(number) click to toggle source
# File lib/safe_money.rb, line 24
def dollars number
  return number if number.is_a?(self)
  raise ArgumentError, 'dollars must be Numeric' unless number.is_a?(Numeric)
  if number != number.round(2)
    raise CoercionError, "#{number} cannot safely be converted into cents"
  end
  cents = (number * 100.00).round
  new cents
end
new(cents) click to toggle source
# File lib/safe_money.rb, line 38
def initialize cents
  raise ArgumentError, 'cents must be an Integer' unless cents.is_a?(Integer)
  @cents = cents
  freeze
end

Public Instance Methods

*(number) click to toggle source
# File lib/safe_money.rb, line 82
def * number
  self.class.cents( to_f * number )
rescue CoercionError
  raise ArithmeticError, "#{self} cannot be safely multiplied by #{number}"
end
+(number) click to toggle source
# File lib/safe_money.rb, line 70
def + number
  self.class.cents( to_i + number )
rescue CoercionError
  raise ArithmeticError, "#{number} cannot be safely added to #{self}"
end
-(number) click to toggle source
# File lib/safe_money.rb, line 76
def - number
  self.class.cents( to_i - number )
rescue CoercionError
  raise ArithmeticError, "#{number} cannot be safely subtracted from #{self}"
end
-@() click to toggle source
# File lib/safe_money.rb, line 96
def -@
  self.class.cents(-to_i)
end
/(number) click to toggle source
# File lib/safe_money.rb, line 88
def / number
  self.class.cents( to_f / number )
rescue CoercionError
  raise ArithmeticError, "#{self} cannot be safely divided by #{number}"
end
Also aliased as: div
as_json(options=nil) click to toggle source
# File lib/safe_money.rb, line 115
def as_json options=nil
  to_i
end
div(number)
Alias for: /
eql?(other) click to toggle source
# File lib/safe_money.rb, line 56
def eql? other
  other = other.to_i if self.class === other
  to_i.eql?(other)
end
equal?(other) click to toggle source
# File lib/safe_money.rb, line 61
def equal? other
  other = other.to_i if self.class === other
  to_i.equal?(other)
end
inspect() click to toggle source
# File lib/safe_money.rb, line 108
def inspect
  dollars = to_dollars
  "#{'-' if dollars < 0}$#{sprintf('%.2f', dollars.abs)}"
end
Also aliased as: to_s, to_str
negative?() click to toggle source
# File lib/safe_money.rb, line 104
def negative?
  self < 0
end
positive?() click to toggle source
# File lib/safe_money.rb, line 100
def positive?
  self > 0
end
to_dollars() click to toggle source
# File lib/safe_money.rb, line 66
def to_dollars
  (@cents * 0.01).round(2)
end
to_i() click to toggle source
# File lib/safe_money.rb, line 44
def to_i
  @cents
end
to_param() click to toggle source
# File lib/safe_money.rb, line 119
def to_param
  to_i.to_s
end
to_s()
Alias for: inspect
to_str()
Alias for: inspect