Sha256: d93bade1065e63c2d1bce875cd73ad1bb1b53b80af6a1641f1e1736ae5d4f183

Contents?: true

Size: 1.55 KB

Versions: 27

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  module Persistable

    # Defines behavior for $pull and $pullAll operations.
    #
    # @since 4.0.0
    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.
      #
      # @since 4.0.0
      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.
      #
      # @since 4.0.0
      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

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/persistable/pullable.rb
mongoid-7.3.4 lib/mongoid/persistable/pullable.rb
mongoid-7.1.11 lib/mongoid/persistable/pullable.rb
mongoid-7.2.6 lib/mongoid/persistable/pullable.rb
mongoid-7.3.3 lib/mongoid/persistable/pullable.rb
mongoid-7.3.2 lib/mongoid/persistable/pullable.rb
mongoid-7.2.5 lib/mongoid/persistable/pullable.rb
mongoid-7.1.10 lib/mongoid/persistable/pullable.rb
mongoid-7.1.9 lib/mongoid/persistable/pullable.rb
mongoid-7.2.4 lib/mongoid/persistable/pullable.rb
mongoid-7.3.1 lib/mongoid/persistable/pullable.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/persistable/pullable.rb
mongoid-7.3.0 lib/mongoid/persistable/pullable.rb
mongoid-7.2.3 lib/mongoid/persistable/pullable.rb
mongoid-7.1.8 lib/mongoid/persistable/pullable.rb
mongoid-7.2.2 lib/mongoid/persistable/pullable.rb
mongoid-7.2.1 lib/mongoid/persistable/pullable.rb
mongoid-7.1.7 lib/mongoid/persistable/pullable.rb
mongoid-7.2.0 lib/mongoid/persistable/pullable.rb
mongoid-7.1.6 lib/mongoid/persistable/pullable.rb