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



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

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

#find_one(options = {}) ⇒ Object



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

def find_one(options={})
  query = query(options)
  criteria = query.criteria.to_hash
  
  if simple_find?(criteria) && identity_map.key?(criteria[:_id])
    identity_map[criteria[:_id]]
  else
    super.tap do |document|
      remove_documents_from_map(document) if selecting_fields?(query.options)
    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



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

def identity_map_off
  @identity_map_status = false
end

#identity_map_off?Boolean

Returns:



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

def identity_map_off?
  !identity_map_on?
end

#identity_map_onObject



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

def identity_map_on
  @identity_map_status = true
end

#identity_map_on?Boolean

Returns:



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

def identity_map_on?
  identity_map_status
end

#identity_map_statusObject



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

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



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

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



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

def without_identity_map(&block)
  identity_map_off
  yield
ensure
  identity_map_on
end