Module: Humanoid::Commands::InstanceMethods

Defined in:
lib/humanoid/commands.rb

Instance Method Summary collapse

Instance Method Details

#deleteObject

Delete the Document from the database. This method is an optimized delete that does not force any callbacks.

Example:

document.delete

Returns: true unless an error occurs.



32
33
34
# File 'lib/humanoid/commands.rb', line 32

def delete
  Delete.execute(self)
end

#destroyObject

Destroy the Document. This will delete the document from the database and run the before and after destroy callbacks.

Example:

document.destroy

Returns: true unless an error occurs.



44
45
46
# File 'lib/humanoid/commands.rb', line 44

def destroy
  Destroy.execute(self)
end

#save(validate = true) ⇒ Object

Save the Document. If the document is new, then the before and after create callbacks will get executed as well as the save callbacks. Otherwise only the save callbacks will run.

Options:

validate: Run validations or not. Defaults to true.

Example:

document.save # save with validations document.save(false) # save without validations

Returns: true if validation passes, false if not.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/humanoid/commands.rb', line 62

def save(validate = true)
  new = new_record?
  run_callbacks(:before_create) if new
  begin
    saved = Save.execute(self, validate)
  rescue Mongo::OperationFailure => e
    errors.add(:humanoid, e.message)
  end
  run_callbacks(:after_create) if new && saved
  saved
end

#save!Object

Save the Document, dangerously. Before and after save callbacks will get run. If validation fails an error will get raised.

Example:

document.save!

Returns: true if validation passes



82
83
84
# File 'lib/humanoid/commands.rb', line 82

def save!
  return save(true) || (raise Errors::Validations.new(self.errors))
end

#update_attributes(attrs = {}) ⇒ Object

Update the document attributes and persist the document to the database. Will delegate to save with all callbacks.

Example:

document.update_attributes(:title => "Test")



92
93
94
# File 'lib/humanoid/commands.rb', line 92

def update_attributes(attrs = {})
  set_attributes(attrs); save
end

#update_attributes!(attrs = {}) ⇒ Object

Update the document attributes and persist the document to the database. Will delegate to save!

Example:

document.update_attributes!(:title => "Test")



102
103
104
# File 'lib/humanoid/commands.rb', line 102

def update_attributes!(attrs = {})
  set_attributes(attrs); save!
end