Module: Mongoo::Persistence
- Included in:
- Base
- Defined in:
- lib/mongoo/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Classes: RawUpdate
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
42
43
44
|
# File 'lib/mongoo/persistence.rb', line 42
def self.included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#collection ⇒ Object
206
207
208
|
# File 'lib/mongoo/persistence.rb', line 206
def collection
self.class.collection
end
|
#collection_name ⇒ Object
177
178
179
|
# File 'lib/mongoo/persistence.rb', line 177
def collection_name
self.class.collection_name
end
|
#conn ⇒ Object
173
174
175
|
# File 'lib/mongoo/persistence.rb', line 173
def conn
self.class.conn
end
|
#db ⇒ Object
169
170
171
|
# File 'lib/mongoo/persistence.rb', line 169
def db
self.class.db
end
|
#destroyed? ⇒ Boolean
293
294
295
|
# File 'lib/mongoo/persistence.rb', line 293
def destroyed?
@destroyed != nil
end
|
#insert(opts = {}) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/mongoo/persistence.rb', line 210
def insert(opts={})
ret = _run_insert_callbacks do
if persisted?
raise AlreadyInsertedError, "document has already been inserted"
end
unless valid?
if opts[:safe] == true
raise Mongoo::NotValidError, "document contains errors"
else
return false
end
end
ret = self.collection.insert(mongohash, opts)
unless ret.is_a?(BSON::ObjectId)
raise InsertError, "not an object: #{ret.inspect}"
end
mongohash.delete(:_id)
set("_id", ret)
@persisted = true
set_persisted_mongohash(mongohash)
ret
end
Mongoo::IdentityMap.write(self) if Mongoo::IdentityMap.on?
ret
end
|
#insert!(opts = {}) ⇒ Object
236
237
238
|
# File 'lib/mongoo/persistence.rb', line 236
def insert!(opts={})
insert(opts.merge(:safe => true))
end
|
#new_record? ⇒ Boolean
297
298
299
|
# File 'lib/mongoo/persistence.rb', line 297
def new_record?
!persisted?
end
|
#persisted? ⇒ Boolean
201
202
203
204
|
# File 'lib/mongoo/persistence.rb', line 201
def persisted?
@persisted == true
end
|
#raw_update(&block) ⇒ Object
240
241
242
243
244
|
# File 'lib/mongoo/persistence.rb', line 240
def raw_update(&block)
raw = RawUpdate.new(self)
block.call(raw)
raw.run
end
|
#reload(new_doc = nil) ⇒ Object
321
322
323
324
325
326
327
|
# File 'lib/mongoo/persistence.rb', line 321
def reload(new_doc=nil)
new_doc ||= collection.find_one(get("_id"))
init_from_hash(new_doc)
@persisted = true
set_persisted_mongohash(mongohash)
true
end
|
#remove(opts = {}) ⇒ Object
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/mongoo/persistence.rb', line 301
def remove(opts={})
_run_remove_callbacks do
unless persisted?
raise NotInsertedError, "document must be inserted before it can be removed"
end
ret = self.collection.remove({"_id" => get("_id")}, opts)
if !ret.is_a?(Hash) || (ret["err"] == nil && ret["n"] == 1)
@destroyed = true
@persisted = false
true
else
raise RemoveError, ret.inspect
end
end
end
|
#remove!(opts = {}) ⇒ Object
317
318
319
|
# File 'lib/mongoo/persistence.rb', line 317
def remove!(opts={})
remove(opts.merge(:safe => true))
end
|
#save(*args) ⇒ Object
197
198
199
|
# File 'lib/mongoo/persistence.rb', line 197
def save(*args)
persisted? ? update(*args) : insert(*args)
end
|
#save!(*args) ⇒ Object
193
194
195
|
# File 'lib/mongoo/persistence.rb', line 193
def save!(*args)
persisted? ? update!(*args) : insert!(*args)
end
|
#to_key ⇒ Object
185
186
187
|
# File 'lib/mongoo/persistence.rb', line 185
def to_key
get("_id")
end
|
#to_model ⇒ Object
189
190
191
|
# File 'lib/mongoo/persistence.rb', line 189
def to_model
self
end
|
#to_param ⇒ Object
181
182
183
|
# File 'lib/mongoo/persistence.rb', line 181
def to_param
persisted? ? get("_id").to_s : nil
end
|
#update(opts = {}) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/mongoo/persistence.rb', line 246
def update(opts={})
_run_update_callbacks do
unless persisted?
raise NotInsertedError, "document must be inserted before being updated"
end
unless valid?
if opts[:safe] == true
raise Mongoo::NotValidError, "document contains errors"
else
return false
end
end
opts[:only_if_current] = true unless opts.has_key?(:only_if_current)
opts[:safe] = true if !opts.has_key?(:safe) && opts[:only_if_current] == true
update_hash = build_update_hash(self.changelog)
return true if update_hash.empty?
update_query_hash = build_update_query_hash(persisted_mongohash.to_key_value, self.changelog)
if Mongoo.verbose_debug
puts "\n* update_query_hash: #{update_query_hash.inspect}\n update_hash: #{update_hash.inspect}\n opts: #{opts.inspect}\n"
end
if opts.delete(:find_and_modify) == true
ret = self.collection.find_and_modify(query: update_query_hash,
update: update_hash,
new: true)
reload(ret)
else
ret = self.collection.update(update_query_hash, update_hash, opts)
if !ret.is_a?(Hash) || (ret["updatedExisting"] && ret["n"] == 1)
reset_persisted_mongohash
true
else
if opts[:only_if_current]
raise StaleUpdateError, ret.inspect
else
raise UpdateError, ret.inspect
end
end
end end
end
|
#update!(opts = {}) ⇒ Object
289
290
291
|
# File 'lib/mongoo/persistence.rb', line 289
def update!(opts={})
update(opts.merge(:safe => true))
end
|