class Mongoid::IdentityMap
Defines behaviour for the identity map in Mongoid
.
Public Instance Methods
Clear out the loaded documents for the provided selector.
@example Clear out the documents.
map.clear_many(Post, { person_id: person.id })
@param [ Class ] klass The class of the relation. @param [ Hash ] selector The selector.
@return [ Array ] An empty array.
@since 2.4.10
# File lib/mongoid/identity_map.rb, line 18 def clear_many(klass, selector) documents_for(klass)[selector] = {} end
Get a document from the identity map by its id.
@example Get the document from the map.
map.get(Person, id)
@param [ Class ] klass The class of the document. @param [ Object
, Hash ] idenfier The document id or selector.
@return [ Document
] The matching document.
@since 2.1.0
# 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 documents from the map via the selector
@example Get the document from the map.
map.get(Person, { post_id: post })
@param [ Class ] klass The class of the document. @param [ Hash ] idenfier The selector.
@return [ Array<Document> ] The matching documents.
@since 3.0.0
# 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 the document from the identity map.
@example Remove the document.
map.removed(person)
@param [ Document
] document The document to remove.
@return [ Document
, nil ] The removed document.
@since 2.1.0
# 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
Puts a document in the identity map, accessed by its id.
@example Put the document in the map.
identity_map.set(document)
@param [ Document
] document The document to place in the map.
@return [ Document
] The provided document.
@since 2.1.0
# 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 a document in the identity map for the provided selector.
@example Set the document in the map.
identity_map.set_selector(document, { :person_id => person.id })
@param [ Document
] document The document to set. @param [ Hash ] selector The selector to identify it.
@return [ Array<Document> ] The documents.
@since 2.2.0
# File lib/mongoid/identity_map.rb, line 106 def set_many(document, selector) (documents_for(document.class)[selector] ||= {})[document.id] = document end
Set a document in the identity map for the provided selector.
@example Set the document in the map.
identity_map.set_selector(document, { :person_id => person.id })
@param [ Document
] document The document to set. @param [ Hash ] selector The selector to identify it.
@return [ Document
] The matching document.
@since 2.2.0
# File lib/mongoid/identity_map.rb, line 121 def set_one(document, selector) documents_for(document.class)[selector] = document end
Private Instance Methods
Get the documents in the identity map for a specific class.
@example Get the documents for the class.
map.documents_for(Person)
@param [ Class ] klass The class to retrieve.
@return [ Hash ] The documents.
@since 2.1.0
# File lib/mongoid/identity_map.rb, line 137 def documents_for(klass) if klass self[klass.collection_name] ||= {} end end