class Mongoid::Atomic::Modifiers
database.
This class contains the logic for supporting atomic operations against the
def add_each_operation(mods, field, value)
- Since: - 7.0.0
Parameters:
-
value(Hash) -- The atomic op. -
field(String) -- The field. -
mods(Hash) -- The modifications.
Other tags:
- Example: Add the operation. -
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
def add_operation(mods, field, value)
- Since: - 2.2.0
Parameters:
-
value(Hash) -- The atomic op. -
field(String) -- The field. -
mods(Hash) -- The modifications.
Other tags:
- Example: Add the operation. -
def add_operation(mods, field, value) if mods.has_key?(field) if mods[field].is_a?(Array) value.each do |val| mods[field].push(val) end elsif mods[field]['$each'] mods[field]['$each'].concat(value['$each']) end else mods[field] = value end end
def add_to_set(modifications)
- Since: - 2.4.0
Parameters:
-
modifications(Hash) -- The add to set modifiers.
Other tags:
- Example: Add the $addToSet modifiers. -
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
def add_to_sets
- Since: - 2.4.0
Returns:
-
(Hash)- The $addToSet operations.
Other tags:
- Example: Get the $addToSet operations. -
def add_to_sets self["$addToSet"] ||= {} end
def conflicting_pulls
- Since: - 2.2.0
Returns:
-
(Hash)- The conflicting pull operations.
Other tags:
- Example: Get the conflicting pulls. -
def conflicting_pulls conflicts["$pullAll"] ||= {} end
def conflicting_pushes
- Since: - 2.2.0
Returns:
-
(Hash)- The conflicting push operations.
Other tags:
- Example: Get the conflicting pushs. -
def conflicting_pushes conflicts["$push"] ||= {} end
def conflicting_sets
- Since: - 2.2.0
Returns:
-
(Hash)- The conflicting set operations.
Other tags:
- Example: Get the conflicting sets. -
def conflicting_sets conflicts["$set"] ||= {} end
def conflicts
- Since: - 2.1.0
Returns:
-
(Hash)- The conflicting modifications.
Other tags:
- Example: Get the conflicts. -
def conflicts self[:conflicts] ||= {} end
def pull(modifications)
- Since: - 3.0.0
Parameters:
-
modifications(Hash) -- The pull all modifiers.
Other tags:
- Example: Add pull all operations. -
def pull(modifications) modifications.each_pair do |field, value| pulls[field] = value pull_fields[field.split(".", 2)[0]] = field end end
def pull_all(modifications)
- Since: - 3.0.0
Parameters:
-
modifications(Hash) -- The pull all modifiers.
Other tags:
- Example: Add pull all operations. -
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
def pull_alls
- Since: - 3.0.0
Returns:
-
(Hash)- The $pullAll operations.
Other tags:
- Example: Get the $pullAll operations. -
def pull_alls self["$pullAll"] ||= {} end
def pull_fields
- Since: - 2.2.0
Returns:
-
(Array- The pull fields.)
Other tags:
- Example: Get the pull fields. -
def pull_fields @pull_fields ||= {} end
def pulls
- Since: - 3.0.0
Returns:
-
(Hash)- The $pull operations.
Other tags:
- Example: Get the $pull operations. -
def pulls self["$pull"] ||= {} end
def push(modifications)
- Since: - 2.1.0
Parameters:
-
modifications(Hash) -- The push modifiers.
Other tags:
- Example: Add push operations. -
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
def push_conflict?(field)
- Since: - 2.2.0
Returns:
-
(true, false)- If this field is a conflict.
Parameters:
-
field(String) -- The field.
Other tags:
- Example: Is this a conflict for a push? -
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 =~ /#{name}/ } > 1) end
def push_fields
- Since: - 2.2.0
Returns:
-
(Array- The push fields.)
Other tags:
- Example: Get the push fields. -
def push_fields @push_fields ||= {} end
def pushes
- Since: - 2.1.0
Returns:
-
(Hash)- The $push/$each operations.
Other tags:
- Example: Get the $push/$each operations. -
def pushes self["$push"] ||= {} end
def set(modifications)
- Since: - 2.1.0
Parameters:
-
modifications(Hash) -- The set modifiers.
Other tags:
- Example: Add set operations. -
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
def set_conflict?(field)
- Since: - 2.2.0
Returns:
-
(true, false)- If this field is a conflict.
Parameters:
-
field(String) -- The field.
Other tags:
- Example: Is this a conflict for a set? -
def set_conflict?(field) name = field.split(".", 2)[0] pull_fields.has_key?(name) || push_fields.has_key?(name) end
def set_fields
- Since: - 2.2.0
Returns:
-
(Array- The set fields.)
Other tags:
- Example: Get the set fields. -
def set_fields @set_fields ||= {} end
def sets
- Since: - 2.1.0
Returns:
-
(Hash)- The $set operations.
Other tags:
- Example: Get the $set operations. -
def sets self["$set"] ||= {} end
def unset(modifications)
- Since: - 2.2.0
Parameters:
-
modifications(Array) -- The unset association names.
Other tags:
- Example: Add unset operations. -
def unset(modifications) modifications.each do |field| unsets.update(field => true) end end
def unsets
- Since: - 2.2.0
Returns:
-
(Hash)- The $unset operations.
Other tags:
- Example: Get the $unset operations. -
def unsets self["$unset"] ||= {} end