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
-
#mul(factors) ⇒ Document
Multiply the provided fields by the corresponding values.
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.
# 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