模块:Mongoid::Persistable::Multipliable

扩展方式:
ActiveSupport::Concern
包含在:
Mongoid::Persistable
定义于:
lib/mongoid/persistable/multipliable.rb

Overview

定义 $mul 操作的行为。

实例方法摘要折叠

实例方法详细信息

# mul (factors) ⇒文档

将提供的字段乘以相应的值。 值可以是正数或负数,如果该字段不存在值,则该值将设立为零。

例子:

将字段相乘。

document.mul(score: 10, place: 1, lives: -10)

参数:

  • 因素 (哈希)

    字段/因子乘数对。

返回:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/persistable/multipliable.rb', line 21

def mul(因素)
  prepare_atomic_operation do |运维|
    process_atomic_operations(因素) do |字段, |
      因素 = .is_a?(BigDecimal) ? .to_f : 
      Current = 属性[字段]
      new_value = (Current || 0) * 因素
      process_attribute 字段, new_value if Executing_atomically?
      属性[字段] = new_value
      运维[atomic_attribute_name(字段)] = 因素
    end
    { "$mul" => 运维 } 除非 运维.空?
  end
end