Sha256: c373a6d0711dc9c00ad1a7eb18e085a7257cb473f0710e4ff990e328fb14c1f0

Contents?: true

Size: 1.52 KB

Versions: 59

Compression:

Stored size: 1.52 KB

Contents

# encoding: utf-8
module Mongoid
  module Persistable

    # Defines behaviour 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

59 entries across 55 versions & 3 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/persistable/pullable.rb
mongoid-7.0.12 lib/mongoid/persistable/pullable.rb
mongoid-6.4.8 lib/mongoid/persistable/pullable.rb
mongoid-7.0.11 lib/mongoid/persistable/pullable.rb
mongoid-7.0.10 lib/mongoid/persistable/pullable.rb
mongoid-6.4.7 lib/mongoid/persistable/pullable.rb
mongoid-6.4.5 lib/mongoid/persistable/pullable.rb
mongoid-7.0.8 lib/mongoid/persistable/pullable.rb
mongoid-7.0.7 lib/mongoid/persistable/pullable.rb
mongoid-7.0.6 lib/mongoid/persistable/pullable.rb
mongoid-5.4.1 lib/mongoid/persistable/pullable.rb
mongoid-7.0.5 lib/mongoid/persistable/pullable.rb
mongoid-7.0.4 lib/mongoid/persistable/pullable.rb
mongoid-6.4.4 lib/mongoid/persistable/pullable.rb
mongoid-7.0.3 lib/mongoid/persistable/pullable.rb
mongoid-6.4.2 lib/mongoid/persistable/pullable.rb
mongoid-7.0.2 lib/mongoid/persistable/pullable.rb
mongoid-7.0.1 lib/mongoid/persistable/pullable.rb
mongoid-6.4.1 lib/mongoid/persistable/pullable.rb
mongoid-5.4.0 lib/mongoid/persistable/pullable.rb