class Money

Attributes

cents[R]

Public Class Methods

new(cents) click to toggle source
# File lib/mongoid_money/money.rb, line 28
def initialize(cents)
  @cents = cents.round.to_i
end
new_from_cents(cents) click to toggle source
# File lib/mongoid_money/money.rb, line 24
def self.new_from_cents(cents)
  Money.new cents.round.to_i
end
new_from_dollars(value) click to toggle source
# File lib/mongoid_money/money.rb, line 7
def self.new_from_dollars(value)
  case value
    when Fixnum
      Money.new(value * 100)
    when BigDecimal
      Money.new((value * 100).fix)
    when Float
      Money.new((BigDecimal.new(value.to_s) * 100).fix)
    when Numeric
      Money.new((BigDecimal.new(value.to_s) * 100).fix)
    when String
      Money.new((BigDecimal.new(value.to_s) * 100).fix)
    else
      raise ArgumentError, "#{value} must be a numeric object or a string representation of a number."
  end
end

Public Instance Methods

%(val) click to toggle source
# File lib/mongoid_money/money.rb, line 105
def %(val)
  self.modulo(val)
end
*(value) click to toggle source
# File lib/mongoid_money/money.rb, line 70
def *(value)
  if value.is_a?(Money)
    raise ArgumentError, "Can't multiply a Money by a Money"
  else
    Money.new(cents * value)
  end
end
+(other_money) click to toggle source
# File lib/mongoid_money/money.rb, line 62
def +(other_money)
  Money.new(cents + other_money.cents)
end
-(other_money) click to toggle source
# File lib/mongoid_money/money.rb, line 66
def -(other_money)
  Money.new(cents - other_money.cents)
end
/(value) click to toggle source
# File lib/mongoid_money/money.rb, line 78
def /(value)
  if value.is_a?(Money)
    (cents / BigDecimal.new(value.cents.to_s)).to_f
  else
    Money.new(cents / value)
  end
end
<=>(other_money) click to toggle source
# File lib/mongoid_money/money.rb, line 53
def <=>(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    cents <=> other_money.cents
  else
    raise ArgumentError, "Comparison of #{self.class} with #{other_money.inspect} failed"
  end
end
==(other_money) click to toggle source
# File lib/mongoid_money/money.rb, line 36
def ==(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    cents == other_money.cents
  else
    false
  end
end
abs() click to toggle source
# File lib/mongoid_money/money.rb, line 120
def abs
  Money.new(self.cents.abs)
end
coerce(other) click to toggle source
# File lib/mongoid_money/money.rb, line 156
def coerce(other)
  return other, to_f
end
div(value) click to toggle source
# File lib/mongoid_money/money.rb, line 86
def div(value)
  self / value
end
divmod(val) click to toggle source
# File lib/mongoid_money/money.rb, line 90
def divmod(val)
  if val.is_a?(Money)
    a = self.cents
    b = val.cents
    q, m = a.divmod(b)
    return [q, Money.new(m)]
  else
    return [self.div(val), Money.new(self.cents.modulo(val))]
  end
end
dollars() click to toggle source
# File lib/mongoid_money/money.rb, line 32
def dollars
  (BigDecimal.new(cents.to_s) / 100 ).to_f
end
eql?(other_money) click to toggle source
# File lib/mongoid_money/money.rb, line 45
def eql?(other_money)
  self == other_money
end
even?() click to toggle source
# File lib/mongoid_money/money.rb, line 144
def even?
  @cents%2 == 0
end
hash() click to toggle source
# File lib/mongoid_money/money.rb, line 49
def hash
  cents.hash
end
inspect() click to toggle source
# File lib/mongoid_money/money.rb, line 136
def inspect
  to_s
end
modulo(val) click to toggle source
# File lib/mongoid_money/money.rb, line 101
def modulo(val)
  self.divmod(val)[1]
end
nonzero?() click to toggle source
# File lib/mongoid_money/money.rb, line 128
def nonzero?
  cents != 0 ? self : nil
end
odd?() click to toggle source
# File lib/mongoid_money/money.rb, line 140
def odd?
  @cents%2 > 0
end
remainder(val) click to toggle source
# File lib/mongoid_money/money.rb, line 109
def remainder(val)
  a, b = self, val

  a_sign, b_sign = :pos, :pos
  a_sign = :neg if a.cents < 0
  b_sign = :neg if (b.is_a?(Money) and b.cents < 0) or (b < 0)

  return a.modulo(b) if a_sign == b_sign
  a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b))
end
to_f() click to toggle source
# File lib/mongoid_money/money.rb, line 152
def to_f
  dollars
end
to_money() click to toggle source
# File lib/mongoid_money/money.rb, line 132
def to_money
  self
end
to_s() click to toggle source
# File lib/mongoid_money/money.rb, line 148
def to_s
  dollars.to_s
end
zero?() click to toggle source
# File lib/mongoid_money/money.rb, line 124
def zero?
  cents == 0
end