123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Persistable::Multipliable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/persistable/multipliable.rb

Overview

Defines behavior for $mul operations.

Instance Method Summary

Instance Method Details

#mul(factors) ⇒ Document

Multiply the provided fields by the corresponding values. Values can be positive or negative, and if no value exists for the field it will be set to zero.

Examples:

Multiply the fields.

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

Parameters:

  • factors (Hash)

    The field/factor multiplier pairs.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/persistable/multipliable.rb', line 21

def mul(factors)
  prepare_atomic_operation do |ops|
    process_atomic_operations(factors) do |field, value|
      factor = value.is_a?(BigDecimal) ? value.to_f : value
      current = attributes[field]
      new_value = (current || 0) * factor
      process_attribute field, new_value if executing_atomically?
      attributes[field] = new_value
      ops[atomic_attribute_name(field)] = factor
    end
    { "$mul" => ops } unless ops.empty?
  end
end