Module: Mongo::Model::Crud::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#build(attributes = {}, options = {}, &block) ⇒ Object



70
71
72
73
74
75
# File 'lib/mongo/model/crud.rb', line 70

def build attributes = {}, options = {}, &block
  model = self.new
  model.set attributes, options
  block.call model if block
  model
end

#create(attributes = {}, options = {}, &block) ⇒ Object



77
78
79
80
81
# File 'lib/mongo/model/crud.rb', line 77

def create attributes = {}, options = {}, &block
  model = build attributes, options, &block
  model.save
  model
end

#create!(attributes = {}, options = {}, &block) ⇒ Object



83
84
85
86
87
# File 'lib/mongo/model/crud.rb', line 83

def create! attributes = {}, options = {}, &block
  model = build attributes, options, &block
  model.save || raise(Mongo::Error, "can't create #{self} #{model.errors.inspect}!")
  model
end

#delete_all(selector = {}, options = {}) ⇒ Object



89
90
91
92
93
# File 'lib/mongo/model/crud.rb', line 89

def delete_all selector = {}, options = {}
  success = true
  each(selector){|o| success = false unless o.delete options}
  success
end

#delete_all!(selector = {}, options = {}) ⇒ Object



95
96
97
# File 'lib/mongo/model/crud.rb', line 95

def delete_all! selector = {}, options = {}
  delete_all(selector, options) || raise(Mongo::Error, "can't delete #{selector.inspect}!")
end

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



99
100
101
# File 'lib/mongo/model/crud.rb', line 99

def update selector, doc, options = {}
  collection.update selector, doc, options
end