module Mongoize::ClassMethods

Public Instance Methods

custom_between(field, value, options = {}) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 40
def custom_between(field, value, options = {})
  { "$gte" => ::Money.new(value.min, value.iso_code), "$lte" => ::Money.new(value.max, value.iso_code) }
end
custom_between?(field, value) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 44
def custom_between? field, value
  true
end
custom_serialization?(operator) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 20
def custom_serialization?(operator)
  return false unless operator
  case operator
    when "$gte", "$gt", "$lt", "$lte"
      true
  else
    false
  end
end
custom_specify(name, operator, value, options = {}) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 30
def custom_specify(name, operator, value, options = {})
  money = value.__evolve_to_money__
  case operator
    when "$gte", "$gt", "$lt", "$lte"
      specify_with_multiple_currencies(name, operator, money, options)
  else
    raise RuntimeError, "Unsupported operator"
  end
end
demongoize(value) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 12
def demongoize(value)
  value && ::Money.new( get_cents(value), get_currency(value) )
end
evolve(object) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 16
def evolve(object)
  object && object.__evolve_to_money__.mongoize
end

Private Instance Methods

get_cents(value) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 66
def get_cents value
  value[:cents] || value['cents']
end
get_currency(value) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 70
def get_currency value
  value[:currency_iso] || value['currency_iso']
end
specify_with_multiple_currencies(name, operator, value, options) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 50
def specify_with_multiple_currencies(name, operator, value, options)
  currencies = [value.currency.iso_code]
  currencies.concat options[:compare_using] if options && options[:compare_using]
  multiple_money_values = value.exchange_to_currencies(currencies.to_set)
  subconditions = multiple_money_values.collect {|money| specify_with_single_currency(name, operator, money)}
  if subconditions.count > 0
    {"$or" => subconditions}
  else
    subconditions[0]
  end
end
specify_with_single_currency(name, operator, value) click to toggle source
# File lib/money/mongoid/3x/money.rb, line 62
def specify_with_single_currency(name, operator, value)
  {"#{name}.cents" => {operator => value.cents}, "#{name}.currency_iso" => value.currency.iso_code}
end