Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved identity map on/off to per model basis.

  • Loading branch information...
commit df504c7dd897bbe2d02f289f60ecc71004b2160c 1 parent 6f7754c
@jnunemaker jnunemaker authored
View
52 lib/mongo_mapper/plugins/identity_map.rb
@@ -9,26 +9,6 @@ def self.clear
models.each { |m| m.identity_map.clear }
end
- def self.status
- defined?(@map) ? @map : true
- end
-
- def self.on
- @map = true
- end
-
- def self.off
- @map = false
- end
-
- def self.on?
- status
- end
-
- def self.off?
- !on?
- end
-
module ClassMethods
def inherited(descendant)
descendant.identity_map = identity_map
@@ -65,19 +45,39 @@ def find_many(options)
def load(attrs)
document = identity_map[attrs['_id']]
- if document.nil? || IdentityMap.off?
+ if document.nil? || identity_map_off?
document = super
- identity_map[document._id] = document if IdentityMap.on?
+ identity_map[document._id] = document if identity_map_on?
end
document
end
+
+ def identity_map_status
+ defined?(@identity_map_status) ? @identity_map_status : true
+ end
+
+ def identity_map_on
+ @identity_map_status = true
+ end
+
+ def identity_map_off
+ @identity_map_status = false
+ end
+
+ def identity_map_on?
+ identity_map_status
+ end
+
+ def identity_map_off?
+ !identity_map_on?
+ end
def without_identity_map(&block)
- IdentityMap.off
+ identity_map_off
yield
ensure
- IdentityMap.on
+ identity_map_on
end
private
@@ -103,13 +103,13 @@ def identity_map
def save(*args)
if result = super
- identity_map[_id] = self if IdentityMap.on?
+ identity_map[_id] = self if self.class.identity_map_on?
end
result
end
def delete
- identity_map.delete(_id) if IdentityMap.on?
+ identity_map.delete(_id) if self.class.identity_map_on?
super
end
end
View
29 test/functional/test_identity_map.rb
@@ -27,7 +27,6 @@ def expects_one_query
context "Document" do
setup do
MongoMapper::Plugins::IdentityMap.models.clear
- MongoMapper::Plugins::IdentityMap.on
@person_class = Doc('Person') do
set_collection_name 'people'
@@ -47,6 +46,8 @@ def expects_one_query
@post_class.belongs_to :person, :class => @person_class
@person_class.many :posts, :class => @post_class
+ @post_class.identity_map_on
+ @person_class.identity_map_on
MongoMapper::Plugins::IdentityMap.clear
end
@@ -68,26 +69,22 @@ def expects_one_query
context "IM on off status" do
teardown do
- MongoMapper::Plugins::IdentityMap.on
+ @post_class.identity_map_on
end
should "be true if on" do
- MongoMapper::Plugins::IdentityMap.on
- MongoMapper::Plugins::IdentityMap.should be_on
- MongoMapper::Plugins::IdentityMap.should_not be_off
+ @post_class.identity_map_on
+ @post_class.should be_identity_map_on
+ @post_class.should_not be_identity_map_off
end
should "be false if off" do
- MongoMapper::Plugins::IdentityMap.off
- MongoMapper::Plugins::IdentityMap.should be_off
- MongoMapper::Plugins::IdentityMap.should_not be_on
+ @post_class.identity_map_off
+ @post_class.should be_identity_map_off
+ @post_class.should_not be_identity_map_on
end
end
- should "be able to turn identity map on" do
- MongoMapper::Plugins::IdentityMap
- end
-
should "default identity map to hash" do
Doc do
plugin MongoMapper::Plugins::IdentityMap
@@ -455,6 +452,14 @@ class ::BlogPost < ::Item
loaded.should_not equal(post)
end
end
+
+ context "all" do
+ should "not add to map" do
+ @post_class.without_identity_map do
+
+ end
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.