Module: Mongoid::Atomic
- Extended by:
- ActiveSupport::Concern
- Included in:
- Components
- Defined in:
- lib/mongoid/atomic.rb,
lib/mongoid/atomic/modifiers.rb,
lib/mongoid/atomic/paths/root.rb,
lib/mongoid/atomic/paths/embedded.rb,
lib/mongoid/atomic/paths/embedded/one.rb,
lib/mongoid/atomic/paths/embedded/many.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Paths Classes: Modifiers
Instance Method Summary collapse
-
#add_atomic_pull(document) ⇒ Object
Add the document as an atomic pull.
-
#atomic_delete_modifier ⇒ String
Get the removal modifier for the document.
-
#atomic_insert_modifier ⇒ String
Get the insertion modifier for the document.
-
#atomic_path ⇒ String
Return the path to this
Document
in JSON notation, used for atomic updates via $set in MongoDB. -
#atomic_position ⇒ String
Returns the positional operator of this document for modification.
-
#atomic_pulls ⇒ Array<Hash>
Get all the attributes that need to be pulled.
-
#atomic_pushes ⇒ Hash
Get all the push attributes that need to occur.
-
#atomic_selector ⇒ String
Return the selector for this document to be matched exactly for use with MongoDB’s $ operator.
-
#atomic_sets ⇒ Hash
Get all the attributes that need to be set.
-
#atomic_unsets ⇒ Array<Hash>
Get all the attributes that need to be unset.
-
#atomic_updates ⇒ Hash
(also: #_updates)
Get all the atomic updates that need to happen for the current
Document
. -
#delayed_atomic_pulls ⇒ Hash
Get a hash of atomic pulls that are pending.
-
#delayed_atomic_sets ⇒ Hash
Get all the atomic sets that have had their saves delayed.
Instance Method Details
#add_atomic_pull(document) ⇒ Object
Add the document as an atomic pull.
123 124 125 126 |
# File 'lib/mongoid/atomic.rb', line 123 def add_atomic_pull(document) document.flagged_for_destroy = true (delayed_atomic_pulls[document..name.to_s] ||= []).push(document) end |
#atomic_delete_modifier ⇒ String
Get the removal modifier for the document. Will be nil on root documents, $unset on embeds_one, $set on embeds_many.
59 60 61 |
# File 'lib/mongoid/atomic.rb', line 59 def atomic_delete_modifier atomic_paths.delete_modifier end |
#atomic_insert_modifier ⇒ String
Get the insertion modifier for the document. Will be nil on root documents, $set on embeds_one, $push on embeds_many.
70 71 72 |
# File 'lib/mongoid/atomic.rb', line 70 def atomic_insert_modifier atomic_paths.insert_modifier end |
#atomic_path ⇒ String
Return the path to this Document
in JSON notation, used for atomic updates via $set in MongoDB.
81 82 83 |
# File 'lib/mongoid/atomic.rb', line 81 def atomic_path atomic_paths.path end |
#atomic_position ⇒ String
Returns the positional operator of this document for modification.
91 92 93 |
# File 'lib/mongoid/atomic.rb', line 91 def atomic_position atomic_paths.position end |
#atomic_pulls ⇒ Array<Hash>
Get all the attributes that need to be pulled.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/mongoid/atomic.rb', line 103 def atomic_pulls delayed_atomic_pulls.inject({}) do |pulls, (name, docs)| pulls.tap do |pull| docs.each do |doc| (pull[doc.atomic_path] ||= []).push(doc.as_document) doc.destroyed = true doc.flagged_for_destroy = false end end end end |
#atomic_pushes ⇒ Hash
Get all the push attributes that need to occur.
136 137 138 |
# File 'lib/mongoid/atomic.rb', line 136 def atomic_pushes pushable? ? { atomic_path => as_document } : {} end |
#atomic_selector ⇒ String
Return the selector for this document to be matched exactly for use with MongoDB’s $ operator.
147 148 149 |
# File 'lib/mongoid/atomic.rb', line 147 def atomic_selector atomic_paths.selector end |
#atomic_sets ⇒ Hash
Get all the attributes that need to be set.
159 160 161 |
# File 'lib/mongoid/atomic.rb', line 159 def atomic_sets updateable? ? setters : settable? ? { atomic_path => as_document } : {} end |
#atomic_unsets ⇒ Array<Hash>
Get all the attributes that need to be unset.
171 172 173 |
# File 'lib/mongoid/atomic.rb', line 171 def atomic_unsets @atomic_unsets ||= [] end |
#atomic_updates ⇒ Hash Also known as: _updates
MongoDB does not allow “conflicting modifications” to be performed in a single operation. Conflicting modifications are detected by the ‘haveConflictingMod’ function in MongoDB. Examination of the code suggests that two modifications (a $set and a $pushAll, for example) conflict if:
(1) the key paths being modified are equal.
(2) one key path is a prefix of the other.
So a $set of ‘addresses.0.street’ will conflict with a $pushAll to ‘addresses’, and we will need to split our update into two pieces. We do not, however, attempt to match MongoDB’s logic exactly. Instead, we assume that two updates conflict if the first component of the two key paths matches.
Get all the atomic updates that need to happen for the current Document
. This includes all changes that need to happen in the entire hierarchy that exists below where the save call was made.
42 43 44 45 46 47 48 49 |
# File 'lib/mongoid/atomic.rb', line 42 def atomic_updates Modifiers.new.tap do |mods| generate_atomic_updates(mods, self) _children.each do |child| generate_atomic_updates(mods, child) end end end |
#delayed_atomic_pulls ⇒ Hash
Get a hash of atomic pulls that are pending.
195 196 197 |
# File 'lib/mongoid/atomic.rb', line 195 def delayed_atomic_pulls @delayed_atomic_pulls ||= {} end |
#delayed_atomic_sets ⇒ Hash
Get all the atomic sets that have had their saves delayed.
183 184 185 |
# File 'lib/mongoid/atomic.rb', line 183 def delayed_atomic_sets @delayed_atomic_sets ||= {} end |