Class: Mongoid::IdentityMap
- Inherits:
-
Hash
- Object
- Hash
- Mongoid::IdentityMap
- Defined in:
- lib/mongoid/identity_map.rb
Overview
Defines behaviour for the identity map in Mongoid.
Instance Method Summary collapse
-
#clear_many(klass, selector) ⇒ Array
Clear out the loaded documents for the provided selector.
-
#get(klass, identifier) ⇒ Document
Get a document from the identity map by its id.
-
#get_many(klass, identifier) ⇒ Array<Document>
Get many documents from the map via the selector.
-
#remove(document) ⇒ Document?
Remove the document from the identity map.
-
#set(document) ⇒ Document
Puts a document in the identity map, accessed by its id.
-
#set_many(document, selector) ⇒ Array<Document>
Set a document in the identity map for the provided selector.
-
#set_one(document, selector) ⇒ Document
Set a document in the identity map for the provided selector.
Instance Method Details
#clear_many(klass, selector) ⇒ Array
Clear out the loaded documents for the provided selector.
18 19 20 |
# File 'lib/mongoid/identity_map.rb', line 18 def clear_many(klass, selector) documents_for(klass)[selector] = {} end |
#get(klass, identifier) ⇒ Document
Get a document from the identity map by its id.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongoid/identity_map.rb', line 33 def get(klass, identifier) if Mongoid.using_identity_map? && klass if identifier.is_a?(::Array) documents = documents_for(klass) identifier.map do |id| documents[id] || (return nil) end else documents_for(klass)[identifier] end end end |
#get_many(klass, identifier) ⇒ Array<Document>
Get many documents from the map via the selector
57 58 59 60 61 |
# File 'lib/mongoid/identity_map.rb', line 57 def get_many(klass, identifier) if Mongoid.using_identity_map? && klass documents_for(klass)[identifier].try(:values) end end |
#remove(document) ⇒ Document?
Remove the document from the identity map.
73 74 75 76 77 |
# File 'lib/mongoid/identity_map.rb', line 73 def remove(document) if Mongoid.using_identity_map? && document && document.id documents_for(document.class).delete(document.id) end end |
#set(document) ⇒ Document
Puts a document in the identity map, accessed by its id.
89 90 91 92 93 |
# File 'lib/mongoid/identity_map.rb', line 89 def set(document) if Mongoid.using_identity_map? && document && document.id documents_for(document.class)[document.id] = document end end |
#set_many(document, selector) ⇒ Array<Document>
Set a document in the identity map for the provided selector.
106 107 108 |
# File 'lib/mongoid/identity_map.rb', line 106 def set_many(document, selector) (documents_for(document.class)[selector] ||= {})[document.id] = document end |
#set_one(document, selector) ⇒ Document
Set a document in the identity map for the provided selector.
121 122 123 |
# File 'lib/mongoid/identity_map.rb', line 121 def set_one(document, selector) documents_for(document.class)[selector] = document end |