模块:Mongoid::Extensions::BigDecimal::ClassMethods

定义于:
lib/mongoid/extensions/big_decimal.rb

实例方法摘要折叠

实例方法详细信息

# demogoize (object) ⇒ BigDecimal | nil

将对象从其 mongo 友好的 Ruby 类型转换为此类型。

参数:

  • 对象 ( Object )

    要去妖化的对象。

返回:

  • ( BigDecimal | nil )

    从对象派生的 BigDecimal 或 nil。



56
57
58
59
60
61
62
63
# File 'lib/mongoid/extensions/big_decimal.rb', line 56

def demogoize(对象)
  return if 对象.空白?
  if 对象.is_a?(BSON::Decimal128)
    对象.to_big_decimal
  elsif 对象.numeric?
    对象.to_d
  end
end

# mongoize (object) ⇒ string | BSON::Decimal128 | BSON::Decimal nil

根据其在数据库中的存储方式,对任意类型的对象进行 Mongoize 操作。

例子:

将对象 Mongoize。

BigDecimal.mongoize(123)

参数:

  • 对象 ( Object )

    要 Mongoize 的对象

返回:

  • ( string | BSON::Decimal128 | nil )

    表示对象的string或 Decimal128 或 nil。 如果 Mongoid.map_big_decimal_to_decimal128 为 false,则为string ,否则为 BSON::Decimal128。



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mongoid/extensions/big_decimal.rb', line 75

def mongoize(对象)
  return if 对象.空白?
  if Mongoid.map_big_decimal_to_decimal 128
    if 对象.is_a?(BSON::Decimal128)
      对象
    elsif 对象.is_a?(BigDecimal)
      BSON::Decimal128.new(对象)
    elsif 对象.numeric?
      BSON::Decimal128.new(对象.to_s)
    elsif !对象.is_a?(字符串)
      对象.try(:to_d)
    end
  else
    if 对象.is_a?(BSON::Decimal128) || 对象.numeric?
      对象.to_s
    elsif !对象.is_a?(字符串)
      对象.try(:to_d)&。to_s
    end
  end
end