Module: MongoODM::Document::Persistence::InstanceMethods
- Defined in:
- lib/mongo_odm/document/persistence.rb
Instance Method Summary collapse
- #destroy ⇒ Object
- #id ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #reload ⇒ Object
-
#save(options = {}) ⇒ ObjectId
Save a document to its collection.
- #to_dbref ⇒ Object
- #to_mongo ⇒ Object
- #update_attributes(attributes) ⇒ Object
Instance Method Details
#destroy ⇒ Object
47 48 49 50 51 52 |
# File 'lib/mongo_odm/document/persistence.rb', line 47 def destroy return false if new_record? _run_destroy_callbacks do self.class.remove({ :_id => self.id }) end end |
#id ⇒ Object
12 13 14 |
# File 'lib/mongo_odm/document/persistence.rb', line 12 def id attributes[:_id] end |
#new_record? ⇒ Boolean
16 17 18 |
# File 'lib/mongo_odm/document/persistence.rb', line 16 def new_record? id.nil? end |
#persisted? ⇒ Boolean
20 21 22 |
# File 'lib/mongo_odm/document/persistence.rb', line 20 def persisted? !new_record? end |
#reload ⇒ Object
24 25 26 |
# File 'lib/mongo_odm/document/persistence.rb', line 24 def reload self.load_attributes_or_defaults(self.class.find_one(:_id => id).attributes) unless new_record? end |
#save(options = {}) ⇒ ObjectId
Save a document to its collection.
36 37 38 39 40 |
# File 'lib/mongo_odm/document/persistence.rb', line 36 def save( = {}) _run_save_callbacks do write_attribute(:_id, self.class.save(to_mongo, )) end end |
#to_dbref ⇒ Object
61 62 63 |
# File 'lib/mongo_odm/document/persistence.rb', line 61 def to_dbref BSON::DBRef.new(self.class.collection.name, _id) end |
#to_mongo ⇒ Object
54 55 56 57 58 59 |
# File 'lib/mongo_odm/document/persistence.rb', line 54 def to_mongo attributes.inject({}) do |attrs, (key, value)| attrs[key] = value.to_mongo unless value.nil? # self.class.fields[key].default == value attrs end end |
#update_attributes(attributes) ⇒ Object
42 43 44 45 |
# File 'lib/mongo_odm/document/persistence.rb', line 42 def update_attributes(attributes) self.attributes = attributes save end |