mongoo 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +39 -0
- data/Gemfile +2 -2
- data/VERSION +1 -1
- data/lib/mongoo.rb +4 -26
- data/lib/mongoo/identity_map.rb +65 -0
- data/lib/mongoo/persistence.rb +85 -62
- data/mongoo.gemspec +10 -6
- data/test/helper.rb +10 -4
- data/test/test_identity_map.rb +76 -0
- data/test/test_mongoo.rb +42 -17
- metadata +9 -5
data/CHANGELOG
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= 0.2.0
|
2
|
+
|
3
|
+
* Depends on mongo gem >= 1.3.1
|
4
|
+
|
5
|
+
|
6
|
+
* Can no longer set Mongoo.config = {...}
|
7
|
+
|
8
|
+
Set Mongoo.conn and Mongoo.db_name instead (more flexibility):
|
9
|
+
|
10
|
+
Mongoo.conn = Mongo::Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)
|
11
|
+
Mongoo.db_name = "mongoo-test"
|
12
|
+
|
13
|
+
You can set these on a model level as well:
|
14
|
+
|
15
|
+
Person.conn = Mongo::Connection.new("localhost", 30000, :pool_size => 5, :timeout => 5)
|
16
|
+
Person.db_name = "mongoo-test"
|
17
|
+
|
18
|
+
|
19
|
+
* You can optionally set the collection name on a model now:
|
20
|
+
|
21
|
+
class Person < Mongoo::Base
|
22
|
+
collection_name "spacemen"
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
* There is a new Identity Map feature available. It will only work when using find_one to
|
27
|
+
find a specific id. You need to manually turn it on:
|
28
|
+
|
29
|
+
Mongoo::IdentityMap.on!
|
30
|
+
|
31
|
+
If using it in a web application like Rails be sure to flush the map after each request:
|
32
|
+
|
33
|
+
Mongoo::IdentityMap.flush!
|
34
|
+
|
35
|
+
The map is scoped to the current thread or fiber. You can also turn the map back off:
|
36
|
+
|
37
|
+
Mongoo::IdentityMap.off!
|
38
|
+
|
39
|
+
Inspired by: http://railstips.org/blog/archives/2010/02/21/mongomapper-07-identity-map/
|
data/Gemfile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/mongoo.rb
CHANGED
@@ -1,32 +1,9 @@
|
|
1
1
|
module Mongoo
|
2
2
|
INDEX_META = {}
|
3
3
|
ATTRIBUTE_META = {}
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
port: 27017,
|
8
|
-
db: "test",
|
9
|
-
opts: {}}.merge(@config || {})
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.config=(cfg)
|
13
|
-
@config = cfg
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.conn
|
17
|
-
@conn ||= Mongo::Connection.new(config[:host], config[:port], config[:opts])
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.db
|
21
|
-
@db ||= conn.db(config[:db])
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.verbose_debug
|
25
|
-
@verbose_debug
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.verbose_debug=(val)
|
29
|
-
@verbose_debug = val
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :conn, :db_name, :verbose_debug
|
30
7
|
end
|
31
8
|
end
|
32
9
|
|
@@ -47,3 +24,4 @@ require "mongoo/persistence"
|
|
47
24
|
require "mongoo/modifiers"
|
48
25
|
require "mongoo/base"
|
49
26
|
require "mongoo/mongohash"
|
27
|
+
require "mongoo/identity_map"
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Mongoo
|
2
|
+
class IdentityMap
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def on!
|
7
|
+
@on = true
|
8
|
+
end
|
9
|
+
|
10
|
+
def on?
|
11
|
+
@on == true
|
12
|
+
end
|
13
|
+
|
14
|
+
def off!
|
15
|
+
@on = false
|
16
|
+
if Thread.current[:mongoo]
|
17
|
+
Thread.current[:mongoo][:identity_map] = nil
|
18
|
+
end; true
|
19
|
+
end
|
20
|
+
|
21
|
+
def off?
|
22
|
+
@on == false
|
23
|
+
end
|
24
|
+
|
25
|
+
def store
|
26
|
+
return nil unless on?
|
27
|
+
Thread.current[:mongoo] ||= {}
|
28
|
+
Thread.current[:mongoo][:identity_map] ||= {}
|
29
|
+
Thread.current[:mongoo][:identity_map][:store] ||= {}
|
30
|
+
end
|
31
|
+
|
32
|
+
def simple_query?(query, opts)
|
33
|
+
return false unless opts.blank?
|
34
|
+
return true if query.is_a?(BSON::ObjectId)
|
35
|
+
return true if [[:_id], ["_id"]].include?(query.keys)
|
36
|
+
false
|
37
|
+
end
|
38
|
+
|
39
|
+
def read(id)
|
40
|
+
if store
|
41
|
+
if id.is_a?(BSON::ObjectId)
|
42
|
+
store[id.to_s]
|
43
|
+
elsif id.is_a?(Hash)
|
44
|
+
store[(id[:_id] || id["_id"]).to_s]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def write(doc)
|
50
|
+
if store
|
51
|
+
store[doc.id.to_s] = doc
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def flush!
|
56
|
+
if store
|
57
|
+
Thread.current[:mongoo][:identity_map][:store] = {}
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
data/lib/mongoo/persistence.rb
CHANGED
@@ -6,95 +6,116 @@ module Mongoo
|
|
6
6
|
class UpdateError < Exception; end
|
7
7
|
class RemoveError < Exception; end
|
8
8
|
class NotValidError < Exception; end
|
9
|
-
|
9
|
+
|
10
|
+
class DbNameNotSet < Exception; end
|
11
|
+
class ConnNotSet < Exception; end
|
12
|
+
|
10
13
|
module Persistence
|
11
|
-
|
14
|
+
|
12
15
|
def self.included(base)
|
13
16
|
base.extend(ClassMethods)
|
14
17
|
end
|
15
|
-
|
16
|
-
module ClassMethods
|
17
|
-
def collection_name
|
18
|
-
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def collection_name(val=nil)
|
21
|
+
if val
|
22
|
+
@collection_name = val
|
23
|
+
@collection = nil
|
24
|
+
@collection_name
|
25
|
+
else
|
26
|
+
@collection_name ||= self.model_name.tableize
|
27
|
+
end
|
19
28
|
end
|
20
|
-
|
29
|
+
|
21
30
|
def collection
|
22
31
|
@collection ||= db.collection(collection_name)
|
23
32
|
end
|
24
|
-
|
33
|
+
|
25
34
|
def conn
|
26
35
|
@conn ||= begin
|
27
|
-
|
28
|
-
Mongoo.conn
|
29
|
-
else
|
30
|
-
Mongo::Connection.new(config[:host], config[:port], config[:opts])
|
31
|
-
end
|
36
|
+
Mongoo.conn || raise(Mongoo::ConnNotSet)
|
32
37
|
end
|
33
38
|
end
|
34
|
-
|
35
|
-
def
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
conn.db(config[:db])
|
41
|
-
end
|
42
|
-
end
|
39
|
+
|
40
|
+
def conn=(conn)
|
41
|
+
@conn = conn
|
42
|
+
@db = nil
|
43
|
+
@collection = nil
|
44
|
+
@conn
|
43
45
|
end
|
44
|
-
|
45
|
-
def
|
46
|
-
@
|
46
|
+
|
47
|
+
def db
|
48
|
+
@db ||= conn.db(db_name)
|
47
49
|
end
|
48
|
-
|
49
|
-
def
|
50
|
-
@
|
50
|
+
|
51
|
+
def db_name
|
52
|
+
@db_name ||= begin
|
53
|
+
Mongoo.db_name || raise(Mongoo::DbNameNotSet)
|
54
|
+
end
|
51
55
|
end
|
52
|
-
|
53
|
-
def
|
54
|
-
@
|
56
|
+
|
57
|
+
def db_name=(db_name)
|
58
|
+
@db_name = db_name
|
59
|
+
@db = nil
|
60
|
+
@collection = nil
|
61
|
+
@db_name
|
55
62
|
end
|
56
|
-
|
63
|
+
|
57
64
|
def find(query={}, opts={})
|
58
65
|
Mongoo::Cursor.new(self, collection.find(query, opts))
|
59
66
|
end
|
60
|
-
|
67
|
+
|
61
68
|
def find_one(query={}, opts={})
|
62
|
-
|
63
|
-
|
69
|
+
id_map_on = Mongoo::IdentityMap.on?
|
70
|
+
is_simple_query = Mongoo::IdentityMap.simple_query?(query, opts)
|
71
|
+
|
72
|
+
if id_map_on && is_simple_query
|
73
|
+
if doc = Mongoo::IdentityMap.read(query)
|
74
|
+
return doc
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
if doc = collection.find_one(query, opts)
|
79
|
+
doc = new(doc, true)
|
80
|
+
Mongoo::IdentityMap.write(doc) if id_map_on && is_simple_query
|
81
|
+
doc
|
82
|
+
else
|
83
|
+
nil
|
84
|
+
end
|
64
85
|
end
|
65
|
-
|
86
|
+
|
66
87
|
def all
|
67
88
|
find
|
68
89
|
end
|
69
|
-
|
90
|
+
|
70
91
|
def each
|
71
92
|
find.each { |found| yield(found) }
|
72
93
|
end
|
73
|
-
|
94
|
+
|
74
95
|
def first
|
75
96
|
find.limit(1).next_document
|
76
97
|
end
|
77
|
-
|
98
|
+
|
78
99
|
def empty?
|
79
100
|
count == 0
|
80
101
|
end
|
81
|
-
|
102
|
+
|
82
103
|
def count
|
83
104
|
collection.count
|
84
105
|
end
|
85
|
-
|
106
|
+
|
86
107
|
def drop
|
87
108
|
collection.drop
|
88
109
|
end
|
89
|
-
|
110
|
+
|
90
111
|
def index_meta
|
91
112
|
Mongoo::INDEX_META[self.collection_name] ||= {}
|
92
113
|
end
|
93
|
-
|
114
|
+
|
94
115
|
def index(spec, opts={})
|
95
116
|
self.index_meta[spec] = opts
|
96
117
|
end
|
97
|
-
|
118
|
+
|
98
119
|
def create_indexes
|
99
120
|
self.index_meta.each do |spec, opts|
|
100
121
|
opts[:background] = true if !opts.has_key?(:background)
|
@@ -102,30 +123,30 @@ module Mongoo
|
|
102
123
|
end; true
|
103
124
|
end
|
104
125
|
end # ClassMethods
|
105
|
-
|
126
|
+
|
106
127
|
def to_param
|
107
128
|
persisted? ? get("_id").to_s : nil
|
108
129
|
end
|
109
|
-
|
130
|
+
|
110
131
|
def to_key
|
111
132
|
get("_id")
|
112
133
|
end
|
113
|
-
|
134
|
+
|
114
135
|
def to_model
|
115
136
|
self
|
116
137
|
end
|
117
|
-
|
138
|
+
|
118
139
|
def persisted?
|
119
140
|
@persisted == true
|
120
141
|
#!get("_id").nil?
|
121
142
|
end
|
122
|
-
|
143
|
+
|
123
144
|
def collection
|
124
145
|
self.class.collection
|
125
146
|
end
|
126
|
-
|
147
|
+
|
127
148
|
def insert(opts={})
|
128
|
-
_run_insert_callbacks do
|
149
|
+
ret = _run_insert_callbacks do
|
129
150
|
if persisted?
|
130
151
|
raise AlreadyInsertedError, "document has already been inserted"
|
131
152
|
end
|
@@ -145,12 +166,14 @@ module Mongoo
|
|
145
166
|
set_persisted_mongohash(mongohash.deep_clone)
|
146
167
|
ret
|
147
168
|
end
|
169
|
+
Mongoo::IdentityMap.write(self) if Mongoo::IdentityMap.on?
|
170
|
+
ret
|
148
171
|
end
|
149
|
-
|
172
|
+
|
150
173
|
def insert!(opts={})
|
151
174
|
insert(opts.merge(:safe => true))
|
152
175
|
end
|
153
|
-
|
176
|
+
|
154
177
|
def update(opts={})
|
155
178
|
_run_update_callbacks do
|
156
179
|
unless persisted?
|
@@ -185,19 +208,19 @@ module Mongoo
|
|
185
208
|
end
|
186
209
|
end
|
187
210
|
end
|
188
|
-
|
211
|
+
|
189
212
|
def update!(opts={})
|
190
213
|
update(opts.merge(:safe => true))
|
191
214
|
end
|
192
|
-
|
215
|
+
|
193
216
|
def destroyed?
|
194
217
|
@destroyed != nil
|
195
218
|
end
|
196
|
-
|
219
|
+
|
197
220
|
def new_record?
|
198
221
|
!persisted?
|
199
222
|
end
|
200
|
-
|
223
|
+
|
201
224
|
def remove(opts={})
|
202
225
|
_run_remove_callbacks do
|
203
226
|
unless persisted?
|
@@ -213,18 +236,18 @@ module Mongoo
|
|
213
236
|
end
|
214
237
|
end
|
215
238
|
end
|
216
|
-
|
239
|
+
|
217
240
|
def remove!(opts={})
|
218
241
|
remove(opts.merge(:safe => true))
|
219
242
|
end
|
220
|
-
|
243
|
+
|
221
244
|
def reload
|
222
245
|
init_from_hash(collection.find_one(get("_id")))
|
223
246
|
@persisted = true
|
224
247
|
set_persisted_mongohash(mongohash.deep_clone)
|
225
248
|
true
|
226
249
|
end
|
227
|
-
|
250
|
+
|
228
251
|
def build_update_hash(changelog)
|
229
252
|
update_hash = {}
|
230
253
|
changelog.each do |op, k, v|
|
@@ -234,7 +257,7 @@ module Mongoo
|
|
234
257
|
update_hash
|
235
258
|
end
|
236
259
|
protected :build_update_hash
|
237
|
-
|
260
|
+
|
238
261
|
def build_update_query_hash(persisted_mongohash_kv, changelog)
|
239
262
|
update_query_hash = {}
|
240
263
|
changelog.each do |op, k, v|
|
@@ -251,6 +274,6 @@ module Mongoo
|
|
251
274
|
update_query_hash
|
252
275
|
end
|
253
276
|
protected :build_update_query_hash
|
254
|
-
|
277
|
+
|
255
278
|
end
|
256
279
|
end
|
data/mongoo.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoo}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ben Myles"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-05-11}
|
13
13
|
s.description = %q{Simple object mapper for MongoDB}
|
14
14
|
s.email = %q{ben.myles@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".rvmrc",
|
22
|
+
"CHANGELOG",
|
22
23
|
"Gemfile",
|
23
24
|
"LICENSE.txt",
|
24
25
|
"README.rdoc",
|
@@ -32,23 +33,26 @@ Gem::Specification.new do |s|
|
|
32
33
|
"lib/mongoo/changelog.rb",
|
33
34
|
"lib/mongoo/cursor.rb",
|
34
35
|
"lib/mongoo/hash_ext.rb",
|
36
|
+
"lib/mongoo/identity_map.rb",
|
35
37
|
"lib/mongoo/modifiers.rb",
|
36
38
|
"lib/mongoo/mongohash.rb",
|
37
39
|
"lib/mongoo/persistence.rb",
|
38
40
|
"mongoo.gemspec",
|
39
41
|
"test/helper.rb",
|
40
42
|
"test/test_activemodel.rb",
|
43
|
+
"test/test_identity_map.rb",
|
41
44
|
"test/test_mongohash.rb",
|
42
45
|
"test/test_mongoo.rb"
|
43
46
|
]
|
44
47
|
s.homepage = %q{http://github.com/benmyles/mongoo}
|
45
48
|
s.licenses = ["MIT"]
|
46
49
|
s.require_paths = ["lib"]
|
47
|
-
s.rubygems_version = %q{1.6.
|
50
|
+
s.rubygems_version = %q{1.6.1}
|
48
51
|
s.summary = %q{Object mapper for MongoDB}
|
49
52
|
s.test_files = [
|
50
53
|
"test/helper.rb",
|
51
54
|
"test/test_activemodel.rb",
|
55
|
+
"test/test_identity_map.rb",
|
52
56
|
"test/test_mongohash.rb",
|
53
57
|
"test/test_mongoo.rb"
|
54
58
|
]
|
@@ -60,7 +64,7 @@ Gem::Specification.new do |s|
|
|
60
64
|
s.add_runtime_dependency(%q<i18n>, [">= 0.4.1"])
|
61
65
|
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.3"])
|
62
66
|
s.add_runtime_dependency(%q<activemodel>, [">= 3.0.3"])
|
63
|
-
s.add_runtime_dependency(%q<mongo>, [">= 1.
|
67
|
+
s.add_runtime_dependency(%q<mongo>, [">= 1.3.1"])
|
64
68
|
s.add_runtime_dependency(%q<em-synchrony>, [">= 0.2.0"])
|
65
69
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
66
70
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
@@ -70,7 +74,7 @@ Gem::Specification.new do |s|
|
|
70
74
|
s.add_dependency(%q<i18n>, [">= 0.4.1"])
|
71
75
|
s.add_dependency(%q<activesupport>, [">= 3.0.3"])
|
72
76
|
s.add_dependency(%q<activemodel>, [">= 3.0.3"])
|
73
|
-
s.add_dependency(%q<mongo>, [">= 1.
|
77
|
+
s.add_dependency(%q<mongo>, [">= 1.3.1"])
|
74
78
|
s.add_dependency(%q<em-synchrony>, [">= 0.2.0"])
|
75
79
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
76
80
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
@@ -81,7 +85,7 @@ Gem::Specification.new do |s|
|
|
81
85
|
s.add_dependency(%q<i18n>, [">= 0.4.1"])
|
82
86
|
s.add_dependency(%q<activesupport>, [">= 3.0.3"])
|
83
87
|
s.add_dependency(%q<activemodel>, [">= 3.0.3"])
|
84
|
-
s.add_dependency(%q<mongo>, [">= 1.
|
88
|
+
s.add_dependency(%q<mongo>, [">= 1.3.1"])
|
85
89
|
s.add_dependency(%q<em-synchrony>, [">= 0.2.0"])
|
86
90
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
87
91
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
data/test/helper.rb
CHANGED
@@ -20,7 +20,9 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
20
20
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
21
21
|
require 'mongoo'
|
22
22
|
|
23
|
-
Mongoo.config = {host: "127.0.0.1", port: 27017, db: 'mongoo-test'}
|
23
|
+
#Mongoo.config = {host: "127.0.0.1", port: 27017, db: 'mongoo-test'}
|
24
|
+
Mongoo.conn = Mongo::Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)
|
25
|
+
Mongoo.db_name = "mongoo-test"
|
24
26
|
|
25
27
|
class SearchIndex < Mongoo::Base
|
26
28
|
attribute "terms", :type => :array
|
@@ -39,20 +41,24 @@ class Person < Mongoo::Base
|
|
39
41
|
attribute "location.demographics.crime_rate", :type => :symbol
|
40
42
|
attribute "location.demographics.education_quality", :type => :symbol
|
41
43
|
attribute "misc", :type => :hash
|
42
|
-
|
44
|
+
|
43
45
|
index "name"
|
44
46
|
index "location.city"
|
45
47
|
end
|
46
48
|
|
49
|
+
class SpacePerson < Mongoo::Base
|
50
|
+
collection_name "spacemen"
|
51
|
+
end
|
52
|
+
|
47
53
|
class TvShow < Mongoo::Base
|
48
54
|
attribute "name", :type => :string
|
49
55
|
attribute "cast.director", :type => :string
|
50
56
|
attribute "cast.lead", :type => :string
|
51
57
|
attribute "rating", :type => :float
|
52
58
|
attribute "comments", :type => :array
|
53
|
-
|
59
|
+
|
54
60
|
index "name"
|
55
|
-
|
61
|
+
|
56
62
|
validates_presence_of "name"
|
57
63
|
validates_presence_of "cast.director"
|
58
64
|
validates_presence_of "rating"
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestIdentityMap < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
[Person, TvShow, SearchIndex].each do |obj|
|
7
|
+
obj.drop
|
8
|
+
obj.create_indexes
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
should "set and get attributes" do
|
13
|
+
p = Person.new("name" => "Ben")
|
14
|
+
p.insert!
|
15
|
+
|
16
|
+
p2 = Person.find_one(p.id)
|
17
|
+
assert_equal p, p2
|
18
|
+
assert_not_equal p.object_id, p2.object_id
|
19
|
+
|
20
|
+
Mongoo::IdentityMap.on!
|
21
|
+
|
22
|
+
p3 = Person.find_one(p.id)
|
23
|
+
p4 = Person.find_one(p.id)
|
24
|
+
assert_equal p3, p4
|
25
|
+
assert_equal p3.object_id, p4.object_id
|
26
|
+
|
27
|
+
p4.name = "Ben Myles"
|
28
|
+
assert_equal "Ben Myles", p3.name
|
29
|
+
p4.update!
|
30
|
+
assert_equal "Ben Myles", p3.name
|
31
|
+
|
32
|
+
Mongoo::IdentityMap.flush!
|
33
|
+
|
34
|
+
p50 = Person.find_one(p4.id)
|
35
|
+
|
36
|
+
p5 = Person.find_one(p4.id)
|
37
|
+
assert_not_equal p4.object_id, p5.object_id
|
38
|
+
assert_equal Person.find_one(p5.id).object_id, p5.object_id
|
39
|
+
|
40
|
+
t = Thread.new do
|
41
|
+
assert_not_equal Person.find_one(p5.id).object_id, p5.object_id
|
42
|
+
end; t.join
|
43
|
+
|
44
|
+
assert_equal "Ben Myles", p5.name
|
45
|
+
Person.collection.update({"_id" => p5.id}, { "name" => "Captain Awesome" })
|
46
|
+
assert_equal "Ben Myles", Person.find_one(p5.id).name
|
47
|
+
p5.name = "should error"
|
48
|
+
assert_raise(Mongoo::StaleUpdateError) { p5.update! }
|
49
|
+
p5.reload
|
50
|
+
p5.name = "will work now"
|
51
|
+
p5.update!
|
52
|
+
|
53
|
+
assert_equal "will work now", p5.name
|
54
|
+
assert_equal "will work now", p50.name
|
55
|
+
|
56
|
+
Mongoo::IdentityMap.off!
|
57
|
+
end
|
58
|
+
|
59
|
+
should "use id map for simple queries only" do
|
60
|
+
Mongoo::IdentityMap.on!
|
61
|
+
|
62
|
+
p = Person.new("name" => "Ben")
|
63
|
+
p.insert!
|
64
|
+
p.name = "Not Ben"
|
65
|
+
|
66
|
+
assert_equal "Not Ben", Person.find_one(p.id).name
|
67
|
+
assert_equal "Not Ben", Person.find_one({"_id" => p.id}).name
|
68
|
+
assert_equal "Not Ben", Person.find_one({:_id => p.id}).name
|
69
|
+
|
70
|
+
assert_equal "Ben", Person.find_one(p.id, {sort: [["_id",-1]]}).name
|
71
|
+
assert_equal "Ben", Person.find({"_id" => p.id}).next.name
|
72
|
+
|
73
|
+
Mongoo::IdentityMap.off!
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/test/test_mongoo.rb
CHANGED
@@ -50,7 +50,7 @@ class TestMongoo < Test::Unit::TestCase
|
|
50
50
|
p = Person.new("name" => "Ben")
|
51
51
|
p.jobs.internships.high_school = ["Sun Microsystems"]
|
52
52
|
p.insert
|
53
|
-
|
53
|
+
|
54
54
|
p = Person.find_one(p.id)
|
55
55
|
p.update
|
56
56
|
p.location.city = "San Francisco"
|
@@ -59,30 +59,30 @@ class TestMongoo < Test::Unit::TestCase
|
|
59
59
|
p.location.city = "San Diego"
|
60
60
|
assert_not_equal p.persisted_mongohash.raw_hash, p.mongohash.raw_hash
|
61
61
|
p.update
|
62
|
-
|
62
|
+
|
63
63
|
p2 = Person.find_one(p.id)
|
64
64
|
assert_equal "Ben Myles", p2.name
|
65
65
|
assert_equal "San Diego", p2.location.city
|
66
|
-
|
66
|
+
|
67
67
|
p.location.city = "Los Angeles"
|
68
68
|
p.update!
|
69
|
-
|
69
|
+
|
70
70
|
p2.location.city = "San Jose"
|
71
71
|
assert_raise(Mongoo::StaleUpdateError) { p2.update! }
|
72
72
|
p2.location.city = "San Diego"
|
73
73
|
p2.name = "Benjamin"
|
74
74
|
p2.update!
|
75
|
-
|
75
|
+
|
76
76
|
assert p2.reload
|
77
|
-
|
77
|
+
|
78
78
|
assert_equal "Los Angeles", p2.location.city
|
79
79
|
assert_equal "Benjamin", p2.name
|
80
|
-
|
80
|
+
|
81
81
|
assert p2.persisted_mongohash.raw_hash["location"].has_key?("city")
|
82
82
|
p2.unset "location.city"
|
83
83
|
p2.update
|
84
84
|
assert !p2.persisted_mongohash.raw_hash["location"].has_key?("city")
|
85
|
-
|
85
|
+
|
86
86
|
p2.location.demographics.crime_rate = :high
|
87
87
|
p2.location.city = "San Bruno"
|
88
88
|
p2.update
|
@@ -92,7 +92,7 @@ class TestMongoo < Test::Unit::TestCase
|
|
92
92
|
p2.update
|
93
93
|
p2 = Person.find_one(p2.id)
|
94
94
|
assert !p2.persisted_mongohash.raw_hash.has_key?("location")
|
95
|
-
|
95
|
+
|
96
96
|
p2.location.city = "Brisbane"
|
97
97
|
p2.location.demographics.crime_rate = :low
|
98
98
|
p2.update
|
@@ -104,7 +104,7 @@ class TestMongoo < Test::Unit::TestCase
|
|
104
104
|
assert !p2.persisted_mongohash.raw_hash.has_key?("location")
|
105
105
|
p2 = Person.find_one(p2.id)
|
106
106
|
assert !p2.persisted_mongohash.raw_hash.has_key?("location")
|
107
|
-
|
107
|
+
|
108
108
|
p2.location.city = "Brisbane"
|
109
109
|
p2.location.demographics.crime_rate = :low
|
110
110
|
p2.update
|
@@ -140,7 +140,7 @@ class TestMongoo < Test::Unit::TestCase
|
|
140
140
|
assert_equal 15, p.jobs.total
|
141
141
|
p = Person.find_one(p.id)
|
142
142
|
assert_equal 15, p.jobs.total
|
143
|
-
|
143
|
+
|
144
144
|
assert_equal nil, p.interests
|
145
145
|
p.mod! { |mod| mod.push("interests", "skydiving") }
|
146
146
|
assert_equal ["skydiving"], p.interests
|
@@ -150,25 +150,25 @@ class TestMongoo < Test::Unit::TestCase
|
|
150
150
|
assert_equal ["skydiving", "snowboarding"], p.interests
|
151
151
|
p = Person.find_one(p.id)
|
152
152
|
assert_equal ["skydiving", "snowboarding"], p.interests
|
153
|
-
|
153
|
+
|
154
154
|
p.mod! { |mod| mod.push_all("interests", ["reading","travelling"]) }
|
155
155
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling"], p.interests
|
156
156
|
p = Person.find_one(p.id)
|
157
157
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling"], p.interests
|
158
|
-
|
158
|
+
|
159
159
|
p.mod! { |mod| mod.add_to_set("interests", "skydiving") }
|
160
160
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling"], p.interests
|
161
161
|
p.mod! { |mod| mod.add_to_set("interests", "swimming") }
|
162
162
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling", "swimming"], p.interests
|
163
163
|
p.mod! { |mod| mod.add_to_set("interests", "swimming") }
|
164
164
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling", "swimming"], p.interests
|
165
|
-
|
165
|
+
|
166
166
|
p.mod! { |mod| mod.pop("interests") }
|
167
167
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling"], p.interests
|
168
|
-
|
168
|
+
|
169
169
|
p.mod! { |mod| mod.pop("interests") }
|
170
170
|
assert_equal ["skydiving", "snowboarding", "reading"], p.interests
|
171
|
-
|
171
|
+
|
172
172
|
p.mod! { |mod| mod.push("interests", "reading") }
|
173
173
|
assert_equal ["skydiving", "snowboarding", "reading", "reading"], p.interests
|
174
174
|
p = Person.find_one(p.id)
|
@@ -182,7 +182,7 @@ class TestMongoo < Test::Unit::TestCase
|
|
182
182
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling"], p.interests
|
183
183
|
p = Person.find_one(p.id)
|
184
184
|
assert_equal ["skydiving", "snowboarding", "reading", "travelling"], p.interests
|
185
|
-
|
185
|
+
|
186
186
|
p.mod! { |mod| mod.pull_all("interests", ["reading", "skydiving"]) }
|
187
187
|
assert_equal ["snowboarding", "travelling"], p.interests
|
188
188
|
p = Person.find_one(p.id)
|
@@ -272,5 +272,30 @@ class TestMongoo < Test::Unit::TestCase
|
|
272
272
|
i.terms << "foo"
|
273
273
|
i.update!
|
274
274
|
end
|
275
|
+
|
276
|
+
should "work when forked" do
|
277
|
+
Person.new(:name => "Ben").insert!
|
278
|
+
fork { assert_equal "Ben", Person.find_one({name: "Ben"}).name }
|
279
|
+
Process.wait
|
280
|
+
end
|
281
|
+
|
282
|
+
should "work when threaded" do
|
283
|
+
1.upto(10) do |i|
|
284
|
+
Person.new(:name => "Ben#{i}").insert!
|
285
|
+
end
|
286
|
+
|
287
|
+
threads = []
|
288
|
+
100.times do |count|
|
289
|
+
threads << Thread.new do
|
290
|
+
(1..10).to_a.shuffle { |i| assert_equal "Ben#{i}", Person.find_one({name: "Ben#{i}"}).name }
|
291
|
+
end
|
292
|
+
end
|
293
|
+
threads.each { |th| th.join }
|
294
|
+
end
|
295
|
+
|
296
|
+
should "be able to set the collection name manually" do
|
297
|
+
assert_equal "people", Person.collection.name
|
298
|
+
assert_equal "spacemen", SpacePerson.collection.name
|
299
|
+
end
|
275
300
|
end
|
276
301
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ben Myles
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-11 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
requirements:
|
54
54
|
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: 1.
|
56
|
+
version: 1.3.1
|
57
57
|
type: :runtime
|
58
58
|
prerelease: false
|
59
59
|
version_requirements: *id004
|
@@ -124,6 +124,7 @@ extra_rdoc_files:
|
|
124
124
|
files:
|
125
125
|
- .document
|
126
126
|
- .rvmrc
|
127
|
+
- CHANGELOG
|
127
128
|
- Gemfile
|
128
129
|
- LICENSE.txt
|
129
130
|
- README.rdoc
|
@@ -137,12 +138,14 @@ files:
|
|
137
138
|
- lib/mongoo/changelog.rb
|
138
139
|
- lib/mongoo/cursor.rb
|
139
140
|
- lib/mongoo/hash_ext.rb
|
141
|
+
- lib/mongoo/identity_map.rb
|
140
142
|
- lib/mongoo/modifiers.rb
|
141
143
|
- lib/mongoo/mongohash.rb
|
142
144
|
- lib/mongoo/persistence.rb
|
143
145
|
- mongoo.gemspec
|
144
146
|
- test/helper.rb
|
145
147
|
- test/test_activemodel.rb
|
148
|
+
- test/test_identity_map.rb
|
146
149
|
- test/test_mongohash.rb
|
147
150
|
- test/test_mongoo.rb
|
148
151
|
has_rdoc: true
|
@@ -159,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
162
|
requirements:
|
160
163
|
- - ">="
|
161
164
|
- !ruby/object:Gem::Version
|
162
|
-
hash: -
|
165
|
+
hash: -1099798970353591123
|
163
166
|
segments:
|
164
167
|
- 0
|
165
168
|
version: "0"
|
@@ -172,12 +175,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
175
|
requirements: []
|
173
176
|
|
174
177
|
rubyforge_project:
|
175
|
-
rubygems_version: 1.6.
|
178
|
+
rubygems_version: 1.6.1
|
176
179
|
signing_key:
|
177
180
|
specification_version: 3
|
178
181
|
summary: Object mapper for MongoDB
|
179
182
|
test_files:
|
180
183
|
- test/helper.rb
|
181
184
|
- test/test_activemodel.rb
|
185
|
+
- test/test_identity_map.rb
|
182
186
|
- test/test_mongohash.rb
|
183
187
|
- test/test_mongoo.rb
|