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 conjunction 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.
-
#conflicting_unsets ⇒ Hash
private
Get the conflicting unset 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(initialize: true) ⇒ Hash
private
Get the $set operations or initialize a new one.
-
#unset_conflict?(field) ⇒ true | false
private
Is the operation going to be a conflict for an $unset?
-
#unset_superseded_by_set?(field) ⇒ Boolean
private
Returns true if the $unset is made redundant by a $set that covers the entire root-level field.
-
#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 conjunction with $push.
# File 'lib/mongoid/atomic/modifiers.rb', line 139
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 14
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 155
def add_to_sets self['$addToSet'] ||= {} end
#conflicting_pulls ⇒ Hash (private)
Get the conflicting pull modifications.
# File 'lib/mongoid/atomic/modifiers.rb', line 214
def conflicting_pulls conflicts['$pullAll'] ||= {} end
#conflicting_pushes ⇒ Hash (private)
Get the conflicting push modifications.
# File 'lib/mongoid/atomic/modifiers.rb', line 224
def conflicting_pushes conflicts['$push'] ||= {} end
#conflicting_sets ⇒ Hash (private)
Get the conflicting set modifications.
# File 'lib/mongoid/atomic/modifiers.rb', line 234
def conflicting_sets conflicts['$set'] ||= {} end
#conflicting_unsets ⇒ Hash (private)
Get the conflicting unset modifications.
# File 'lib/mongoid/atomic/modifiers.rb', line 241
def conflicting_unsets conflicts['$unset'] ||= {} end
#conflicts ⇒ Hash (private)
Get the push operations that would have conflicted with the sets.
# File 'lib/mongoid/atomic/modifiers.rb', line 251
def conflicts self[:conflicts] ||= {} end
#pull(modifications)
Adds pull all modifiers to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 45
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 32
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 291
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 261
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 301
def pulls self['$pull'] ||= {} end
#push(modifications)
Adds push modifiers to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 58
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 190
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 271
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 311
def pushes self['$push'] ||= {} end
#set(modifications)
Adds set operations to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 72
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 167
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 281
def set_fields @set_fields ||= {} end
#sets(initialize: true) ⇒ Hash (private)
Get the $set operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 321
def sets(initialize: true) return self['$set'] unless initialize self['$set'] ||= {} end
#unset(modifications)
Adds unset operations to the modifiers hash.
# File 'lib/mongoid/atomic/modifiers.rb', line 88
def unset(modifications) modifications.each do |field| field = field.to_s if unset_conflict?(field) # If the conflicting $set covers the entire parent field (not just a # sub-path), it writes the complete current state, which already # reflects this unset. Skip the $unset — it's redundant. next if unset_superseded_by_set?(field) conflicting_unsets.update(field => true) else unsets.update(field => true) end end end
#unset_conflict?(field) ⇒ true | false (private)
Is the operation going to be a conflict for an $unset?
# File 'lib/mongoid/atomic/modifiers.rb', line 177
def unset_conflict?(field) key = field.split('.', 2)[0] set_fields.has_key?(key) end
#unset_superseded_by_set?(field) ⇒ Boolean (private)
Returns true if the $unset is made redundant by a $set that covers the entire root-level field. When $set “children” is issued, the current (live) state of the array already includes all pending changes (e.g., embedded association removed), so a $unset for any children.*.x field is unnecessary.
# File 'lib/mongoid/atomic/modifiers.rb', line 203
def unset_superseded_by_set?(field) key = field.split('.', 2).first sets(initialize: false).has_key?(key) end
#unsets ⇒ Hash (private)
Get the $unset operations or initialize a new one.
# File 'lib/mongoid/atomic/modifiers.rb', line 333
def unsets self['$unset'] ||= {} end