Class: Mongoid::AtomicUpdatePreparer Private
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
-
.prepare(attributes, klass) ⇒ Hash
Internal use only
Convert the key/values in the attributes into a hash of atomic updates.
-
.mongoize_for(operator, klass, key, value) ⇒ Object
private
Internal use only
Mongoize for the klass, key and value.
-
.prepare_operation(klass, key, value) ⇒ Hash
private
Internal use only
Treats the key as if it were a MongoDB operator and prepares the value accordingly.
-
.value_for(operator, klass, key, value) ⇒ Object
private
Internal use only
Get the value for the provided operator, klass, key and value.
Class Method Details
.mongoize_for(operator, klass, key, value) ⇒ Object
(private)
Mongoize for the klass, key and value.
# File 'lib/mongoid/atomic_update_preparer.rb', line 76
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.
# 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('$set', 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.
# 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, key2, value2) end end
.value_for(operator, klass, key, value) ⇒ Object
(private)
Get the value for the provided operator, klass, key and value.
This is necessary for special cases like $rename, $addToSet, $push, $pull and $pop.
# File 'lib/mongoid/atomic_update_preparer.rb', line 60
def value_for(operator, klass, key, value) case operator when '$rename' then value.to_s when '$addToSet', '$push', '$pull', '$pop' then value.mongoize else mongoize_for(operator, klass, key, value) end end