mongo_doc 0.6.4 → 0.6.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +1 -1
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/mongo_doc/document.rb +18 -3
- data/lib/mongo_doc/railtie.rb +4 -0
- data/lib/mongo_doc/railties/db_prepare.task +9 -0
- data/lib/mongo_doc.rb +1 -1
- data/mongo_doc.gemspec +4 -1
- data/spec/document_spec.rb +0 -120
- data/spec/update_spec.rb +162 -0
- metadata +6 -3
data/README.textile
CHANGED
data/Rakefile
CHANGED
@@ -9,6 +9,7 @@ begin
|
|
9
9
|
gem.email = "leshill@gmail.com"
|
10
10
|
gem.homepage = "http://github.com/leshill/mongodoc"
|
11
11
|
gem.authors = ["Les Hill"]
|
12
|
+
gem.files.include('lib/**/*.task')
|
12
13
|
gem.add_dependency "activesupport", ">= 3.0.0.beta.4"
|
13
14
|
gem.add_dependency "activemodel", ">=3.0.0.beta.4"
|
14
15
|
gem.add_dependency "mongo", ">= 1.0.0"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.5
|
data/lib/mongo_doc/document.rb
CHANGED
@@ -101,14 +101,25 @@ module MongoDoc
|
|
101
101
|
self.attributes = attrs
|
102
102
|
return save if new_record?
|
103
103
|
return false unless valid?
|
104
|
-
|
104
|
+
_update_attributes(attrs, false)
|
105
105
|
end
|
106
106
|
|
107
107
|
def update_attributes!(attrs)
|
108
108
|
self.attributes = attrs
|
109
109
|
return save! if new_record?
|
110
110
|
raise DocumentInvalidError unless valid?
|
111
|
-
|
111
|
+
_update_attributes(attrs, true)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Update without checking validations. The +Document+ will be saved without validations if it is a new record.
|
115
|
+
def update(attrs, safe = false)
|
116
|
+
self.attributes = attrs
|
117
|
+
if new_record?
|
118
|
+
_root.send(:_save, safe) if _root
|
119
|
+
_save(safe)
|
120
|
+
else
|
121
|
+
_update_attributes(attrs, safe)
|
122
|
+
end
|
112
123
|
end
|
113
124
|
|
114
125
|
module ClassMethods
|
@@ -143,12 +154,16 @@ module MongoDoc
|
|
143
154
|
|
144
155
|
protected
|
145
156
|
|
157
|
+
def _update_attributes(attrs, safe)
|
158
|
+
return _root.send(:_update, {_selector_path + '._id' => _id}, hash_with_modifier_path_keys(attrs), safe) if _root
|
159
|
+
_update({}, attrs, safe)
|
160
|
+
end
|
161
|
+
|
146
162
|
def _remove
|
147
163
|
_collection.remove({'_id' => _id})
|
148
164
|
end
|
149
165
|
|
150
166
|
def _update(selector, data, safe)
|
151
|
-
return _root.send(:_update, {_selector_path + '._id' => _id}, hash_with_modifier_path_keys(data), safe) if _root
|
152
167
|
_collection.update({'_id' => _id}.merge(selector), {'$set' => data}, :safe => safe)
|
153
168
|
end
|
154
169
|
|
data/lib/mongo_doc/railtie.rb
CHANGED
data/lib/mongo_doc.rb
CHANGED
data/mongo_doc.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_doc}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Les Hill"]
|
@@ -102,6 +102,7 @@ Gem::Specification.new do |s|
|
|
102
102
|
"lib/mongo_doc/matchers.rb",
|
103
103
|
"lib/mongo_doc/railtie.rb",
|
104
104
|
"lib/mongo_doc/railties/config.rb",
|
105
|
+
"lib/mongo_doc/railties/db_prepare.task",
|
105
106
|
"lib/mongo_doc/root.rb",
|
106
107
|
"lib/mongo_doc/scope.rb",
|
107
108
|
"lib/mongo_doc/timestamps.rb",
|
@@ -166,6 +167,7 @@ Gem::Specification.new do |s|
|
|
166
167
|
"spec/scope_spec.rb",
|
167
168
|
"spec/spec_helper.rb",
|
168
169
|
"spec/timestamps_spec.rb",
|
170
|
+
"spec/update_spec.rb",
|
169
171
|
"spec/validations_spec.rb"
|
170
172
|
]
|
171
173
|
s.homepage = %q{http://github.com/leshill/mongodoc}
|
@@ -205,6 +207,7 @@ Gem::Specification.new do |s|
|
|
205
207
|
"spec/scope_spec.rb",
|
206
208
|
"spec/spec_helper.rb",
|
207
209
|
"spec/timestamps_spec.rb",
|
210
|
+
"spec/update_spec.rb",
|
208
211
|
"spec/validations_spec.rb",
|
209
212
|
"examples/simple_document.rb",
|
210
213
|
"examples/simple_object.rb"
|
data/spec/document_spec.rb
CHANGED
@@ -259,126 +259,6 @@ describe "MongoDoc::Document" do
|
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
262
|
-
context "updating attributes" do
|
263
|
-
class UpdateAttributesChild
|
264
|
-
include MongoDoc::Document
|
265
|
-
|
266
|
-
attr_accessor :child_data
|
267
|
-
end
|
268
|
-
|
269
|
-
class UpdateAttributes
|
270
|
-
include MongoDoc::Document
|
271
|
-
|
272
|
-
attr_accessor :data
|
273
|
-
embed :child
|
274
|
-
end
|
275
|
-
|
276
|
-
let(:collection) { stub(:update => nil) }
|
277
|
-
|
278
|
-
let(:new_doc) { UpdateAttributes.new }
|
279
|
-
|
280
|
-
let(:existing_doc) do
|
281
|
-
doc = UpdateAttributes.new
|
282
|
-
doc._id = 'exists'
|
283
|
-
doc.stub(:_collection).and_return(collection)
|
284
|
-
doc
|
285
|
-
end
|
286
|
-
|
287
|
-
let(:child) do
|
288
|
-
child = UpdateAttributesChild.new
|
289
|
-
child._id = 'child exists'
|
290
|
-
existing_doc.child = child
|
291
|
-
child
|
292
|
-
end
|
293
|
-
|
294
|
-
describe "#update_attributes" do
|
295
|
-
it "delegates to save if the doc is a new record" do
|
296
|
-
new_doc.should_receive(:save)
|
297
|
-
new_doc.update_attributes(:data => 'data')
|
298
|
-
end
|
299
|
-
|
300
|
-
context "with an existing doc" do
|
301
|
-
|
302
|
-
subject { existing_doc.update_attributes(:data => 'data') }
|
303
|
-
|
304
|
-
it "sets the attributes" do
|
305
|
-
subject
|
306
|
-
existing_doc.data.should == 'data'
|
307
|
-
end
|
308
|
-
|
309
|
-
it "validates the doc" do
|
310
|
-
existing_doc.should_receive(:valid?)
|
311
|
-
subject
|
312
|
-
end
|
313
|
-
|
314
|
-
it "returns false if the doc is not valid" do
|
315
|
-
existing_doc.stub(:valid?).and_return(false)
|
316
|
-
should be_false
|
317
|
-
end
|
318
|
-
|
319
|
-
it "delegates to collection update" do
|
320
|
-
collection.should_receive(:update).with({'_id' => existing_doc._id}, {'$set' => {:data => 'data'}}, :safe => false)
|
321
|
-
subject
|
322
|
-
end
|
323
|
-
|
324
|
-
context "that is embedded" do
|
325
|
-
it "delegates to the root's collection update" do
|
326
|
-
collection.should_receive(:update).with({'_id' => existing_doc._id, 'child._id' => child._id}, {'$set' => {'child.child_data' => 'data'}}, :safe => false)
|
327
|
-
child.update_attributes(:child_data => 'data')
|
328
|
-
end
|
329
|
-
end
|
330
|
-
end
|
331
|
-
end
|
332
|
-
|
333
|
-
describe "#update_attributes!" do
|
334
|
-
it "delegates to save! if the doc is a new record" do
|
335
|
-
new_doc.should_receive(:save!)
|
336
|
-
new_doc.update_attributes!(:data => 'data')
|
337
|
-
end
|
338
|
-
|
339
|
-
context "with an existing doc" do
|
340
|
-
|
341
|
-
subject { existing_doc.update_attributes!(:data => 'data') }
|
342
|
-
|
343
|
-
it "sets the attributes" do
|
344
|
-
subject
|
345
|
-
existing_doc.data.should == 'data'
|
346
|
-
end
|
347
|
-
|
348
|
-
it "validates the doc" do
|
349
|
-
existing_doc.should_receive(:valid?).and_return(true)
|
350
|
-
subject
|
351
|
-
end
|
352
|
-
|
353
|
-
it "raises if not valid" do
|
354
|
-
existing_doc.stub(:valid?).and_return(false)
|
355
|
-
expect do
|
356
|
-
subject
|
357
|
-
end.should raise_error(MongoDoc::DocumentInvalidError)
|
358
|
-
end
|
359
|
-
|
360
|
-
it "delegates to collection update" do
|
361
|
-
collection.should_receive(:update).with({'_id' => existing_doc._id}, {'$set' => {:data => 'data'}}, :safe => true)
|
362
|
-
subject
|
363
|
-
end
|
364
|
-
|
365
|
-
context "that is embedded" do
|
366
|
-
it "delegates to the root's collection update" do
|
367
|
-
collection.should_receive(:update).with({'_id' => existing_doc._id, 'child._id' => child._id}, {'$set' => {'child.child_data' => 'data'}}, :safe => true)
|
368
|
-
child.update_attributes!(:child_data => 'data')
|
369
|
-
end
|
370
|
-
end
|
371
|
-
end
|
372
|
-
end
|
373
|
-
|
374
|
-
describe "#hash_with_modifier_path_keys" do
|
375
|
-
it "returns a hash with the keys prepended with the modifier path" do
|
376
|
-
new_doc.stub(:_modifier_path).and_return('path.to.root')
|
377
|
-
new_doc.send(:hash_with_modifier_path_keys, :name => 1).should == {'path.to.root.name' => 1}
|
378
|
-
end
|
379
|
-
end
|
380
|
-
end
|
381
|
-
|
382
262
|
describe "bson" do
|
383
263
|
class BSONTest
|
384
264
|
include MongoDoc::Document
|
data/spec/update_spec.rb
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "MongoDoc::Document" do
|
4
|
+
|
5
|
+
class UpdateAttributesChild
|
6
|
+
include MongoDoc::Document
|
7
|
+
|
8
|
+
attr_accessor :child_data
|
9
|
+
end
|
10
|
+
|
11
|
+
class UpdateAttributes
|
12
|
+
include MongoDoc::Document
|
13
|
+
|
14
|
+
attr_accessor :data
|
15
|
+
embed :child
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:collection) { stub(:save => nil, :update => nil) }
|
19
|
+
let(:child) do
|
20
|
+
child = UpdateAttributesChild.new
|
21
|
+
child._id = 'child exists'
|
22
|
+
existing_doc.child = child
|
23
|
+
child
|
24
|
+
end
|
25
|
+
let(:existing_doc) do
|
26
|
+
doc = UpdateAttributes.new
|
27
|
+
doc._id = 'exists'
|
28
|
+
doc.stub(:_collection).and_return(collection)
|
29
|
+
doc
|
30
|
+
end
|
31
|
+
let(:new_child) do
|
32
|
+
child = UpdateAttributesChild.new
|
33
|
+
existing_doc.child = child
|
34
|
+
child
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:new_doc) { UpdateAttributes.new }
|
38
|
+
|
39
|
+
describe "#update" do
|
40
|
+
context "with a new doc" do
|
41
|
+
it "delegates to save! if the doc is a new record" do
|
42
|
+
new_doc.should_receive(:_save)
|
43
|
+
new_doc.update(:data => 'data')
|
44
|
+
end
|
45
|
+
|
46
|
+
it "delegates to the root's save if the child is a new record" do
|
47
|
+
existing_doc.should_receive(:_save)
|
48
|
+
new_child.update(:child_data => 'data')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with an existing doc" do
|
53
|
+
|
54
|
+
subject { existing_doc.update(:data => 'data') }
|
55
|
+
|
56
|
+
it "sets the attributes" do
|
57
|
+
subject
|
58
|
+
existing_doc.data.should == 'data'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "delegates to collection update" do
|
62
|
+
collection.should_receive(:update).with({'_id' => existing_doc._id}, {'$set' => {:data => 'data'}}, :safe => false)
|
63
|
+
subject
|
64
|
+
end
|
65
|
+
|
66
|
+
context "that is embedded" do
|
67
|
+
it "delegates to the root's collection update" do
|
68
|
+
collection.should_receive(:update).with({'_id' => existing_doc._id, 'child._id' => child._id}, {'$set' => {'child.child_data' => 'data'}}, :safe => true)
|
69
|
+
child.update_attributes!(:child_data => 'data')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#update_attributes" do
|
76
|
+
it "delegates to save if the doc is a new record" do
|
77
|
+
new_doc.should_receive(:save)
|
78
|
+
new_doc.update_attributes(:data => 'data')
|
79
|
+
end
|
80
|
+
|
81
|
+
context "with an existing doc" do
|
82
|
+
|
83
|
+
subject { existing_doc.update_attributes(:data => 'data') }
|
84
|
+
|
85
|
+
it "sets the attributes" do
|
86
|
+
subject
|
87
|
+
existing_doc.data.should == 'data'
|
88
|
+
end
|
89
|
+
|
90
|
+
it "validates the doc" do
|
91
|
+
existing_doc.should_receive(:valid?)
|
92
|
+
subject
|
93
|
+
end
|
94
|
+
|
95
|
+
it "returns false if the doc is not valid" do
|
96
|
+
existing_doc.stub(:valid?).and_return(false)
|
97
|
+
should be_false
|
98
|
+
end
|
99
|
+
|
100
|
+
it "delegates to collection update" do
|
101
|
+
collection.should_receive(:update).with({'_id' => existing_doc._id}, {'$set' => {:data => 'data'}}, :safe => false)
|
102
|
+
subject
|
103
|
+
end
|
104
|
+
|
105
|
+
context "that is embedded" do
|
106
|
+
it "delegates to the root's collection update" do
|
107
|
+
collection.should_receive(:update).with({'_id' => existing_doc._id, 'child._id' => child._id}, {'$set' => {'child.child_data' => 'data'}}, :safe => false)
|
108
|
+
child.update_attributes(:child_data => 'data')
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "#update_attributes!" do
|
115
|
+
it "delegates to save! if the doc is a new record" do
|
116
|
+
new_doc.should_receive(:save!)
|
117
|
+
new_doc.update_attributes!(:data => 'data')
|
118
|
+
end
|
119
|
+
|
120
|
+
context "with an existing doc" do
|
121
|
+
|
122
|
+
subject { existing_doc.update_attributes!(:data => 'data') }
|
123
|
+
|
124
|
+
it "sets the attributes" do
|
125
|
+
subject
|
126
|
+
existing_doc.data.should == 'data'
|
127
|
+
end
|
128
|
+
|
129
|
+
it "validates the doc" do
|
130
|
+
existing_doc.should_receive(:valid?).and_return(true)
|
131
|
+
subject
|
132
|
+
end
|
133
|
+
|
134
|
+
it "raises if not valid" do
|
135
|
+
existing_doc.stub(:valid?).and_return(false)
|
136
|
+
expect do
|
137
|
+
subject
|
138
|
+
end.should raise_error(MongoDoc::DocumentInvalidError)
|
139
|
+
end
|
140
|
+
|
141
|
+
it "delegates to collection update" do
|
142
|
+
collection.should_receive(:update).with({'_id' => existing_doc._id}, {'$set' => {:data => 'data'}}, :safe => true)
|
143
|
+
subject
|
144
|
+
end
|
145
|
+
|
146
|
+
context "that is embedded" do
|
147
|
+
it "delegates to the root's collection update" do
|
148
|
+
collection.should_receive(:update).with({'_id' => existing_doc._id, 'child._id' => child._id}, {'$set' => {'child.child_data' => 'data'}}, :safe => true)
|
149
|
+
child.update_attributes!(:child_data => 'data')
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "#hash_with_modifier_path_keys" do
|
156
|
+
it "returns a hash with the keys prepended with the modifier path" do
|
157
|
+
new_doc.stub(:_modifier_path).and_return('path.to.root')
|
158
|
+
new_doc.send(:hash_with_modifier_path_keys, :name => 1).should == {'path.to.root.name' => 1}
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_doc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 5
|
10
|
+
version: 0.6.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Les Hill
|
@@ -231,6 +231,7 @@ files:
|
|
231
231
|
- lib/mongo_doc/matchers.rb
|
232
232
|
- lib/mongo_doc/railtie.rb
|
233
233
|
- lib/mongo_doc/railties/config.rb
|
234
|
+
- lib/mongo_doc/railties/db_prepare.task
|
234
235
|
- lib/mongo_doc/root.rb
|
235
236
|
- lib/mongo_doc/scope.rb
|
236
237
|
- lib/mongo_doc/timestamps.rb
|
@@ -295,6 +296,7 @@ files:
|
|
295
296
|
- spec/scope_spec.rb
|
296
297
|
- spec/spec_helper.rb
|
297
298
|
- spec/timestamps_spec.rb
|
299
|
+
- spec/update_spec.rb
|
298
300
|
- spec/validations_spec.rb
|
299
301
|
has_rdoc: true
|
300
302
|
homepage: http://github.com/leshill/mongodoc
|
@@ -362,6 +364,7 @@ test_files:
|
|
362
364
|
- spec/scope_spec.rb
|
363
365
|
- spec/spec_helper.rb
|
364
366
|
- spec/timestamps_spec.rb
|
367
|
+
- spec/update_spec.rb
|
365
368
|
- spec/validations_spec.rb
|
366
369
|
- examples/simple_document.rb
|
367
370
|
- examples/simple_object.rb
|