Module: MarkMapper::Plugins::IdentityMap::ClassMethods

Defined in:
lib/mark_mapper/plugins/identity_map.rb

Instance Method Summary collapse

Instance Method Details

#get_from_identity_map(id) ⇒ Object

Private - Looks for a document in the identity map


56
57
58
# File 'lib/mark_mapper/plugins/identity_map.rb', line 56

def get_from_identity_map(id)
  IdentityMap.repository[IdentityMap.key(self, id)]
end

#load(attrs, with_cast = false) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mark_mapper/plugins/identity_map.rb', line 72

def load(attrs, with_cast = false)
  return super unless Thread.current[:mark_mapper_identity_map_enabled]
  return nil unless attrs
  document = get_from_identity_map(attrs['_id'])

  if !document
    document = super
    document.add_to_identity_map
  end

  document
end

#query(opts = {}) ⇒ Object


60
61
62
63
64
# File 'lib/mark_mapper/plugins/identity_map.rb', line 60

def query(opts={})
  super.tap do |query|
    query.identity_map = self if Thread.current[:mark_mapper_identity_map_enabled]
  end
end

#remove_documents_from_map(*documents) ⇒ Object


66
67
68
69
70
# File 'lib/mark_mapper/plugins/identity_map.rb', line 66

def remove_documents_from_map(*documents)
  documents.flatten.compact.each do |document|
    document.remove_from_identity_map
  end
end