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
-
.included(c) ⇒ Object
Includes the Validation class methods into the including class.
Instance Method Summary collapse
-
#errors ⇒ Object
Returns the validation errors associated with the object.
-
#valid? ⇒ Boolean
Validates the object and returns true if no errors are reported.
-
#validate ⇒ Object
Validates the object.
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
#errors ⇒ Object
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.
34 35 36 37 |
# File 'lib/assistance/validation.rb', line 34 def valid? validate errors.empty? end |
#validate ⇒ Object
Validates the object.
28 29 30 31 |
# File 'lib/assistance/validation.rb', line 28 def validate errors.clear self.class.validate(self) end |