Module: Mongoid::Criteria::Queryable::Extensions::BigDecimal::ClassMethods

Defined in:
build/mongoid-master/lib/mongoid/criteria/queryable/extensions/big_decimal.rb

Instance Method Summary collapse

Instance Method Details

#evolve(object) ⇒ Object

Evolves the big decimal into a MongoDB friendly value.

Examples:

Evolve the big decimal

BigDecimal.evolve(decimal)

Parameters:

Returns:

  • (Object)

    The big decimal as a string, a Decimal128, or the inputted object if it is uncastable.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'build/mongoid-master/lib/mongoid/criteria/queryable/extensions/big_decimal.rb', line 24

def evolve(object)
  __evolve__(object) do |obj|
    return if obj.nil?
    case obj
    when ::BigDecimal
      if Mongoid.map_big_decimal_to_decimal128
        BSON::Decimal128.new(obj)
      else
        obj.to_s
      end
    # Always return on string for backwards compatibility with querying
    # string-backed BigDecimal fields.
    when BSON::Decimal128, String then obj
    else
      if obj.numeric?
        if Mongoid.map_big_decimal_to_decimal128
          BSON::Decimal128.new(object.to_s)
        else
          obj.to_s
        end
      else
        obj
      end
    end
  end
end