Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Always include IdentityMap, but disabled by default.

  • Loading branch information...
commit 9f9a0a91e2117ab342101c3301d86b550f6a1422 1 parent d41c360
John Nunemaker jnunemaker authored
11 examples/identity_map.rb
View
@@ -4,21 +4,19 @@
MongoMapper.database = 'testing'
+MongoMapper::Plugins::IdentityMap.enabled = true
+
class User
include MongoMapper::Document
- plugin MongoMapper::Plugins::IdentityMap
key :name, String
end
User.delete_all
-user = User.create(:name => 'John')
-
# User gets added to map on save
-pp User.identity_map[user.id]
+user = User.create(:name => 'John')
# Does not matter how you find user, it is always the same object until the identity map is cleared
-puts "#{User.identity_map[user.id].object_id} == #{user.object_id}"
puts "#{User.find(user.id).object_id} == #{user.object_id}"
puts "#{User.all[0].object_id} == #{user.object_id}"
@@ -28,6 +26,3 @@ class User
# User gets removed from map on destroy
user = User.create
user.destroy
-puts "Should be nil: " + User.identity_map[user.id].inspect
-
-
2  examples/identity_map/automatic.rb
View
@@ -1,2 +1,2 @@
# Using the following will turn on identity map for all models
-MongoMapper::Document.plugin(MongoMapper::Plugins::IdentityMap)
+MongoMapper::Plugins::IdentityMap.enabled = true
1  lib/mongo_mapper/document.rb
View
@@ -33,6 +33,7 @@ module Document
include Plugins::Validations
include Plugins::EmbeddedCallbacks
include Plugins::Callbacks # for now callbacks needs to be after validations
+ include Plugins::IdentityMap
included do
extend Plugins
14 test/functional/test_identity_map.rb
View
@@ -26,19 +26,20 @@ def clear_identity_map
MongoMapper::Plugins::IdentityMap.clear
end
+ should "default identity map to off" do
+ MongoMapper::Plugins::IdentityMap.enabled?.should be_false
+ end
+
context "Document" do
setup do
+ @original_identity_map_enabled = MongoMapper::Plugins::IdentityMap.enabled
MongoMapper::Plugins::IdentityMap.enabled = true
@person_class = Doc('Person') do
- plugin MongoMapper::Plugins::IdentityMap
-
key :name, String
end
@post_class = Doc('Post') do
- plugin MongoMapper::Plugins::IdentityMap
-
key :title, String
key :person_id, ObjectId
end
@@ -49,6 +50,10 @@ def clear_identity_map
clear_identity_map
end
+ teardown do
+ MongoMapper::Plugins::IdentityMap.enabled = @original_identity_map_enabled
+ end
+
should "be able to clear the map of all models" do
person = @person_class.create(:name => 'John')
post = @post_class.create(:title => 'IM 4eva')
@@ -376,7 +381,6 @@ def clear_identity_map
setup do
class ::Item
include MongoMapper::Document
- plugin MongoMapper::Plugins::IdentityMap
key :title, String
key :parent_id, ObjectId
Please sign in to comment.
Something went wrong with that request. Please try again.