module MongoMapper::Plugins::IdentityMap

Public Class Methods

clear() click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 25
def self.clear
  repository.clear
end
enabled() click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 13
def self.enabled
  Thread.current[:mongo_mapper_identity_map_enabled]
end
enabled=(flag) click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 9
def self.enabled=(flag)
  Thread.current[:mongo_mapper_identity_map_enabled] = flag
end
enabled?() click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 17
def self.enabled?
  enabled == true
end
include?(document) click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 29
def self.include?(document)
  repository.key?(IdentityMap.key(document.class, document._id))
end
key(model, id) click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 33
def self.key(model, id)
  "#{model.single_collection_root.name}:#{id}"
end
repository() click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 21
def self.repository
  Thread.current[:mongo_mapper_identity_map] ||= {}
end
use() { || ... } click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 37
def self.use
  old, self.enabled = enabled, true

  yield if block_given?
ensure
  self.enabled = old
  clear
end
without() { || ... } click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 46
def self.without
  old, self.enabled = enabled, false

  yield if block_given?
ensure
  self.enabled = old
end

Public Instance Methods

add_to_identity_map() click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 94
def add_to_identity_map
  if IdentityMap.enabled?
    key = IdentityMap.key(self.class, _id)
    IdentityMap.repository[key] = self
  end
end
delete() click to toggle source
Calls superclass method
# File lib/mongo_mapper/plugins/identity_map.rb, line 90
def delete
  super.tap { remove_from_identity_map }
end
remove_from_identity_map() click to toggle source
# File lib/mongo_mapper/plugins/identity_map.rb, line 101
def remove_from_identity_map
  if IdentityMap.enabled?
    key = IdentityMap.key(self.class, _id)
    IdentityMap.repository.delete(key)
  end
end
save(*args) click to toggle source
Calls superclass method
# File lib/mongo_mapper/plugins/identity_map.rb, line 86
def save(*args)
  super.tap { |result| add_to_identity_map if result }
end