Module: Validation

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.


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

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

Instance Method Details

#errorsObject

Returns the validation errors associated with the object.


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

def errors
  @errors ||= Errors.new
end

#valid?Boolean

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

Returns:

  • (Boolean)

39
40
41
42
# File 'lib/rcap/validation.rb', line 39

def valid?
  validate
  errors.empty?
end

#validateObject

Validates the object.


33
34
35
36
# File 'lib/rcap/validation.rb', line 33

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