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



25
26
27
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 25

def identity_map
  @identity_map ||= {}
end

#identity_map=(v) ⇒ Object



29
30
31
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 29

def identity_map=(v)
  @identity_map = v
end

#identity_map_offObject



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

def identity_map_off
  @identity_map_status = false
end

#identity_map_off?Boolean

Returns:



92
93
94
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 92

def identity_map_off?
  !identity_map_on?
end

#identity_map_onObject



80
81
82
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 80

def identity_map_on
  @identity_map_status = true
end

#identity_map_on?Boolean

Returns:



88
89
90
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 88

def identity_map_on?
  identity_map_status
end

#identity_map_statusObject



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

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

#inherited(descendant) ⇒ Object



20
21
22
23
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 20

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

#load(attrs) ⇒ Object



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

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



54
55
56
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 54

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

#remove_documents_from_map(*documents) ⇒ Object



58
59
60
61
62
# File 'lib/mongo_mapper/plugins/identity_map.rb', line 58

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

#without_identity_map(&block) ⇒ Object



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

def without_identity_map(&block)
  identity_map_off
  yield
ensure
  identity_map_on
end