mongoid_identity_map 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ require "active_support/core_ext"
2
+
3
+ module MongoidIdentityMap
4
+ module Contexts
5
+ module IdentityMappable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ alias_method_chain :one, :identity_map
10
+ alias_method_chain :first, :identity_map
11
+ end
12
+
13
+ def one_with_identity_map
14
+ IdentityMap.fetch(selector.merge(:_klass => klass)) do
15
+ one_without_identity_map
16
+ end
17
+ end
18
+ alias :first_with_identity_map :one_with_identity_map
19
+
20
+ end
21
+ end
22
+ end
23
+
24
+ Mongoid::Contexts::Mongo.send(:include, MongoidIdentityMap::Contexts::IdentityMappable)
@@ -6,6 +6,10 @@ module MongoidIdentityMap
6
6
  ThreadLocalHash.get(selector) || ThreadLocalHash.set(selector, yield)
7
7
  end
8
8
 
9
+ def remove(document)
10
+ ThreadLocalHash.remove(document)
11
+ end
12
+
9
13
  def clear
10
14
  ThreadLocalHash.clear
11
15
  end
@@ -0,0 +1,24 @@
1
+ require "active_support/core_ext"
2
+
3
+ module MongoidIdentityMap
4
+ module Persistence
5
+ module IdentityMappable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ alias_method_chain :remove, :identity_map
10
+ end
11
+
12
+ def remove_with_identity_map(options = {})
13
+ IdentityMap.remove(self)
14
+ self.remove_without_identity_map(options)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ module Mongoid::Persistence
21
+ included do
22
+ include MongoidIdentityMap::Persistence::IdentityMappable
23
+ end
24
+ end
@@ -8,7 +8,11 @@ module MongoidIdentityMap
8
8
  def set(key, value)
9
9
  thread_local_hash[key] = value
10
10
  end
11
-
11
+
12
+ def remove(value)
13
+ thread_local_hash.delete_if {|k, v| v == value}
14
+ end
15
+
12
16
  def clear
13
17
  Thread.current[:mongoid_identity_map_current_thread_hash] = nil
14
18
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidIdentityMap
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,5 +1,8 @@
1
+ require "mongoid"
2
+
1
3
  require "mongoid_identity_map/clear_middleware"
2
4
  require "mongoid_identity_map/thread_local_hash"
3
5
  require "mongoid_identity_map/identity_map"
4
- require "mongoid_identity_map/identity_mappable"
5
6
  require "mongoid_identity_map/railtie" if defined?(Rails)
7
+ require "mongoid_identity_map/contexts/identity_mappable"
8
+ require "mongoid_identity_map/persistence/identity_mappable"
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe MongoidIdentityMap::Contexts::IdentityMappable do
4
+ let(:model) do
5
+ Model.new
6
+ end
7
+
8
+ let(:model_attributes) do
9
+ model.attributes
10
+ end
11
+
12
+ describe "#one" do
13
+ let(:selector) do
14
+ {:selector => :value}
15
+ end
16
+
17
+ before do
18
+ Model.collection.stub!(:find_one).with(selector, {}).and_return(model_attributes)
19
+ end
20
+
21
+ it "should return model" do
22
+ Model.where(selector).one.should == model
23
+ end
24
+
25
+ it "should fetch model from identity map" do
26
+ MongoidIdentityMap::IdentityMap.should_receive(:fetch).with(selector.merge(:_klass => Model))
27
+ Model.where(selector).one
28
+ end
29
+ end
30
+
31
+ describe "#first" do
32
+ let(:selector) do
33
+ {:selector => :value}
34
+ end
35
+
36
+ before do
37
+ Model.collection.stub!(:find_one).with(selector, {}).and_return(model_attributes)
38
+ end
39
+
40
+ it "should return model" do
41
+ Model.where(selector).first.should == model
42
+ end
43
+
44
+ it "should fetch model from identity map" do
45
+ MongoidIdentityMap::IdentityMap.should_receive(:fetch).with(selector.merge(:_klass => Model))
46
+ Model.where(selector).first
47
+ end
48
+ end
49
+ end
@@ -39,13 +39,20 @@ describe MongoidIdentityMap::IdentityMap do
39
39
  fetch.should be(@model)
40
40
  end
41
41
 
42
- it "should not hit database" do
42
+ it "should not call block" do
43
43
  Model.collection.should_not_receive(:find_one_without_identity_map)
44
44
  fetch
45
45
  end
46
46
  end
47
47
  end
48
48
 
49
+ describe ".remove" do
50
+ it "should remove document from identity map" do
51
+ MongoidIdentityMap::ThreadLocalHash.should_receive(:remove).with(@model)
52
+ MongoidIdentityMap::IdentityMap.remove(@model)
53
+ end
54
+ end
55
+
49
56
  describe ".clear" do
50
57
  it "should clear identity map" do
