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

#scoped

Methods included from Extensions::Hash::CriteriaHelpers

#expand_complex_criteria

Instance Method Details

#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



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

def get(klass, identifier)
  return nil unless Mongoid.identity_map_enabled?
  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



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

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



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

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



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

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



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

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