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

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

Defined Under Namespace

Modules: IdentityMapQueryMethods

Instance Method Summary collapse

Instance Method Details

#identity_mapObject



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

def identity_map
  @identity_map ||= {}
end

#identity_map=(v) ⇒ Object



31
32
33
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 31

def identity_map=(v)
  @identity_map = v
end

#identity_map_offObject



86
87
88
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 86

def identity_map_off
  @identity_map_status = false
end

#identity_map_off?Boolean

Returns:



94
95
96
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 94

def identity_map_off?
  !identity_map_on?
end

#identity_map_onObject



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

def identity_map_on
  @identity_map_status = true
end

#identity_map_on?Boolean

Returns:



90
91
92
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 90

def identity_map_on?
  identity_map_status
end

#identity_map_statusObject



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

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

#inherited(descendant) ⇒ Object



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

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

#load(attrs) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 66

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

#query(opts = {}) ⇒ Object



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

def query(opts={})
  super.extend(IdentityMapQueryMethods)
end

#remove_documents_from_map(*documents) ⇒ Object



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

def remove_documents_from_map(*documents)
  documents.flatten.compact.each do |document|
    identity_map.delete(document['_id'])
  end
end

#without_identity_map(&block) ⇒ Object



98
99
100
101
102
103
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 98

def without_identity_map(&block)
  identity_map_off
  yield
ensure
  identity_map_on
end