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/exclusion.rb,
lib/mixture/validate/inclusion.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:



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

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:



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

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:



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

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:



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

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