Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



375
376
377
# File 'lib/mongo_mapper/document.rb', line 375

def collection
  self.class.collection
end

#databaseObject



379
380
381
# File 'lib/mongo_mapper/document.rb', line 379

def database
  self.class.database
end

#deleteObject



398
399
400
# File 'lib/mongo_mapper/document.rb', line 398

def delete
  self.class.delete(id) unless new?
end

#destroyObject



394
395
396
# File 'lib/mongo_mapper/document.rb', line 394

def destroy
  delete
end

#destroyed?Boolean

here for ActiveModel compatibility, should do something real with this

Returns:



360
361
362
# File 'lib/mongo_mapper/document.rb', line 360

def destroyed?
  false
end

#initialize(attrs = {}, from_database = false) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/mongo_mapper/document.rb', line 351

def initialize(attrs={}, from_database=false)
  unless attrs.nil?
    provided_keys = attrs.keys.map { |k| k.to_s }
    unless provided_keys.include?('_id') || provided_keys.include?('id')
      write_key :_id, Mongo::ObjectID.new
    end
  end
  
  #here for ActiveModel compatibility, should do something real with this
  def destroyed?
    false
  end

  assign_type_if_present

  if from_database
    @new = false
    self.attributes = attrs
  else
    @new = true
    assign(attrs)
  end
end

#reloadObject



402
403
404
405
406
407
408
409
410
# File 'lib/mongo_mapper/document.rb', line 402

def reload
  if attrs = collection.find_one({:_id => _id})
    self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
    self.attributes = attrs
    self
  else
    raise DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection"
  end
end

#save(options = {}) ⇒ Object



383
384
385
386
387
# File 'lib/mongo_mapper/document.rb', line 383

def save(options={})
  options.assert_valid_keys(:validate, :safe)
  options.reverse_merge!(:validate => true)
  !options[:validate] || valid? ? create_or_update(options) : false
end

#save!(options = {}) ⇒ Object



389
390
391
392
# File 'lib/mongo_mapper/document.rb', line 389

def save!(options={})
  options.assert_valid_keys(:safe)
  save(options) || raise(DocumentNotValid.new(self))
end