Module: Mixture::Validate

Defined in:
lib/mixture/validate.rb,
lib/mixture/validate/base.rb,
lib/mixture/validate/match.rb,
lib/mixture/validate/length.rb,
lib/mixture/validate/presence.rb,
lib/mixture/validate/inclusion.rb,
lib/mixture/validate/exclusion.rb

Overview

Handles validation of attributes. You can register validators with this module, and they will be used to validate attributes. Check out Base for the expected interface.

Defined Under Namespace

Classes: Base, Exclusion, Inclusion, Length, Match, Presence

Class Method Summary collapse

Class Method Details

.register(name, validator) ⇒ void

This method returns an undefined value.

Registers a validator. This lets Mixture know about the validators that can occur.

Parameters:


16
17
18
# File 'lib/mixture/validate.rb', line 16

def self.register(name, validator)
  validations[name] = validator
end

.validate(record, attribute, value) ⇒ void

This method returns an undefined value.

Performs a validation on the attribute. It loops through the validation requirements on the attribute, and runs each validation with validate_with.

Parameters:


44
45
46
47
48
49
50
# File 'lib/mixture/validate.rb', line 44

def self.validate(record, attribute, value)
  return unless attribute.options[:validate]
  attribute.options[:validate].each do |k, v|
    validator = validations.fetch(k).new(v)
    validate_with(validator, record, attribute, value)
  end
end

.validate_with(validator, record, attribute, value) ⇒ void

This method returns an undefined value.

Validates a (record, attribute, value) triplet with the given validator. It calls #validate on the validator with the three as arguments, and rescues any validation errors thrown (and places them in record.errors).

Parameters:


63
64
65
66
67
68
# File 'lib/mixture/validate.rb', line 63

def self.validate_with(validator, record, attribute, value)
  validator.validate(record, attribute, value)

rescue ValidationError => e
  record.errors[attribute] << e
end

.validationsHash{Symbol => Validate::Base}

The validators that are currently registered. This returns a key-value hash of validations.

Returns:


24
25
26
# File 'lib/mixture/validate.rb', line 24

def self.validations
  @_validations ||= {}
end