Module: Humanoid::Commands::ClassMethods

Defined in:
lib/humanoid/commands.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = {}) ⇒ Object

Create a new Document. This will instantiate a new document and save it in a single call. Will always return the document whether save passed or not.

Example:

Person.create(:title => "Mr")

Returns: the Document.



126
127
128
129
130
131
132
133
134
# File 'lib/humanoid/commands.rb', line 126

def create(attributes = {})
  document = new(attributes)
  begin
    Create.execute(document)
  rescue Mongo::OperationFailure => e
    document.errors.add(:humanoid, e.message)
  end
  document
end

#create!(attributes = {}) ⇒ Object

Create a new Document. This will instantiate a new document and save it in a single call. Will always return the document whether save passed or not. Will raise an error if validation fails.

Example:

Person.create!(:title => "Mr")

Returns: the Document.



145
146
147
148
149
# File 'lib/humanoid/commands.rb', line 145

def create!(attributes = {})
  document = Create.execute(new(attributes), true)
  raise Errors::Validations.new(document.errors) unless document.errors.empty?
  document
end

#delete_all(conditions = {}) ⇒ Object

Delete all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Does not fire any callbacks.

Example:

Person.delete_all(:conditions => { :title => "Sir" }) Person.delete_all

Returns: true or raises an error.



161
162
163
# File 'lib/humanoid/commands.rb', line 161

def delete_all(conditions = {})
  DeleteAll.execute(self, conditions)
end

#destroy_all(conditions = {}) ⇒ Object

Delete all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Fires the destroy callbacks if conditions were passed.

Example:

Person.destroy_all(:conditions => { :title => "Sir" }) Person.destroy_all

Returns: true or raises an error.



175
176
177
# File 'lib/humanoid/commands.rb', line 175

def destroy_all(conditions = {})
  DestroyAll.execute(self, conditions)
end