Class: Mongoid::IdentityMap

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

Overview

Defines behaviour for the identity map in Mongoid.

Instance Method Summary collapse

Instance Method Details

#clear_many(klass, selector) ⇒ Array

Clear out the loaded documents for the provided selector.

Examples:

Clear out the documents.

map.clear_many(Post, { person_id: person.id })

Parameters:

  • klass (Class)

    The class of the relation.

  • selector (Hash)

    The selector.

Returns:

  • (Array)

    An empty array.

Since:

  • 2.4.10



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.

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
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

Examples:

Get the document from the map.

map.get(Person, { post_id: post })

Parameters:

  • klass (Class)

    The class of the document.

  • idenfier (Hash)

    The selector.

Returns:

  • (Array<Document>)

    The matching documents.

Since:

  • 3.0.0



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.

Examples:

Remove the document.

map.removed(person)

Parameters:

  • document (Document)

    The document to remove.

Returns:

  • (Document, nil)

    The removed document.

Since:

  • 2.1.0



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.

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



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.

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



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.

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



121
122
123
# File 'lib/mongoid/identity_map.rb', line 121

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