51
58
  MongoidIdentityMap::ThreadLocalHash.should_receive(:clear)
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe MongoidIdentityMap::Persistence::IdentityMappable do
4
+ describe "#remove" do
5
+ before do
6
+ @model = Model.new
7
+ @model.stub!(:remove_without_identity_map)
8
+ MongoidIdentityMap::IdentityMap.stub!(:remove)
9
+ end
10
+
11
+ it "should remove document from identity map" do
12
+ MongoidIdentityMap::IdentityMap.should_receive(:remove).with(@model)
13
+ @model.remove
14
+ end
15
+
16
+ it "should remove document from database" do
17
+ options = {:opt1 => :val1}
18
+ @model.should_receive(:remove_without_identity_map).with(options)
19
+ @model.remove(options)
20
+ end
21
+ end
22
+ end
@@ -1,8 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe MongoidIdentityMap::ThreadLocalHash do
4
- describe ".set and .get" do
5
- it "should set and get key based value" do
4
+ describe "storing key value pairs" do
5
+ it "should store key value pairs" do
6
6
  MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
7
7
 
8
8
  Thread.new do
@@ -13,6 +13,20 @@ describe MongoidIdentityMap::ThreadLocalHash do
13
13
  MongoidIdentityMap::ThreadLocalHash.get(:key).should == "value"
14
14
  end
15
15
  end
16
+
17
+ describe ".remove" do
18
+ it "should remove value" do
19
+ MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
20
+
21
+ Thread.new do
22
+ MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
23
+ MongoidIdentityMap::ThreadLocalHash.remove("value")
24
+ MongoidIdentityMap::ThreadLocalHash.get(:key).should be_nil
25
+ end.join
26
+
27
+ MongoidIdentityMap::ThreadLocalHash.get(:key).should == "value"
28
+ end
29
+ end
16
30
 
17
31
  describe ".clear" do
18
32
  it "should clear" do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 3
9
- version: 0.1.3
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Guilherme Cirne
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-03 00:00:00 -03:00
17
+ date: 2011-05-06 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -108,15 +108,17 @@ files:
108
108
  - Rakefile
109
109
  - lib/mongoid_identity_map.rb
110
110
  - lib/mongoid_identity_map/clear_middleware.rb
111
+ - lib/mongoid_identity_map/contexts/identity_mappable.rb
111
112
  - lib/mongoid_identity_map/identity_map.rb
112
- - lib/mongoid_identity_map/identity_mappable.rb
113
+ - lib/mongoid_identity_map/persistence/identity_mappable.rb
113
114
  - lib/mongoid_identity_map/railtie.rb
114
115
  - lib/mongoid_identity_map/thread_local_hash.rb
115
116
  - lib/mongoid_identity_map/version.rb
116
117
  - mongoid_identity_map.gemspec
117
118
  - spec/mongoid_identity_map/clear_middleware_spec.rb
119
+ - spec/mongoid_identity_map/contexts/identity_mappable_spec.rb
118
120
  - spec/mongoid_identity_map/identity_map_spec.rb
119
- - spec/mongoid_identity_map/identity_mappable_spec.rb
121
+ - spec/mongoid_identity_map/persistence/identity_mappable_spec.rb
120
122
  - spec/mongoid_identity_map/thread_local_hash_spec.rb
121
123
  - spec/spec_helper.rb
122
124
  - spec/support/model.rb
@@ -1,20 +0,0 @@
1
- require 'active_support/core_ext'
2
- require 'mongoid/collection'
3
-
4
- module MongoidIdentityMap
5
- module IdentityMappable
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- alias_method_chain :find_one, :identity_map
10
- end
11
-
12
- def find_one_with_identity_map(selector = {}, options = {})
13
- IdentityMap.fetch(selector.merge(:_collection_name => self.name)) do
14
- find_one_without_identity_map(selector, options)
15
- end
16
- end
17
- end
18
- end
19
-
20
- Mongoid::Collection.send(:include, MongoidIdentityMap::IdentityMappable)
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe MongoidIdentityMap::IdentityMappable do
4
- describe "#find_one" do
5
- before do
6
- @model = Model.new
7
- Model.collection.stub!(:find_one_without_identity_map).with({:_id => BSON::ObjectId("4da13240312f916cd2000002")}, {}).and_return(@model)
8
- end
9
-
10
- it "should return model" do
11
- Model.collection.find_one({:_id => BSON::ObjectId("4da13240312f916cd2000002")}, {}).should be(@model)
12
- end
13
-
14
- it "should include collection name in identity map fetch selector" do
15
- MongoidIdentityMap::IdentityMap.should_receive(:fetch).with(hash_including(:_collection_name => "models"))
16
- Model.collection.find_one({:_id => BSON::ObjectId("4da13240312f916cd2000002")}, {})
17
- end
18
- end
19
- end