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



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

def find_many(options)
  criteria, finder_options = to_finder_options(options)
  super.tap do |documents|
    remove_documents_from_map(documents) if selecting_fields?(finder_options)
  end
end

#find_one(options = {}) ⇒ Object



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

def find_one(options={})
  criteria, finder_options = to_finder_options(options)

  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?(finder_options)
    end
  end
end

#identity_mapObject



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

def identity_map
  @identity_map ||= {}
end

#identity_map=(v) ⇒ Object



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

def identity_map=(v)
  @identity_map = v
end

#identity_map_offObject



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

def identity_map_off
  @identity_map_status = false
end

#identity_map_off?Boolean

Returns:



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

def identity_map_off?
  !identity_map_on?
end

#identity_map_onObject



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

def identity_map_on
  @identity_map_status = true
end

#identity_map_on?Boolean

Returns:



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

def identity_map_on?
  identity_map_status
end

#identity_map_statusObject



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

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

#inherited(descendant) ⇒ Object



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

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

#load(attrs) ⇒ Object



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

def load(attrs)
  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



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

def without_identity_map(&block)
  identity_map_off
  yield
ensure
  identity_map_on
end