Module: Mongoid::Persistable::Poppable

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Persistable
Defined in:
build/mongoid-7.3/lib/mongoid/persistable/poppable.rb

Overview

Defines behavior for $pop operations.

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#pop(pops) ⇒ Document

Pop or shift items from arrays using the $pop operator.

Examples:

Pop items from an array.

document.pop(aliases: 1)

Shift items in the array.

document.pop(aliases: -1)

Multiple pops in one call.

document.pop(names: 1, aliases: 1)

Parameters:

  • pops (Hash)

    The field/value pop operations.

Returns:

Since:

  • 4.0.0



29
30
31
32
33
34
35
36
37
38
# File 'build/mongoid-7.3/lib/mongoid/persistable/poppable.rb', line 29

def pop(pops)
  prepare_atomic_operation do |ops|
    process_atomic_operations(pops) do |field, value|
      values = send(field)
      value > 0 ? values.pop : values.shift
      ops[atomic_attribute_name(field)] = value
    end
    { "$pop" => ops }
  end
end