Sha256: db16fc59e16ae6d8e9ffbe137de83b3f6aaec54686e67a7c68af9b787dedabf6

Contents?: true

Size: 1.45 KB

Versions: 24

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Persistable

    # Defines behavior for $pull and $pullAll operations.
    module Pullable
      extend ActiveSupport::Concern

      # Pull single values from the provided arrays.
      #
      # @example Pull a value from the array.
      #   document.pull(names: "Jeff", levels: 5)
      #
      # @note If duplicate values are found they will all be pulled.
      #
      # @param [ Hash ] pulls The field/value pull pairs.
      #
      # @return [ Document ] The document.
      def pull(pulls)
        prepare_atomic_operation do |ops|
          process_atomic_operations(pulls) do |field, value|
            (send(field) || []).delete(value)
            ops[atomic_attribute_name(field)] = value
          end
          { "$pull" => ops }
        end
      end

      # Pull multiple values from the provided array fields.
      #
      # @example Pull values from the arrays.
      #   document.pull_all(names: [ "Jeff", "Bob" ], levels: [ 5, 6 ])
      #
      # @param [ Hash ] pulls The pull all operations.
      #
      # @return [ Document ] The document.
      def pull_all(pulls)
        prepare_atomic_operation do |ops|
          process_atomic_operations(pulls) do |field, value|
            existing = send(field) || []
            value.each{ |val| existing.delete(val) }
            ops[atomic_attribute_name(field)] = value
          end
          { "$pullAll" => ops }
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
mongoid-8.0.1 lib/mongoid/persistable/pullable.rb
mongoid-7.5.0 lib/mongoid/persistable/pullable.rb
mongoid-7.4.1 lib/mongoid/persistable/pullable.rb
mongoid-7.4.0 lib/mongoid/persistable/pullable.rb