Module: Mongoid::Extensions::BigDecimal::ClassMethods
Relationships & Source Files | |
Defined in: | lib/mongoid/extensions/big_decimal.rb |
Instance Method Summary
-
#demongoize(object) ⇒ BigDecimal | nil
Convert the object from its mongo friendly ruby type to this type.
-
#mongoize(object) ⇒ String | BSON::Decimal128 | nil
Mongoize an object of any type to how it’s stored in the db.
Instance Method Details
#demongoize(object) ⇒ BigDecimal | nil
Convert the object from its mongo friendly ruby type to this type.
# File 'lib/mongoid/extensions/big_decimal.rb', line 56
def demongoize(object) return if object.blank? if object.is_a?(BSON::Decimal128) object.to_big_decimal elsif object.numeric? object.to_d end end
#mongoize(object) ⇒ String | BSON::Decimal128 | nil
Mongoize an object of any type to how it’s stored in the db.
# File 'lib/mongoid/extensions/big_decimal.rb', line 75
def mongoize(object) return if object.blank? if Mongoid.map_big_decimal_to_decimal128 if object.is_a?(BSON::Decimal128) object elsif object.is_a?(BigDecimal) BSON::Decimal128.new(object) elsif object.numeric? BSON::Decimal128.new(object.to_s) elsif !object.is_a?(String) object.try(:to_d) end else if object.is_a?(BSON::Decimal128) || object.numeric? object.to_s elsif !object.is_a?(String) object.try(:to_d)&.to_s end end end