module Money::Arithmetic
def %(val)
- See: #modulo -
Returns:
-
(Money)
-
Parameters:
-
val
(Money, Integer
) -- Number take modulo with.
def %(val) modulo(val) end
def *(value)
-
(TypeError)
- If +value+ is NOT a number.
Returns:
-
(Money)
- The resulting money.
Parameters:
-
value
(Numeric
) -- Number to multiply by.
def *(value) value = value.value if value.is_a?(CoercedNumeric) if value.is_a? Numeric dup_with(fractional: fractional * value) else raise TypeError, "Can't multiply a #{self.class.name} by a #{value.class.name}'s value" end end
def -@
-
(Money)
-
def -@ dup_with(fractional: -fractional) end
def /(value)
-
(Float)
- The resulting number if you divide Money by a Money. -
(Money)
- The resulting money if you divide Money by a number.
Parameters:
-
value
(Money, Numeric
) -- Number to divide by.
def /(value) if value.is_a?(self.class) fractional / as_d(value.exchange_to(currency).fractional).to_f else raise TypeError, 'Can not divide by Money' if value.is_a?(CoercedNumeric) dup_with(fractional: fractional / as_d(value)) end end
def <=>(other)
-
(TypeError)
- when other object is not Money
Returns:
-
(Integer)
-
Parameters:
-
other
(Money
) -- Value to compare with.
def <=>(other) unless other.is_a?(Money) return unless other.respond_to?(:zero?) && other.zero? return other.is_a?(CoercedNumeric) ? 0 <=> fractional : fractional <=> 0 end # Always allow comparison with zero if zero? || other.zero? return fractional <=> other.fractional end other = other.exchange_to(currency) fractional <=> other.fractional rescue Money::Bank::UnknownRate end
def ==(other)
Uses Comparable's implementation but raises ArgumentError if non-zero
def ==(other) if other.is_a?(Numeric) && !other.zero? raise ArgumentError, 'Money#== supports only zero numerics' end super end
def abs
-
(Money)
-
def abs dup_with(fractional: fractional.abs) end
def coerce(other)
-
(Array)
-
def coerce(other) [self, CoercedNumeric.new(other)] end
def div(value)
- See: #/ -
Returns:
-
(Float)
- The resulting number if you divide Money by a Money. -
(Money)
- The resulting money if you divide Money by a number.
Parameters:
-
value
(Money, Numeric
) -- Number to divide by.
def div(value) self / value end
def divmod(val)
-
(Array
-, Array )
Parameters:
-
val
(Money, Integer
) -- Number to divmod by.
def divmod(val) if val.is_a?(Money) divmod_money(val) else divmod_other(val) end end
def divmod_money(val)
def divmod_money(val) cents = val.exchange_to(currency).cents quotient, remainder = fractional.divmod(cents) [quotient, dup_with(fractional: remainder)] end
def divmod_other(val)
def divmod_other(val) quotient, remainder = fractional.divmod(as_d(val)) [dup_with(fractional: quotient), dup_with(fractional: remainder)] end
def eql?(other_money)
-
(Boolean)
-
Parameters:
-
other_money
(Money
) -- Value to compare with.
def eql?(other_money) if other_money.is_a?(Money) (fractional == other_money.fractional && currency == other_money.currency) || (fractional == 0 && other_money.fractional == 0) else false end end
def modulo(val)
-
(Money)
-
Parameters:
-
val
(Money, Integer
) -- Number take modulo with.
def modulo(val) divmod(val)[1] end
def negative?
-
(Boolean)
-
def negative? fractional < 0 end
def nonzero?
-
(Money, nil)
-
def nonzero? fractional != 0 ? self : nil end
def positive?
-
(Boolean)
-
def positive? fractional > 0 end
def remainder(val)
-
(Money)
-
Parameters:
-
val
(Money, Integer
) -- Number to rake remainder with.
def remainder(val) if val.is_a?(Money) && currency != val.currency val = val.exchange_to(currency) end if (fractional < 0 && val < 0) || (fractional > 0 && val > 0) self.modulo(val) else self.modulo(val) - (val.is_a?(Money) ? val : dup_with(fractional: val)) end end
def zero?
-
(Boolean)
-
def zero? fractional == 0 end