Module: MongoMapper::Document::InstanceMethods
- Defined in:
- lib/mongo_mapper/document.rb
Instance Method Summary collapse
- #collection ⇒ Object
- #database ⇒ Object
- #delete ⇒ Object
- #destroy ⇒ Object
-
#destroyed? ⇒ Boolean
here for ActiveModel compatibility, should do something real with this.
- #initialize(attrs = {}, from_database = false) ⇒ Object
- #reload ⇒ Object
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
Instance Method Details
#collection ⇒ Object
375 376 377 |
# File 'lib/mongo_mapper/document.rb', line 375 def collection self.class.collection end |
#database ⇒ Object
379 380 381 |
# File 'lib/mongo_mapper/document.rb', line 379 def database self.class.database end |
#delete ⇒ Object
398 399 400 |
# File 'lib/mongo_mapper/document.rb', line 398 def delete self.class.delete(id) unless new? end |
#destroy ⇒ Object
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
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 |
#reload ⇒ Object
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(={}) .assert_valid_keys(:validate, :safe) .reverse_merge!(:validate => true) ![:validate] || valid? ? create_or_update() : false end |
#save!(options = {}) ⇒ Object
389 390 391 392 |
# File 'lib/mongo_mapper/document.rb', line 389 def save!(={}) .assert_valid_keys(:safe) save() || raise(DocumentNotValid.new(self)) end |