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

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

Overview

This class provides atomic $inc behaviour.

Instance Attribute Summary

Attributes inherited from Operation

#document, #field, #options, #value

Instance Method Summary collapse

Methods inherited from Operation

#initialize, #operation

Methods included from Safe

#safe_mode?

Constructor Details

This class inherits a constructor from Mongoid::Persistence::Atomic::Operation

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



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

def persist
  current = document[field] || 0
  document[field] = current + value
  document[field].tap do
    document.collection.update(document._selector, operation("$inc"), options)
    document.changes.delete(field.to_s)
  end
end