Module: Mongo::Model::Crud

Defined in:
lib/mongo/model/crud.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create_object(collection, options) ⇒ Object

Enhancing Mongo::Object CRUD.



4
5
6
7
8
9
# File 'lib/mongo/model/crud.rb', line 4

def create_object collection, options
  with_model_crud_callbacks [:save, :create], options do |mongo_options|
    super collection, mongo_options
    true
  end
end

#delete(options = {}) ⇒ Object



37
38
39
40
41
# File 'lib/mongo/model/crud.rb', line 37

def delete options = {}
  with_collection options do |collection, options|
    collection.delete self, options
  end
end

#delete!(*args) ⇒ Object



43
44
45
# File 'lib/mongo/model/crud.rb', line 43

def delete! *args
  delete(*args) || raise(Mongo::Error, "can't delete invalid model #{self.errors}!")
end

#delete_object(collection, options) ⇒ Object



18
19
20
21
22
23
# File 'lib/mongo/model/crud.rb', line 18

def delete_object collection, options
  with_model_crud_callbacks [:delete], options do |mongo_options|
    super collection, mongo_options
    true
  end
end

#save(options = {}) ⇒ Object

Model CRUD.



27
28
29
30
31
# File 'lib/mongo/model/crud.rb', line 27

def save options = {}
  with_collection options do |collection, options|
    collection.save self, options
  end
end

#save!(*args) ⇒ Object



33
34
35
# File 'lib/mongo/model/crud.rb', line 33

def save! *args
  save(*args) || raise(Mongo::Error, "can't save invalid model (#{self.errors})!")
end

#update(doc, options = {}) ⇒ Object



47
48
49
50
51
# File 'lib/mongo/model/crud.rb', line 47

def update doc, options = {}
  with_collection options do |collection, options|
    collection.update({id: id}, doc, options)
  end
end

#update_object(collection, options) ⇒ Object



11
12
13
14
15
16
# File 'lib/mongo/model/crud.rb', line 11

def update_object collection, options
  with_model_crud_callbacks [:save, :update], options do |mongo_options|
    super collection, mongo_options
    true
  end
end