Sha256: 587673fcdb1cf76b217036ce8e3a4e770a7336edafd9aa65ac95ab2be435899f

Contents?: true

Size: 992 Bytes

Versions: 24

Compression:

Stored size: 992 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
        include 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
          prepare do
            document[field] = [] unless document[field]
            values = document.send(field)
            values.push(value) unless values.include?(value)
            values.tap do
              if document.persisted?
                collection.update(document.atomic_selector, operation("$addToSet"), options)
                document.remove_change(field)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
mongoid-multi-db-3.0.0 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.2.6 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.3.5 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.2.5 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.3.4 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.2.4 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.3.3 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.3.2 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.2.3 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.3.1 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.3.0 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.2.2 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.2.1 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.2.0 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.1.9 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.1.8 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.1.7 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.1.6 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.1.5 lib/mongoid/persistence/atomic/add_to_set.rb
mongoid-2.1.4 lib/mongoid/persistence/atomic/add_to_set.rb