Module: Cequel::Record::Validations

Extended by:
ActiveSupport::Concern
Defined in:
lib/cequel/record/validations.rb

Overview

Cequel::Record classes can define validations that are run before saving a record instance.

Examples:

Validations

class User
  include Cequel::Record

  key :login, :text
  column :email, :text

  validates :email, presence: true, format: RFC822::EMAIL
end

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#save(options = {}) ⇒ Object

Since:

  • 0.1.0



50
51
52
53
54
# File 'lib/cequel/record/validations.rb', line 50

def save(options = {})
  validate = options.fetch(:validate, true)
  options.delete(:validate)
  (!validate || valid?) && super
end

#save!(attributes = {}) ⇒ Record

Attempt to save the record, or raise an exception if there is a validation error

Parameters:

  • options (Options)

    options for save

Returns:

Raises:

Since:

  • 0.1.0



64
65
66
67
68
69
70
# File 'lib/cequel/record/validations.rb', line 64

def save!(attributes = {})
  tap do
    unless save(attributes)
      fail RecordInvalid, errors.full_messages.join("; ")
    end
  end
end

#update_attributes!(attributes) ⇒ Record

Set the given attributes and attempt to save, raising an exception if there is a validation error

Parameters:

  • attributes (Hash)

    hash of attributes to update

Returns:

Raises:

Since:

  • 0.1.0



79
80
81
82
# File 'lib/cequel/record/validations.rb', line 79

def update_attributes!(attributes)
  self.attributes = attributes
  save!
end