class Economy::Money
Attributes
amount[R]
currency[R]
Public Class Methods
new(amount, currency)
click to toggle source
# File lib/economy/money.rb, line 9 def initialize(amount, currency) if amount.is_a?(BigDecimal) @amount = amount else @amount = BigDecimal(amount.to_s) end @currency = normalize_currency(currency) end
Public Instance Methods
%(value)
click to toggle source
# File lib/economy/money.rb, line 120 def %(value) divmod(value)[1] end
Also aliased as: modulo
*(value)
click to toggle source
# File lib/economy/money.rb, line 84 def *(value) case value when Money amount * value.exchange_to(currency).amount when Numeric Money.new (amount * value), currency else raise "Can't multiply Money by #{value.class.name}" end end
+(other)
click to toggle source
# File lib/economy/money.rb, line 66 def +(other) if other.is_a?(Money) other = other.exchange_to(currency) Money.new (amount + other.amount), currency else raise "Can't add #{other.class.name} to Money" end end
-(other)
click to toggle source
# File lib/economy/money.rb, line 75 def -(other) if other.is_a?(Money) other = other.exchange_to(currency) Money.new (amount - other.amount), currency else raise "Can't subtract #{other.class.name} from Money" end end
-@()
click to toggle source
# File lib/economy/money.rb, line 27 def -@ Money.new -amount, currency end
/(value)
click to toggle source
# File lib/economy/money.rb, line 95 def /(value) case value when Money amount / value.exchange_to(currency).amount when Numeric Money.new (amount / value), currency else raise "Can't divide Money by #{value.class.name}" end end
Also aliased as: div
<=>(other)
click to toggle source
# File lib/economy/money.rb, line 55 def <=>(other) if other.is_a?(Numeric) && other == 0 amount <=> other elsif other.is_a?(Money) other = other.exchange_to(currency) amount <=> other.amount else raise "Can't compare #{other.class.name} with Money" end end
===(other)
click to toggle source
# File lib/economy/money.rb, line 47 def ===(other) if other.is_a?(Money) amount == other.amount && currency == other.currency else raise "Can't compare #{other.class.name} with Money" end end
abs()
click to toggle source
# File lib/economy/money.rb, line 22 def abs Money.new amount.abs, currency end
Also aliased as: magnitude
coerce(other)
click to toggle source
# File lib/economy/money.rb, line 18 def coerce(other) [self, other] end
divmod(value)
click to toggle source
# File lib/economy/money.rb, line 107 def divmod(value) case value when Money quotient, modulo = amount.divmod(value.exchange_to(currency).amount) [quotient, Money.new(modulo, currency)] when Numeric quotient, modulo = amount.divmod(value) [Money.new(quotient, currency), Money.new(modulo, currency)] else raise "Can't divide Money by #{value.class.name}" end end
exchange_to(new_currency)
click to toggle source
# File lib/economy/money.rb, line 136 def exchange_to(new_currency) new_currency = normalize_currency(new_currency) if currency != new_currency if rate = Economy.rate(currency, new_currency) Money.new (amount * BigDecimal(rate)), new_currency else raise "Rate #{currency.iso_code} => #{new_currency.iso_code} not found" end else self end end
negative?()
click to toggle source
# File lib/economy/money.rb, line 35 def negative? amount < 0 end
nonzero?()
click to toggle source
# File lib/economy/money.rb, line 43 def nonzero? amount != 0 end
positive?()
click to toggle source
# File lib/economy/money.rb, line 31 def positive? amount > 0 end
remainder(value)
click to toggle source
# File lib/economy/money.rb, line 125 def remainder(value) case value when Money Money.new amount.remainder(value.exchange_to(currency).amount), currency when Numeric Money.new amount.remainder(value), currency else raise "Can't divide Money by #{value.class.name}" end end
to_json(options={})
click to toggle source
# File lib/economy/money.rb, line 149 def to_json(options={}) "%.#{currency.decimals}f" % amount end
Also aliased as: as_json
to_s(precision=nil)
click to toggle source
# File lib/economy/money.rb, line 154 def to_s(precision=nil) ActiveSupport::NumberHelper.number_to_currency( amount, unit: currency.symbol, precision: (precision || currency.decimals) ) end
zero?()
click to toggle source
# File lib/economy/money.rb, line 39 def zero? amount == 0 end
Private Instance Methods
normalize_currency(value)
click to toggle source
# File lib/economy/money.rb, line 164 def normalize_currency(value) if value.is_a?(Currency) value else Economy.currencies.find value end end