Module: Mongoid::Extensions::BigDecimal::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#demongoize(object) ⇒ BigDecimal?

Convert the object from its mongo friendly ruby type to this type.

Examples:

Demongoize the object.

Object.demongoize(object)

Parameters:

  • object (Object)

    The object to demongoize.

Returns:

  • (BigDecimal, nil)

    A BigDecimal derived from the object or nil.



52
53
54
55
56
57
58
59
60
# File 'build/mongoid-master/lib/mongoid/extensions/big_decimal.rb', line 52

def demongoize(object)
  unless object.nil?
    if object.is_a?(BSON::Decimal128)
      object.to_big_decimal
    elsif object.numeric?
      BigDecimal(object.to_s)
    end
  end
end

#mongoize(object) ⇒ String | BSON::Decimal128 | nil

Mongoize an object of any type to how it's stored in the db.

Examples:

Mongoize the object.

BigDecimal.mongoize(123)

Parameters:

  • object (Object)

    The object to Mongoize

Returns:

  • (String | BSON::Decimal128 | nil)

    A String or Decimal128 representing the object or nil.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'build/mongoid-master/lib/mongoid/extensions/big_decimal.rb', line 71

def mongoize(object)
  unless object.nil?
    if object.is_a?(BSON::Decimal128)
      object
    elsif Mongoid.map_big_decimal_to_decimal128
      if object.is_a?(BigDecimal)
        BSON::Decimal128.new(object)
      elsif object.numeric?
        BSON::Decimal128.new(object.to_s)
      else
        object.mongoize
      end
    elsif object.numeric?
      object.to_s
    end
  end
end