Module: MongoMapper::Plugins::IdentityMap::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#find_one(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 17

def find_one(options={})
  criteria, options = to_finder_options(options)
  key = identity_map_key(criteria[:_id])

  if document = identity_map[key]
    document
  else
    document = super
  end
  
  document
end

#identity_mapObject



5
6
7
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 5

def identity_map
  Thread.current[:mongo_mapper_identity_map] ||= {}
end

#identity_map=(v) ⇒ Object



9
10
11
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 9

def identity_map=(v)
  Thread.current[:mongo_mapper_identity_map] = v
end

#identity_map_key(id) ⇒ Object



13
14
15
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 13

def identity_map_key(id)
  "#{self}:#{id}"
end

#load(attrs) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 30

def load(attrs)
  id = attrs[:_id] || attrs[:id] || attrs['_id'] || attrs['id']
  key = identity_map_key(id)

  unless document = identity_map[key]
    document = super
    identity_map[document.identity_map_key] = document
  end

  document
end