Sha256: 5ce86af6d0567a699c4c2cdcd492855e1e48bdb9a37396e340b8c871a7106d28

Contents?: true

Size: 1.01 KB

Versions: 10

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Persistable

    # Defines behavior for $unset operations.
    module Unsettable
      extend ActiveSupport::Concern

      # Perform an $unset operation on the provided fields and in the
      # values in the document in memory.
      #
      # @example Unset the values.
      #   document.unset(:first_name, :last_name, :middle)
      #
      # @param [ [ String | Symbol | Array<String | Symbol>]... ] *fields
      #   The names of the field(s) to unset.
      #
      # @return [ Document ] The document.
      def unset(*fields)
        prepare_atomic_operation do |ops|
          fields.flatten.each do |field|
            normalized = database_field_name(field)
            if executing_atomically?
              process_attribute normalized, nil
            else
              attributes.delete(normalized)
            end
            ops[atomic_attribute_name(normalized)] = true
          end
          { "$unset" => ops }
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mongoid-8.1.9 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.8 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.7 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.6 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.5 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.4 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.3 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.2 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.1 lib/mongoid/persistable/unsettable.rb
mongoid-8.1.0 lib/mongoid/persistable/unsettable.rb