123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::AtomicUpdatePreparer Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/mongoid/atomic_update_preparer.rb

Overview

A singleton class to assist with preparing attributes for atomic updates.

Once the deprecated Hash#__consolidate__ method is removed entirely, these methods may be moved into Mongoid::Contextual::Mongo as private methods.

Class Method Summary

Class Method Details

.mongoize_for(operator, klass, key, value) ⇒ Object (private)

Mongoize for the klass, key and value.

Parameters:

  • operator (String)

    The operator.

  • klass (Class)

    The model class.

  • key (String | Symbol)

    The field key.

  • value (Object)

    The value to mongoize.

Returns:

  • (Object)

    The mongoized value.

[ GitHub ]

  
# File 'lib/mongoid/atomic_update_preparer.rb', line 75

def mongoize_for(operator, klass, key, value)
  field = klass.fields[key.to_s]
  return value unless field

  mongoized = field.mongoize(value)
  if Mongoid::Persistable::LIST_OPERATIONS.include?(operator) && field.resizable? && !value.is_a?(Array)
    return mongoized.first
  end

  mongoized
end

.prepare(attributes, klass) ⇒ Hash

Convert the key/values in the attributes into a hash of atomic updates. Non-operator keys are assumed to use $set operation.

Parameters:

  • klass (Class)

    The model class.

  • attributes (Hash)

    The attributes to convert.

Returns:

  • (Hash)

    The prepared atomic updates.

[ GitHub ]

  
# File 'lib/mongoid/atomic_update_preparer.rb', line 21

def prepare(attributes, klass)
  attributes.each_pair.with_object({}) do |(key, value), atomic_updates|
    key = klass.database_field_name(key.to_s)

    if key.to_s.start_with?('$')
      (atomic_updates[key] ||= {}).update(prepare_operation(klass, key, value))
    else
      (atomic_updates['$set'] ||= {})[key] = mongoize_for(key, klass, key, value)
    end
  end
end

.prepare_operation(klass, key, value) ⇒ Hash (private)

Treats the key as if it were a MongoDB operator and prepares the value accordingly.

Parameters:

  • klass (Class)

    the model class

  • key (String | Symbol)

    the operator

  • value (Hash)

    the operand

Returns:

  • (Hash)

    the prepared value.

[ GitHub ]

  
# File 'lib/mongoid/atomic_update_preparer.rb', line 43

def prepare_operation(klass, key, value)
  value.each_with_object({}) do |(key2, value2), hash|
    key2 = klass.database_field_name(key2)
    hash[key2] = value_for(key, klass, value2)
  end
end

.value_for(operator, klass, value) ⇒ Object (private)

Get the value for the provided operator, klass, key and value.

This is necessary for special cases like $rename, $addToSet and $push.

Parameters:

  • operator (String)

    The operator.

  • klass (Class)

    The model class.

  • value (Object)

    The original value.

Returns:

  • (Object)

    Value prepared for the provided operator.

[ GitHub ]

  
# File 'lib/mongoid/atomic_update_preparer.rb', line 59

def value_for(operator, klass, value)
  case operator
  when '$rename' then value.to_s
  when '$addToSet', '$push' then value.mongoize
  else mongoize_for(operator, klass, operator, value)
  end
end