Module: Mongoid::Persistable::Incrementable
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/incrementable.rb |
Overview
Defines behavior for $inc operations.
Instance Method Summary
-
#inc(increments) ⇒ Document
Increment the provided fields by the corresponding values.
Instance Method Details
#inc(increments) ⇒ Document
Increment 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 with the provided value.
# File 'lib/mongoid/persistable/incrementable.rb', line 21
def inc(increments) prepare_atomic_operation do |ops| process_atomic_operations(increments) do |field, value| increment = value.is_a?(BigDecimal) ? value.to_f : value current = attributes[field] new_value = (current || 0) + increment process_attribute field, new_value if executing_atomically? attributes[field] = new_value ops[atomic_attribute_name(field)] = increment end { "$inc" => ops } unless ops.empty? end end