Module: Validation::ClassMethods

### Home Classes Methods

### In Files

- lib/assistance/validation.rb

### Methods

- #has_validations?
- #skip_superclass_validations
- #validate
- #validates
- #validates_acceptance_of
- #validates_confirmation_of
- #validates_each
- #validates_format_of
- #validates_length_of
- #validates_numericality_of
- #validates_presence_of
- #validations

### Files

- CHANGELOG
- COPYING
- README

### Class/Module Index

Quicksearch

- Validation
- Validation::ClassMethods
- Validation::Errors
- Validation::Generator
- Assistance
- Assistance::TimeCalculations
- Inflector
- Inflector::Inflections
- Array
- ConnectionPool
- FalseClass
- NilClass
- Numeric
- Object
- SingleThreadedPool
- String
- TrueClass

No matching classes.

# Validation::ClassMethods

Validation class methods.

### Constants

INTEGER_RE
NUMBER_RE

### Public Instance Methods

has_validations?() click to toggle source

Returns true if validations are defined.

    # File lib/assistance/validation.rb, line 120
    def has_validations?
      !validations.empty?
    end

skip_superclass_validations() click to toggle source

    # File lib/assistance/validation.rb, line 135
    def skip_superclass_validations
      @skip_superclass_validations = true
    end

validate(o) click to toggle source

Validates the given instance.

    # File lib/assistance/validation.rb, line 125
    def validate(o)
      if superclass.respond_to?(:validate) && !@skip_superclass_validations
        superclass.validate(o)
      end
      validations.each do |att, procs|
        v = o.send(att)
        procs.each {|p| p[o, att, v]}
      end
    end

validates(&block) click to toggle source

Defines validations by converting a longhand block into a series of shorthand definitions. For example:

    class MyClass
      include Validation
      validates do
        length_of :name, :minimum => 6
        length_of :password, :minimum => 8
      end
    end

is equivalent to:

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

    # File lib/assistance/validation.rb, line 110
    def validates(&block)
      Generator.new(self, &block)
    end

validates_acceptance_of(*atts) click to toggle source

Validates acceptance of an attribute.

    # File lib/assistance/validation.rb, line 151
    def validates_acceptance_of(*atts)
      opts = {
        :message => 'is not accepted',
        :allow_nil => true,
        :accept => '1'
      }.merge!(atts.extract_options!)

      validates_each(*atts) do |o, a, v|
        next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
        o.errors[a] << opts[:message] unless v == opts[:accept]
      end
    end

validates_confirmation_of(*atts) click to toggle source

Validates confirmation of an attribute.

    # File lib/assistance/validation.rb, line 165
    def validates_confirmation_of(*atts)
      opts = {
        :message => 'is not confirmed',
      }.merge!(atts.extract_options!)

      validates_each(*atts) do |o, a, v|
        next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
        c = o.send(:"#{a}_confirmation")
        o.errors[a] << opts[:message] unless v == c
      end
    end

validates_each(*atts, &block) click to toggle source

Adds a validation for each of the given attributes using the supplied block. The block must accept three arguments: instance, attribute and value, e.g.:

    validates_each :name, :password do |object, attribute, value|
      object.errors[attribute] << 'is not nice' unless value.nice?
    end

    # File lib/assistance/validation.rb, line 146
    def validates_each(*atts, &block)
      atts.each {|a| validations[a] << block}
    end

validates_format_of(*atts) click to toggle source

Validates the format of an attribute.

    # File lib/assistance/validation.rb, line 178
    def validates_format_of(*atts)
      opts = {
        :message => 'is invalid',
      }.merge!(atts.extract_options!)

      unless opts[:with].is_a?(Regexp)
        raise ArgumentError, "A regular expression must be supplied as the :with option of the options hash"
      end

      validates_each(*atts) do |o, a, v|
        next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
        o.errors[a] << opts[:message] unless v.to_s =~ opts[:with]
      end
    end

validates_length_of(*atts) click to toggle source

Validates the length of an attribute.

    # File lib/assistance/validation.rb, line 194
    def validates_length_of(*atts)
      opts = {
        :too_long     => 'is too long',
        :too_short    => 'is too short',
        :wrong_length => 'is the wrong length'
      }.merge!(atts.extract_options!)

      validates_each(*atts) do |o, a, v|
        next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
        if m = opts[:maximum]
          o.errors[a] << (opts[:message] || opts[:too_long]) unless v && v.size <= m
        end
        if m = opts[:minimum]
          o.errors[a] << (opts[:message] || opts[:too_short]) unless v && v.size >= m
        end
        if i = opts[:is]
          o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && v.size == i
        end
        if w = opts[:within]
          o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && w.include?(v.size)
        end
      end
    end

validates_numericality_of(*atts) click to toggle source

Validates whether an attribute is a number.

    # File lib/assistance/validation.rb, line 222
    def validates_numericality_of(*atts)
      opts = {
        :message => 'is not a number',
      }.merge!(atts.extract_options!)

      re = opts[:only_integer] ? INTEGER_RE : NUMBER_RE

      validates_each(*atts) do |o, a, v|
        next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
        o.errors[a] << opts[:message] unless v.to_s =~ re
      end
    end

validates_presence_of(*atts) click to toggle source

Validates the presence of an attribute.

    # File lib/assistance/validation.rb, line 236
    def validates_presence_of(*atts)
      opts = {
        :message => 'is not present',
      }.merge!(atts.extract_options!)

      validates_each(*atts) do |o, a, v|
        o.errors[a] << opts[:message] unless v && !v.blank?
      end
    end

validations() click to toggle source

Returns the validations hash for the class.

    # File lib/assistance/validation.rb, line 115
    def validations
      @validations ||= Hash.new {|h, k| h[k] = []}
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.