Module: MongoMapper::Document::InstanceMethods

Defined in:
lib/mongomapper/document.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



224
225
226
# File 'lib/mongomapper/document.rb', line 224

def ==(other)
  other.is_a?(self.class) && id == other.id
end

#collectionObject



197
198
199
# File 'lib/mongomapper/document.rb', line 197

def collection
  self.class.collection
end

#destroyObject



219
220
221
222
# File 'lib/mongomapper/document.rb', line 219

def destroy
  collection.remove(:_id => id) unless new?
  freeze
end

#idObject



228
229
230
# File 'lib/mongomapper/document.rb', line 228

def id
  read_attribute('_id')
end

#new?Boolean

Returns:



201
202
203
# File 'lib/mongomapper/document.rb', line 201

def new?
  read_attribute('_id').blank? || self.class.find_by_id(id).blank?
end

#saveObject



205
206
207
# File 'lib/mongomapper/document.rb', line 205

def save
  create_or_update
end

#save!Object



209
210
211
# File 'lib/mongomapper/document.rb', line 209

def save!
  create_or_update || raise(DocumentNotValid.new(self))
end

#update_attributes(attrs = {}) ⇒ Object



213
214
215
216
217
# File 'lib/mongomapper/document.rb', line 213

def update_attributes(attrs={})
  self.attributes = attrs
  save
  self
end