mongoid_identity_map 0.3.0 → 0.3.1

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.
data/README.md CHANGED
@@ -9,20 +9,24 @@ Mongoid Identity Map is a simple and transparent Identity Map implementation for
9
9
 
10
10
  [Identity Map pattern](http://martinfowler.com/eaaCatalog/identityMap.html)
11
11
 
12
- ## Installation
12
+ ## Installing
13
13
 
14
14
  In Gemfile:
15
15
 
16
16
  gem "mongoid_identity_map"
17
17
 
18
- ## Configuration
18
+ ## Using
19
19
 
20
- Add rack middleware `MongoidIdentityMap::ClearMiddleware` for clearing identity map after each request.
20
+ Identity map is applied automatically to all queries that return a single model.
21
21
 
22
- When using this gem in a Rails app this is done automatically.
22
+ ## Clearing the identity map
23
23
 
24
- ## Current Limitations
24
+ A rack middleware - `MongoidIdentityMap::ClearMiddleware` - is provided for clearing the identity map after each request. In a Rails app, this middleware is automatically added to the list of middlewares.
25
+
26
+ You can also clear the identity map by calling `MongoidIdentityMap::IdentityMap.clear`.
27
+
28
+ ## Current limitations
25
29
 
26
30
  Currently, it's an all or nothing deal. It is not possible to turn the identiy map on or off on a class (Mongoid::Document) basis.
27
31
 
28
- Identity map is applied only on Mongoid's Collection find_one method. The find method doesn't have the identity map applied.
32
+ Identity map is applied only on queries that return a single model. Queries that return a cursor are not affected.
@@ -8,15 +8,28 @@ module MongoidIdentityMap
8
8
  included do
9
9
  alias_method_chain :one, :identity_map
10
10
  alias_method_chain :first, :identity_map
11
+ alias_method_chain :last, :identity_map
11
12
  end
12
13
 
13
14
  def one_with_identity_map
15
+ fetch(:one_without_identity_map)
16
+ end
17
+
18
+ def first_with_identity_map
19
+ fetch(:first_without_identity_map)
20
+ end
21
+
22
+ def last_with_identity_map
23
+ fetch(:last_without_identity_map)
24
+ end
25
+
26
+ private
27
+
28
+ def fetch(method)
14
29
  IdentityMap.fetch(selector.merge(:_klass => klass)) do
15
- one_without_identity_map
30
+ send(method)
16
31
  end
17
32
  end
18
- alias :first_with_identity_map :one_with_identity_map
19
-
20
33
  end
21
34
  end
22
35
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidIdentityMap
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -12,34 +12,40 @@ describe MongoidIdentityMap::Contexts::IdentityMappable do
12
12
  let(:selector) do
13
13
  {:selector => :value}
14
14
  end
15
-
16
- describe "#one" do
15
+
16
+ shared_examples_for "methods that return a single model" do
17
17
  before do
18
- Model.collection.stub!(:find_one).with(selector, {}).and_return(model_attributes)
18
+ Model.collection.stub!(:find_one).with(selector, options).and_return(model_attributes)
19
19
  end
20
20
 
21
21
  it "should fetch model from identity map under key comprised of find selector merged with model class" do
22
22
  MongoidIdentityMap::IdentityMap.should_receive(:fetch).with(selector.merge(:_klass => Model))
23
- Model.where(selector).one
23
+ find
24
24
  end
25
25
 
26
26
  it "should return model" do
27
- Model.where(selector).one.should == model
27
+ find.should == model
28
28
  end
29
29
  end
30
30
 
31
- describe "#first" do
32
- before do
33
- Model.collection.stub!(:find_one).with(selector, {}).and_return(model_attributes)
31
+ describe "#one" do
32
+ it_should_behave_like "methods that return a single model" do
33
+ let(:find) {Model.where(selector).one}
34
+ let(:options) {{}}
34
35
  end
35
-
36
- it "should fetch model from identity map under key comprised of find selector merged with model class" do
37
- MongoidIdentityMap::IdentityMap.should_receive(:fetch).with(selector.merge(:_klass => Model))
38
- Model.where(selector).first
36
+ end
37
+
38
+ describe "#first" do
39
+ it_should_behave_like "methods that return a single model" do
40
+ let(:find) {Model.where(selector).first}
41
+ let(:options) {{}}
39
42
  end
43
+ end
40
44
 
41
- it "should return model" do
42
- Model.where(selector).first.should == model
45
+ describe "#last" do
46
+ it_should_behave_like "methods that return a single model" do
47
+ let(:find) {Model.where(selector).last}
48
+ let(:options) {{:sort=>[[:_id, :desc]]}}
43
49
  end
44
50
  end
45
51
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
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-08 00:00:00 -03:00
17
+ date: 2011-05-10 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency