Module: Validus

Defined in:
lib/validus.rb,
lib/validus/errors.rb,
lib/validus/version.rb

Overview

Add validation support to Plain Old Ruby Objects. Include this module in a target class and override the validate method. Use #valid? to perform the validation.

Example:

require "validus"

class Person
  include Validus

  attr_accessor :name, :age

  def initialize
    @name = ''
    @age  = 0
  end

  def validate
    errors.add(:name, 'cannot be blank') if name == ''
    errors.add(:age, 'must be greater than 0') if age <= 0
  end
end

person = Person.new
person.valid?                    # => false
person.errors.for(:name).to_a    # => ['cannot be blank']
person.errors.full_messages.to_a # => ['name cannot be blank', 'age must be greater than 0']

person.name = 'John'
person.age  = 20

person.valid?        # => true
person.errors.empty? # => true

Defined Under Namespace

Classes: Errors

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#errorsObject

An instance of an Errors object.



59
60
61
# File 'lib/validus.rb', line 59

def errors
  @errors ||= Errors.new
end

#valid?Boolean

Calls validate and returns true if there are any errors on the target’s attributes.

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/validus.rb', line 52

def valid?
  errors.clear
  validate
  errors.empty?
end

#validateObject

Abstract method that performs validation. Override this method in the target class. Overriding methods should use errors#add to add validation errors. For example:

def validate
  errors.add(:name, 'cannot be blank') if name.blank?
end

This method is not meant to be called directly… use #valid? instead.



48
49
# File 'lib/validus.rb', line 48

def validate
end