Sha256: d9c9176c168a80fe936cbc7f75d7bd02f9177a861637d58c50b9f1ee3494a556

Contents?: true

Size: 1.11 KB

Versions: 24

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Persistable

    # Defines behavior for $inc operations.
    module Incrementable
      extend ActiveSupport::Concern

      # 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.
      #
      # @example Increment the fields.
      #   document.inc(score: 10, place: 1, lives: -10)
      #
      # @param [ Hash ] increments The field/inc increment pairs.
      #
      # @return [ Document ] The document.
      def inc(increments)
        prepare_atomic_operation do |ops|
          process_atomic_operations(increments) do |field, value|
            increment = value.__to_inc__
            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
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
mongoid-8.1.7 lib/mongoid/persistable/incrementable.rb
mongoid-8.1.6 lib/mongoid/persistable/incrementable.rb
mongoid-8.0.8 lib/mongoid/persistable/incrementable.rb
mongoid-8.1.5 lib/mongoid/persistable/incrementable.rb
mongoid-8.1.4 lib/mongoid/persistable/incrementable.rb
mongoid-8.0.7 lib/mongoid/persistable/incrementable.rb
mongoid-8.1.3 lib/mongoid/persistable/incrementable.rb
mongoid-8.1.2 lib/mongoid/persistable/incrementable.rb
mongoid-8.0.6 lib/mongoid/persistable/incrementable.rb
mongoid-7.5.4 lib/mongoid/persistable/incrementable.rb
mongoid-8.1.1 lib/mongoid/persistable/incrementable.rb
mongoid-8.0.5 lib/mongoid/persistable/incrementable.rb
mongoid-8.1.0 lib/mongoid/persistable/incrementable.rb
mongoid-7.5.3 lib/mongoid/persistable/incrementable.rb
mongoid-8.0.4 lib/mongoid/persistable/incrementable.rb
mongoid-8.0.3 lib/mongoid/persistable/incrementable.rb
mongoid-7.5.2 lib/mongoid/persistable/incrementable.rb
mongoid-8.0.2 lib/mongoid/persistable/incrementable.rb
mongoid-7.5.1 lib/mongoid/persistable/incrementable.rb
mongoid-7.4.3 lib/mongoid/persistable/incrementable.rb