Class: Mongoid::IdentityMap

Inherits:
Hash show all
Defined in:
lib/mongoid/identity_map.rb

Overview

Defines behaviour for the identity map in Mongoid.

Instance Method Summary collapse

Methods included from Extensions::Hash::Scoping

#as_conditions

Methods included from Extensions::Hash::CriteriaHelpers

#expand_complex_criteria, #extract_id

Methods included from Extensions::Hash::DeepCopy

#_deep_copy

Instance Method Details

#clear_many(klass, selector) ⇒ Array<Document>

Clear the many documents.

Examples:

Clear the docs.

identity_map.clear_many(Post, selector)

Parameters:

  • klass (Class)

    The klass to clear.

  • selector (Hash)

    The selector to identify it.

Returns:

Since:

  • 2.4.10



18
19
20
# File 'lib/mongoid/identity_map.rb', line 18

def clear_many(klass, selector)
  (documents_for(klass)[selector] ||= []).clear
end

#get(klass, identifier) ⇒ Document

Get a document from the identity map by its id.

Examples:

Get the document from the map.

map.get(Person, id)

Parameters:

  • klass (Class)

    The class of the document.

  • idenfier (Object, Hash)

    The document id or selector.

Returns:

Since:

  • 2.1.0



33
34
35
36
# File 'lib/mongoid/identity_map.rb', line 33

def get(klass, identifier)
  return nil unless Mongoid.identity_map_enabled? && klass
  documents_for(klass)[identifier]
end

#remove(document) ⇒ Document?

Remove the document from the identity map.

Examples:

Remove the document.

map.removed(person)

Parameters:

  • document (Document)

    The document to remove.

Returns:

  • (Document, nil)

    The removed document.

Since:

  • 2.1.0



48
49
50
51
# File 'lib/mongoid/identity_map.rb', line 48

def remove(document)
  return nil unless Mongoid.identity_map_enabled? && document && document.id
  documents_for(document.class).delete(document.id)
end

#set(document) ⇒ Document

Puts a document in the identity map, accessed by it’s id.

Examples:

Put the document in the map.

identity_map.set(document)

Parameters:

  • document (Document)

    The document to place in the map.

Returns:

Since:

  • 2.1.0



63
64
65
66
# File 'lib/mongoid/identity_map.rb', line 63

def set(document)
  return nil unless Mongoid.identity_map_enabled? && document && document.id
  documents_for(document.class)[document.id] = document
end

#set_many(document, selector) ⇒ Array<Document>

Set a document in the identity map for the provided selector.

Examples:

Set the document in the map.

identity_map.set_selector(document, { :person_id => person.id })

Parameters:

  • document (Document)

    The document to set.

  • selector (Hash)

    The selector to identify it.

Returns:

Since:

  • 2.2.0



79
80
81
# File 'lib/mongoid/identity_map.rb', line 79

def set_many(document, selector)
  (documents_for(document.class)[selector] ||= []).push(document)
end

#set_one(document, selector) ⇒ Document

Set a document in the identity map for the provided selector.

Examples:

Set the document in the map.

identity_map.set_selector(document, { :person_id => person.id })

Parameters:

  • document (Document)

    The document to set.

  • selector (Hash)

    The selector to identify it.

Returns:

Since:

  • 2.2.0



94
95
96
# File 'lib/mongoid/identity_map.rb', line 94

def set_one(document, selector)
 documents_for(document.class)[selector] = document
end