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

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

Instance Method Summary collapse

Instance Method Details

#find_many(options) ⇒ Object



43
44
45
46
47
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 43

def find_many(options)
  super.tap do |documents|
    remove_documents_from_map(documents) if query(options).fields?
  end
end

#find_one(options = {}) ⇒ Object



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

def find_one(options={})
  query = query(options)
  
  if query.simple? && identity_map.key?(query[:_id])
    identity_map[query[:_id]]
  else
    super.tap do |document|
      remove_documents_from_map(document) if query.fields?
    end
  end
end

#identity_mapObject



23
24
25
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 23

def identity_map
  @identity_map ||= {}
end

#identity_map=(v) ⇒ Object



27
28
29
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 27

def identity_map=(v)
  @identity_map = v
end

#identity_map_offObject



69
70
71
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 69

def identity_map_off
  @identity_map_status = false
end

#identity_map_off?Boolean

Returns:



77
78
79
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 77

def identity_map_off?
  !identity_map_on?
end

#identity_map_onObject



65
66
67
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 65

def identity_map_on
  @identity_map_status = true
end

#identity_map_on?Boolean

Returns:



73
74
75
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 73

def identity_map_on?
  identity_map_status
end

#identity_map_statusObject



61
62
63
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 61

def identity_map_status
  defined?(@identity_map_status) ? @identity_map_status : true
end

#inherited(descendant) ⇒ Object



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

def inherited(descendant)
  descendant.identity_map = identity_map
  super
end

#load(attrs) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 49

def load(attrs)
  return nil if attrs.nil?
  document = identity_map[attrs['_id']]

  if document.nil? || identity_map_off?
    document = super
    identity_map[document._id] = document if identity_map_on?
  end

  document
end

#without_identity_map(&block) ⇒ Object



81
82
83
84
85
86
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 81

def without_identity_map(&block)
  identity_map_off
  yield
ensure
  identity_map_on
end