Module: Validation

Defined in:
lib/assistance/validation.rb

Overview

The Validations module provides validation capabilities as a mixin. When included into a class, it enhances the class with class and instance methods for defining validations and validating class instances.

The Validation emulates the validation capabilities of ActiveRecord, and provides methods for validating acceptance, confirmation, presence, format, length and numericality of attributes.

To use validations, you need to include the Validation module in your class:

class MyClass
  include Validation
  validates_length_of :password, :minimum => 6
end

Defined Under Namespace

Modules: ClassMethods Classes: Errors, Generator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(c) ⇒ Object

Includes the Validation class methods into the including class.



18
19
20
# File 'lib/assistance/validation.rb', line 18

def self.included(c)
  c.extend ClassMethods
end

Instance Method Details

#errorsObject

Returns the validation errors associated with the object.



23
24
25
# File 'lib/assistance/validation.rb', line 23

def errors
  @errors ||= Errors.new
end

#valid?Boolean

Validates the object and returns true if no errors are reported.

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/assistance/validation.rb', line 34

def valid?
  validate
  errors.empty?
end

#validateObject

Validates the object.



28
29
30
31
# File 'lib/assistance/validation.rb', line 28

def validate
  errors.clear
  self.class.validate(self)
end