module MongoModel::DocumentExtensions::Validations

Public Instance Methods

save(options={}) click to toggle source

The validation process on save can be skipped by passing :validate => false. The regular Document#save method is replaced with this when the validations module is mixed in, which it is by default.

Calls superclass method
# File lib/mongomodel/document/validations.rb, line 30
def save(options={})
  if perform_validation(options)
    begin
      super
    rescue DocumentNotSaved
      valid?
      false
    end
  else
    false
  end
end
save!(options={}) click to toggle source

Attempts to save the document just like Document#save but will raise a DocumentInvalid exception instead of returning false if the document is not valid.

Calls superclass method
# File lib/mongomodel/document/validations.rb, line 45
def save!(options={})
  if perform_validation(options)
    begin
      super
    rescue DocumentNotSaved => e
      valid? ? raise : raise(DocumentInvalid.new(self))
    end
  else
    raise DocumentInvalid.new(self)
  end
end

Protected Instance Methods

perform_validation(options={}) click to toggle source
# File lib/mongomodel/document/validations.rb, line 58
def perform_validation(options={})
  perform_validation = options != false && options[:validate] != false
  perform_validation ? valid?(options[:context]) : true
end