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



31
32
33
# File 'lib/mongoo/persistence.rb', line 31

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#collectionObject



195
196
197
# File 'lib/mongoo/persistence.rb', line 195

def collection
  self.class.collection
end

#collection_nameObject



166
167
168
# File 'lib/mongoo/persistence.rb', line 166

def collection_name
  self.class.collection_name
end

#connObject



162
163
164
# File 'lib/mongoo/persistence.rb', line 162

def conn
  self.class.conn
end

#dbObject

ClassMethods



158
159
160
# File 'lib/mongoo/persistence.rb', line 158

def db
  self.class.db
end

#destroyed?Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/mongoo/persistence.rb', line 294

def destroyed?
  @destroyed != nil
end

#insert(opts = {}) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/mongoo/persistence.rb', line 199

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
rescue Mongo::OperationFailure => e
  if e.message.to_s =~ /^11000\:/
    raise Mongoo::DuplicateKeyError, e.message
  else
    raise e
  end
end

#insert!(opts = {}) ⇒ Object



231
232
233
# File 'lib/mongoo/persistence.rb', line 231

def insert!(opts={})
  insert(opts.merge(:safe => true))
end

#new_record?Boolean

Returns:

  • (Boolean)


298
299
300
# File 'lib/mongoo/persistence.rb', line 298

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


190
191
192
193
# File 'lib/mongoo/persistence.rb', line 190

def persisted?
  @persisted == true
  #!get("_id").nil?
end

#raw_update(&block) ⇒ Object



235
236
237
238
239
# File 'lib/mongoo/persistence.rb', line 235

def raw_update(&block)
  raw = RawUpdate.new(self)
  block.call(raw)
  raw.run
end

#reload(new_doc = nil) ⇒ Object



322
323
324
325
326
327
328
# File 'lib/mongoo/persistence.rb', line 322

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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/mongoo/persistence.rb', line 302

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



318
319
320
# File 'lib/mongoo/persistence.rb', line 318

def remove!(opts={})
  remove(opts.merge(:safe => true))
end

#save(*args) ⇒ Object



186
187
188
# File 'lib/mongoo/persistence.rb', line 186

def save(*args)
  persisted? ? update(*args) : insert(*args)
end

#save!(*args) ⇒ Object



182
183
184
# File 'lib/mongoo/persistence.rb', line 182

def save!(*args)
  persisted? ? update!(*args) : insert!(*args)
end

#to_keyObject



174
175
176
# File 'lib/mongoo/persistence.rb', line 174

def to_key
  get("_id")
end

#to_modelObject



178
179
180
# File 'lib/mongoo/persistence.rb', line 178

def to_model
  self
end

#to_paramObject



170
171
172
# File 'lib/mongoo/persistence.rb', line 170

def to_param
  persisted? ? get("_id").to_s : nil
end

#update(opts = {}) ⇒ Object



241
242
243
244
245
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
288
# File 'lib/mongoo/persistence.rb', line 241

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 # if opts.delete(:find_and_modify)
  end
rescue Mongo::OperationFailure => e
  if e.message.to_s =~ /^11000\:/
    raise Mongoo::DuplicateKeyError, e.message
  else
    raise e
  end
end

#update!(opts = {}) ⇒ Object



290
291
292
# File 'lib/mongoo/persistence.rb', line 290

def update!(opts={})
  update(opts.merge(:safe => true))
end