Class: Mongoid::Persistence::Atomic::Inc

Inherits:
Object
  • Object
show all
Includes:
Operation
Defined in:
lib/mongoid/persistence/atomic/inc.rb

Overview

This class provides atomic $inc behaviour.

Instance Attribute Summary

Attributes included from Operation

#document, #field, #options, #value

Instance Method Summary collapse

Methods included from Operation

#collection, #initialize, #operation, #path, #prepare

Instance Method Details

#persistObject

Sends the atomic $inc operation to the database.

Examples:

Persist the new values.

inc.persist

Returns:

  • (Object)

    The new integer value.

Since:

  • 2.0.0



18
19
20
21
22
23
24
25
26
27
# File 'lib/mongoid/persistence/atomic/inc.rb', line 18

def persist
  prepare do
    current = document[field] || 0
    document[field] = current + value
    document[field].tap do
      collection.update(document.atomic_selector, operation("$inc"), options)
      document.remove_change(field)
    end
  end
end