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

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

Instance Method Summary collapse

Instance Method Details

#demongoize(object) ⇒ BigDecimal | nil

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

Parameters:

  • object (Object)

    The object to demongoize.

Returns:

  • (BigDecimal | nil)

    A BigDecimal derived from the object or nil.



45
46
47
48
49
50
51
52
53
54
# File 'build/mongoid-8.1/lib/mongoid/extensions/big_decimal.rb', line 45

def demongoize(object)
  return if object.blank?
  if object.is_a?(BSON::Decimal128)
    object.to_big_decimal
  elsif object.numeric?
    BigDecimal(object.to_s)
  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.

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. String if Mongoid.map_big_decimal_to_decimal128 is false, BSON::Decimal128 otherwise.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'build/mongoid-8.1/lib/mongoid/extensions/big_decimal.rb', line 66

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)
    end
  end
end