module Mongoid::Persistable::Poppable

Defines behavior for $pop operations.

@since 4.0.0

Public Instance Methods

pop(pops) click to toggle source

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

@example Pop items from an array.

document.pop(aliases: 1)

@example Shift items in the array.

document.pop(aliases: -1)

@example Multiple pops in one call.

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

@param [ Hash ] pops The field/value pop operations.

@return [ Document ] The document.

@since 4.0.0

# File 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