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

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

Instance Method Summary collapse

Instance Method Details

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



23
24
25
# File 'lib/mongo/model/crud.rb', line 23

def build attributes = {}, options = {}
  self.new.set attributes, options
end

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



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

def create attributes = {}, options = {}
  o = build attributes, options
  o.save
  o
end

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

Raises:

  • (Mongo::Error)


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

def create! attributes = {}, options = {}
  o = create attributes
  raise(Mongo::Error, "can't create #{attributes.inspect}!") if o.new_record?
  o
end

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



39
40
41
42
43
44
# File 'lib/mongo/model/crud.rb', line 39

def destroy_all selector = {}, options = {}
  success = true
  collection = options[:collection] || self.collection
  each(selector){|o| success = false unless o.destroy}
  success
end

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



46
47
48
# File 'lib/mongo/model/crud.rb', line 46

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