mongoid_identity_map 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ pkg/*
3
+ *.gem
4
+ .bundle
5
+ .idea
6
+ .rspec
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+ rvm use 1.9.2@mongoid_identity_map
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in mongoid_identity_map.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,13 @@
1
+ module MongoidIdentityMap
2
+ class ClearMiddleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ res = @app.call(env)
9
+ CurrentThreadHash.clear
10
+ res
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module MongoidIdentityMap
2
+ class CurrentThreadHash
3
+ class << self
4
+ def get(key)
5
+ current_thread_hash[key]
6
+ end
7
+
8
+ def set(key, value)
9
+ current_thread_hash[key] = value
10
+ end
11
+
12
+ def clear
13
+ Thread.current[:mongoid_identity_map_current_thread_hash] = nil
14
+ end
15
+
16
+ private
17
+
18
+ def current_thread_hash
19
+ Thread.current[:mongoid_identity_map_current_thread_hash] ||= Hash.new
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module MongoidIdentityMap
2
+ class IdentityMap
3
+
4
+ class << self
5
+ def fetch(selector)
6
+ CurrentThreadHash.get(selector) || CurrentThreadHash.set(selector, yield)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,20 @@
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)
@@ -0,0 +1,3 @@
1
+ module MongoidIdentityMap
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "mongoid_identity_map/clear_middleware"
2
+ require "mongoid_identity_map/current_thread_hash"
3
+ require "mongoid_identity_map/identity_map"
4
+ require "mongoid_identity_map/identity_mappable"
5
+
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/mongoid_identity_map/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "mongoid_identity_map"
6
+ s.version = MongoidIdentityMap::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Guilherme Cirne"]
9
+ s.email = ["gcirne@gmail.com"]
10
+ s.homepage = "http://rubygems.org/gems/mongoid_identity_map"
11
+ s.summary = "IdentityMap for Mongoid"
12
+ s.description = "Simple and transparent IdentityMap implementation for Mongoid"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "mongoid_identity_map"
16
+
17
+ s.add_dependency "activesupport", "~> 3.0.0"
18
+ s.add_dependency "mongoid", "~> 2.0.0"
19
+
20
+ s.add_development_dependency "bson_ext", ">= 1.3.0"
21
+ s.add_development_dependency "bundler", ">= 1.0.0"
22
+ s.add_development_dependency "rspec", ">= 2.5.0"
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
26
+ s.require_path = 'lib'
27
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ describe MongoidIdentityMap::ClearMiddleware do
4
+ before do
5
+ @env = {}
6
+ @resp = stub("resp")
7
+ @app = stub("app")
8
+ @app.stub!(:call).with(@env).and_return(@resp)
9
+ @clear_middleware = MongoidIdentityMap::ClearMiddleware.new(@app)
10
+ end
11
+
12
+ it "should return app response" do
13
+ @clear_middleware.call(@env).should be(@resp)
14
+ end
15
+
16
+ it "should clear identity map" do
17
+ MongoidIdentityMap::CurrentThreadHash.should_receive(:clear)
18
+ @clear_middleware.call(@env)
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe MongoidIdentityMap::CurrentThreadHash do
4
+ describe ".set and .get" do
5
+ it "should set and get key based value" do
6
+ MongoidIdentityMap::CurrentThreadHash.set(:key, "value")
7
+
8
+ Thread.new do
9
+ MongoidIdentityMap::CurrentThreadHash.set(:key, "value2")
10
+ MongoidIdentityMap::CurrentThreadHash.get(:key).should == "value2"
11
+ end.join
12
+
13
+ MongoidIdentityMap::CurrentThreadHash.get(:key).should == "value"
14
+ end
15
+ end
16
+
17
+ describe ".clear" do
18
+ it "should clear" do
19
+ MongoidIdentityMap::CurrentThreadHash.set(:key, "value")
20
+
21
+ Thread.new do
22
+ MongoidIdentityMap::CurrentThreadHash.set(:key, "value")
23
+ MongoidIdentityMap::CurrentThreadHash.clear
24
+ MongoidIdentityMap::CurrentThreadHash.get(:key).should be_nil
25
+ end.join
26
+
27
+ MongoidIdentityMap::CurrentThreadHash.get(:key).should == "value"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe MongoidIdentityMap::IdentityMap do
4
+ before do
5
+ @model = Model.new
6
+ @selector = {:_id => BSON::ObjectId("4da13240312f916cd2000002")}
7
+ end
8
+
9
+ describe ".fetch" do
10
+ context "when document doesn't exist in identity map" do
11
+ before do
12
+ MongoidIdentityMap::CurrentThreadHash.stub!(:get).with(@selector).and_return(nil)
13
+ Model.collection.stub!(:find_one_without_identity_map).with(@selector).and_return(@model)
14
+ end
15
+
16
+ it "should return document from database" do
17
+ Model.collection.should_receive(:find_one_without_identity_map).with(@selector).and_return(@model)
18
+ fetch.should be(@model)
19
+ end
20
+
21
+ it "should set document in identity map" do
22
+ MongoidIdentityMap::CurrentThreadHash.stub!(:set).with(@selector, @model)
23
+ fetch
24
+ end
25
+ end
26
+
27
+ context "when document exists in identity map" do
28
+ before do
29
+ MongoidIdentityMap::CurrentThreadHash.stub!(:get).with(@selector).and_return(@model)
30
+ end
31
+
32
+ it "should return document from identity map" do
33
+ fetch.should be(@model)
34
+ end
35
+
36
+ it "should not hit database" do
37
+ Model.collection.should_not_receive(:find_one_without_identity_map)
38
+ fetch
39
+ end
40
+ end
41
+
42
+ def fetch
43
+ MongoidIdentityMap::IdentityMap.fetch(@selector) do
44
+ Model.collection.find_one_without_identity_map(@selector)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,12 @@
1
+ require "mongoid_identity_map"
2
+ require "mongoid"
3
+ require "support/model"
4
+
5
+ RSpec.configure do |config|
6
+ config.filter_run :focus => true
7
+ config.run_all_when_everything_filtered = true
8
+
9
+ config.before(:each) do
10
+ MongoidIdentityMap::CurrentThreadHash.clear
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class Model
2
+ include Mongoid::Document
3
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_identity_map
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Guilherme Cirne
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-29 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongoid
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ version: 2.0.0
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: bson_ext
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 1
60
+ - 3
61
+ - 0
62
+ version: 1.3.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: bundler
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 1
75
+ - 0
76
+ - 0
77
+ version: 1.0.0
78
+ type: :development
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: rspec
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 2
90
+ - 5
91
+ - 0
92
+ version: 2.5.0
93
+ type: :development
94
+ version_requirements: *id005
95
+ description: Simple and transparent IdentityMap implementation for Mongoid
96
+ email:
97
+ - gcirne@gmail.com
98
+ executables: []
99
+
100
+ extensions: []
101
+
102
+ extra_rdoc_files: []
103
+
104
+ files:
105
+ - .gitignore
106
+ - .rvmrc
107
+ - Gemfile
108
+ - Rakefile
109
+ - lib/mongoid_identity_map.rb
110
+ - lib/mongoid_identity_map/clear_middleware.rb
111
+ - lib/mongoid_identity_map/current_thread_hash.rb
112
+ - lib/mongoid_identity_map/identity_map.rb
113
+ - lib/mongoid_identity_map/identity_mappable.rb
114
+ - lib/mongoid_identity_map/version.rb
115
+ - mongoid_identity_map.gemspec
116
+ - spec/mongoid_identity_map/clear_middleware_spec.rb
117
+ - spec/mongoid_identity_map/current_thread_hash_spec.rb
118
+ - spec/mongoid_identity_map/identity_map_spec.rb
119
+ - spec/mongoid_identity_map/identity_mappable_spec.rb
120
+ - spec/spec_helper.rb
121
+ - spec/support/model.rb
122
+ has_rdoc: true
123
+ homepage: http://rubygems.org/gems/mongoid_identity_map
124
+ licenses: []
125
+
126
+ post_install_message:
127
+ rdoc_options: []
128
+
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ segments:
145
+ - 1
146
+ - 3
147
+ - 6
148
+ version: 1.3.6
149
+ requirements: []
150
+
151
+ rubyforge_project: mongoid_identity_map
152
+ rubygems_version: 1.3.7
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: IdentityMap for Mongoid
156
+ test_files: []
157
+