Module: MongoODM::Document::Persistence::InstanceMethods

Defined in:
lib/mongo_odm/document/persistence.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) destroy



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

- (Object) id



12
13
14
# File 'lib/mongo_odm/document/persistence.rb', line 12

def id
  attributes[:_id]
end

- (Boolean) new_record?



16
17
18
# File 'lib/mongo_odm/document/persistence.rb', line 16

def new_record?
  id.nil?
end

- (Boolean) persisted?



20
21
22
# File 'lib/mongo_odm/document/persistence.rb', line 20

def persisted?
  !new_record?
end

- (Object) reload



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

- (ObjectId) save(options = {})

Save a document to its collection.



36
37
38
39
40
# File 'lib/mongo_odm/document/persistence.rb', line 36

def save(options = {})
  _run_save_callbacks do
    write_attribute(:_id, self.class.save(to_mongo, options))
  end
end

- (Object) to_dbref



61
62
63
# File 'lib/mongo_odm/document/persistence.rb', line 61

def to_dbref
  BSON::DBRef.new(self.class.collection.name, _id)
end

- (Object) to_mongo



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

- (Object) update_attributes(attributes)



42
43
44
45
# File 'lib/mongo_odm/document/persistence.rb', line 42

def update_attributes(attributes)
  self.attributes = attributes
  save
end