Class: Mongoid::Atomic::Modifiers
- Inherits:
-
Hash
- Object
- Hash
- Mongoid::Atomic::Modifiers
- Defined in:
- lib/mongoid/atomic/modifiers.rb
Overview
This class contains the logic for supporting atomic operations against the database.
Instance Method Summary collapse
-
#add_to_set(modifications) ⇒ Object
Add the atomic $addToSet modifiers to the hash.
-
#pull(modifications) ⇒ Object
Adds pull all modifiers to the modifiers hash.
-
#pull_all(modifications) ⇒ Object
Adds pull all modifiers to the modifiers hash.
-
#push(modifications) ⇒ Object
Adds push modifiers to the modifiers hash.
-
#set(modifications) ⇒ Object
Adds set operations to the modifiers hash.
-
#unset(modifications) ⇒ Object
Adds unset operations to the modifiers hash.
Instance Method Details
#add_to_set(modifications) ⇒ Object
Add the atomic $addToSet modifiers to the hash.
14 15 16 17 18 19 20 21 22 23 24 |
# 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 |
#pull(modifications) ⇒ Object
Adds pull all modifiers to the modifiers hash.
45 46 47 48 49 50 |
# 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) ⇒ Object
Adds pull all modifiers to the modifiers hash.
32 33 34 35 36 37 |
# 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 |
#push(modifications) ⇒ Object
Adds push modifiers to the modifiers hash.
58 59 60 61 62 63 64 |
# 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 |
#set(modifications) ⇒ Object
Adds set operations to the modifiers hash.
72 73 74 75 76 77 78 79 80 |
# 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 |
#unset(modifications) ⇒ Object
Adds unset operations to the modifiers hash.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# 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 |