class Mongoid::Persistence::Atomic::AddToSet

This class provides the ability to perform an explicit $addToSet modification on a specific field.

Public Instance Methods

operation(modifier) click to toggle source

Get the atomic operation to perform.

@example Get the operation.

add_to_set.operation("$addToSet")

@param [ String ] modifier The modifier to use.

@return [ Hash ] The atomic operation for the field and addition.

@since 2.0.0

# File lib/mongoid/persistence/atomic/add_to_set.rb, line 40
def operation(modifier)
  { modifier => { path => value.is_a?(Array) ? { "$each" => value } : value}}
end
persist() click to toggle source

Sends the atomic $addToSet operation to the database.

@example Persist the new values.

addToSet.persist

@return [ Object ] The new array value.

@since 2.0.0

# File lib/mongoid/persistence/atomic/add_to_set.rb, line 18
def persist
  prepare do
    document[field] = [] unless document[field]
    values = document.send(field)
    Array.wrap(value).each do |val|
      values.push(val) unless values.include?(val)
    end
    execute("$addToSet")
    values
  end
end