Module: Mongoid::Persistable::Multipliable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Mongoid::Persistable
- Defined in:
- lib/mongoid/persistable/multipliable.rb
Overview
Defines behavior for $mul operations.
Instance Method Summary collapse
-
#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.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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 |