Sha256: 248ac153bd95874dbcb9bf5e7d0852201e58282cf6f411609716a80e9db27efe

Contents?: true

Size: 934 Bytes

Versions: 4

Compression:

Stored size: 934 Bytes

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Persistence #:nodoc:
    module Atomic #:nodoc:

      # This class provides the ability to perform an explicit $addToSet
      # modification on a specific field.
      class AddToSet < Operation

        # Sends the atomic $addToSet operation to the database.
        #
        # @example Persist the new values.
        #   addToSet.persist
        #
        # @return [ Object ] The new array value.
        #
        # @since 2.0.0
        def persist
          document[field] = [] unless document[field]
          values = document.send(field)
          values.push(value) unless values.include?(value)
          values.tap do
            if document.persisted?
              document.collection.update(document._selector, operation("$addToSet"), options)
              document.changes.delete(field.to_s)
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
mongoid-braxton-2.0.2 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.0.2 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.0.1 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.0.0 lib/mongoid/persistence/atomic/add_to_set.rb