Module: Validation::ClassMethods

Module | Validation::ClassMethods
In: | lib/assistance/validation.rb

Validation class methods.

### 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

### Constants

NUMBER_RE | = | /^\d*\.{0,1}\d+$/
INTEGER_RE | = | /\A[+-]?\d+\Z/

### Public Instance methods

has_validations?()

Returns true if validations are defined.

[Source]

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

skip_superclass_validations()

[Source]

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

validate(o)

Validates the given instance.

[Source]

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

validates(&block)

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

[Source]

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

validates_acceptance_of(*atts)

Validates acceptance of an attribute.

[Source]

         # File lib/assistance/validation.rb, line 151
    151:     def validates_acceptance_of(*atts)
    152:       opts = {
    153:         :message => 'is not accepted',
    154:         :allow_nil => true,
    155:         :accept => '1'
    156:       }.merge!(atts.extract_options!)
    157:
    158:       validates_each(*atts) do |o, a, v|
    159:         next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
    160:         o.errors[a] << opts[:message] unless v == opts[:accept]
    161:       end
    162:     end

validates_confirmation_of(*atts)

Validates confirmation of an attribute.

[Source]

         # File lib/assistance/validation.rb, line 165
    165:     def validates_confirmation_of(*atts)
    166:       opts = {
    167:         :message => 'is not confirmed',
    168:       }.merge!(atts.extract_options!)
    169:
    170:       validates_each(*atts) do |o, a, v|
    171:         next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
    172:         c = o.send("#{a}_confirmation""#{a}_confirmation")
    173:         o.errors[a] << opts[:message] unless v == c
    174:       end
    175:     end

validates_each(*atts, &block)

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

[Source]

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

validates_format_of(*atts)

Validates the format of an attribute.

[Source]

         # File lib/assistance/validation.rb, line 178
    178:     def validates_format_of(*atts)
    179:       opts = {
    180:         :message => 'is invalid',
    181:       }.merge!(atts.extract_options!)
    182:
    183:       unless opts[:with].is_a?(Regexp)
    184:         raise ArgumentError, "A regular expression must be supplied as the :with option of the options hash"
    185:       end
    186:
    187:       validates_each(*atts) do |o, a, v|
    188:         next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
    189:         o.errors[a] << opts[:message] unless v.to_s =~ opts[:with]
    190:       end
    191:     end

validates_length_of(*atts)

Validates the length of an attribute.

[Source]

         # File lib/assistance/validation.rb, line 194
    194:     def validates_length_of(*atts)
    195:       opts = {
    196:         :too_long     => 'is too long',
    197:         :too_short    => 'is too short',
    198:         :wrong_length => 'is the wrong length'
    199:       }.merge!(atts.extract_options!)
    200:
    201:       validates_each(*atts) do |o, a, v|
    202:         next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
    203:         if m = opts[:maximum]
    204:           o.errors[a] << (opts[:message] || opts[:too_long]) unless v && v.size <= m
    205:         end
    206:         if m = opts[:minimum]
    207:           o.errors[a] << (opts[:message] || opts[:too_short]) unless v && v.size >= m
    208:         end
    209:         if i = opts[:is]
    210:           o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && v.size == i
    211:         end
    212:         if w = opts[:within]
    213:           o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && w.include?(v.size)
    214:         end
    215:       end
    216:     end

validates_numericality_of(*atts)

Validates whether an attribute is a number.

[Source]

         # File lib/assistance/validation.rb, line 222
    222:     def validates_numericality_of(*atts)
    223:       opts = {
    224:         :message => 'is not a number',
    225:       }.merge!(atts.extract_options!)
    226:
    227:       re = opts[:only_integer] ? INTEGER_RE : NUMBER_RE
    228:
    229:       validates_each(*atts) do |o, a, v|
    230:         next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank])
    231:         o.errors[a] << opts[:message] unless v.to_s =~ re
    232:       end
    233:     end

validates_presence_of(*atts)

Validates the presence of an attribute.

[Source]

         # File lib/assistance/validation.rb, line 236
    236:     def validates_presence_of(*atts)
    237:       opts = {
    238:         :message => 'is not present',
    239:       }.merge!(atts.extract_options!)
    240:
    241:       validates_each(*atts) do |o, a, v|
    242:         o.errors[a] << opts[:message] unless v && !v.blank?
    243:       end
    244:     end

validations()

Returns the validations hash for the class.

[Source]

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

[Validate]