Class: Mongoid::Atomic::Modifiers
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
Hash
|
Defined in: | lib/mongoid/atomic/modifiers.rb |
Overview
This class contains the logic for supporting atomic operations against the database.
Class Attribute Summary
::Mongoid::Extensions::Hash::ClassMethods
- Extended
resizable? | Can the size of this object change? |
Class Method Summary
::Mongoid::Extensions::Hash::ClassMethods
- Extended
mongoize | Turn the object from the ruby type we deal with to a Mongo friendly type. |
Instance Attribute Summary
::Mongoid::Extensions::Hash
- Included
#resizable? | Can the size of this object change? |
Instance Method Summary
-
#add_to_set(modifications)
Add the atomic $addToSet modifiers to the hash.
-
#pull(modifications)
Adds pull all modifiers to the modifiers hash.
-
#pull_all(modifications)
Adds pull all modifiers to the modifiers hash.
-
#push(modifications)
Adds push modifiers to the modifiers hash.
-
#set(modifications)
Adds set operations to the modifiers hash.
-
#unset(modifications)
Adds unset operations to the modifiers hash.
-
#add_each_operation(mods, field, value)
private
Adds or appends an array operation with the $each specifier used in conjuction with $push.
-
#add_operation(mods, field, value)
private
Add the operation to the modifications, either appending or creating a new one.
-
#add_to_sets ⇒ Hash
private
Get the $addToSet operations or initialize a new one.
-
#conflicting_pulls ⇒ Hash
private
Get the conflicting pull modifications.
-
#conflicting_pushes ⇒ Hash
private
Get the conflicting push modifications.
-
#conflicting_sets ⇒ Hash
private
Get the conflicting set modifications.
-
#conflicts ⇒ Hash
private
Get the push operations that would have conflicted with the sets.
-
#pull_alls ⇒ Hash
private
Get the $pullAll operations or initialize a new one.
-
#pull_fields ⇒ Array<String>
private
Get the names of the fields that need to be pulled.
-
#pulls ⇒ Hash
private
Get the $pull operations or initialize a new one.
-
#push_conflict?(field) ⇒ true | false
private
Is the operation going to be a conflict for a $push?
-
#push_fields ⇒ Array<String>
private
Get the names of the fields that need to be pushed.
-
#pushes ⇒ Hash
private
Get the $push/$each operations or initialize a new one.
-
#set_conflict?(field) ⇒ true | false
private
Is the operation going to be a conflict for a $set?
-
#set_fields ⇒ Array<String>
private
Get the names of the fields that need to be set.
-
#sets ⇒ Hash
private
Get the $set operations or initialize a new one.
-
#unsets ⇒ Hash
private
Get the $unset operations or initialize a new one.
::Mongoid::Extensions::Hash
- Included
#__consolidate__ | Consolidate the key/values in the hash under an atomic $set. |
#__evolve_object_id__ | Evolves each value in the hash to an object id if it is convertable. |
#__mongoize_object_id__ | Mongoizes each value in the hash to an object id if it is convertable. |
#delete_id | Deletes an id value from the hash. |
#extract_id | Get the id attribute from this hash, whether it’s prefixed with an underscore or is a symbol. |
#mongoize | Turn the object from the ruby type we deal with to a Mongo friendly type. |
#to_criteria | Convert this hash to a criteria. |
Instance Method Details
#add_each_operation(mods, field, value) (private)
Adds or appends an array operation with the $each specifier used in conjuction with $push.
# File 'lib/mongoid/atomic/modifiers.rb', line 130
def add_each_operation(mods, field, value) if mods.has_key?(field) value.each do |val| mods[field]["$each"].push(val) end else mods[field] = { "$each" => value } end end
#add_operation(mods, field, value) (private)
Add the operation to the modifications, either appending or creating a new one.
#add_to_set(modifications)
Add the atomic $addToSet modifiers to the hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 17
def add_to_set(modifications) modifications.each_pair do |field, value| if add_to_sets.has_key?(field) value.each do |val| add_to_sets[field]["$each"].push(val) end else add_to_sets[field] = { "$each" => value } end end end
#add_to_sets ⇒ Hash (private)
Get the $addToSet operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 146
def add_to_sets self["$addToSet"] ||= {} end
#conflicting_pulls ⇒ Hash (private)
Get the conflicting pull modifications.
# File 'lib/mongoid/atomic/modifiers.rb', line 183
def conflicting_pulls conflicts["$pullAll"] ||= {} end
#conflicting_pushes ⇒ Hash (private)
Get the conflicting push modifications.
# File 'lib/mongoid/atomic/modifiers.rb', line 193
def conflicting_pushes conflicts["$push"] ||= {} end
#conflicting_sets ⇒ Hash (private)
Get the conflicting set modifications.
# File 'lib/mongoid/atomic/modifiers.rb', line 203
def conflicting_sets conflicts["$set"] ||= {} end
#conflicts ⇒ Hash (private)
Get the push operations that would have conflicted with the sets.
# File 'lib/mongoid/atomic/modifiers.rb', line 213
def conflicts self[:conflicts] ||= {} end
#pull(modifications)
Adds pull all modifiers to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 48
def pull(modifications) modifications.each_pair do |field, value| pulls[field] = value pull_fields[field.split(".", 2)[0]] = field end end
#pull_all(modifications)
Adds pull all modifiers to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 35
def pull_all(modifications) modifications.each_pair do |field, value| add_operation(pull_alls, field, value) pull_fields[field.split(".", 2)[0]] = field end end
#pull_alls ⇒ Hash (private)
Get the $pullAll operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 253
def pull_alls self["$pullAll"] ||= {} end
#pull_fields ⇒ Array<String> (private)
Get the names of the fields that need to be pulled.
# File 'lib/mongoid/atomic/modifiers.rb', line 223
def pull_fields @pull_fields ||= {} end
#pulls ⇒ Hash (private)
Get the $pull operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 263
def pulls self["$pull"] ||= {} end
#push(modifications)
Adds push modifiers to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 61
def push(modifications) modifications.each_pair do |field, value| push_fields[field] = field mods = push_conflict?(field) ? conflicting_pushes : pushes add_operation(mods, field, { '$each' => Array.wrap(value) }) end end
#push_conflict?(field) ⇒ true
| false
(private)
Is the operation going to be a conflict for a $push?
# File 'lib/mongoid/atomic/modifiers.rb', line 171
def push_conflict?(field) name = field.split(".", 2)[0] set_fields.has_key?(name) || pull_fields.has_key?(name) || (push_fields.keys.count { |item| item.split('.', 2).first == name } > 1) end
#push_fields ⇒ Array<String> (private)
Get the names of the fields that need to be pushed.
# File 'lib/mongoid/atomic/modifiers.rb', line 233
def push_fields @push_fields ||= {} end
#pushes ⇒ Hash (private)
Get the $push/$each operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 273
def pushes self["$push"] ||= {} end
#set(modifications)
Adds set operations to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 75
def set(modifications) modifications.each_pair do |field, value| next if field == "_id" mods = set_conflict?(field) ? conflicting_sets : sets add_operation(mods, field, value) set_fields[field.split(".", 2)[0]] = field end end
#set_conflict?(field) ⇒ true
| false
(private)
Is the operation going to be a conflict for a $set?
# File 'lib/mongoid/atomic/modifiers.rb', line 158
def set_conflict?(field) name = field.split(".", 2)[0] pull_fields.has_key?(name) || push_fields.has_key?(name) end
#set_fields ⇒ Array<String> (private)
Get the names of the fields that need to be set.
# File 'lib/mongoid/atomic/modifiers.rb', line 243
def set_fields @set_fields ||= {} end
#sets ⇒ Hash (private)
Get the $set operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 283
def sets self["$set"] ||= {} end
#unset(modifications)
Adds unset operations to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 90
def unset(modifications) modifications.each do |field| unsets.update(field => true) end end
#unsets ⇒ Hash (private)
Get the $unset operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 293
def unsets self["$unset"] ||= {} end