module Subvalid::Validator::DSL

Constants

MODIFIERS
ValidatorEntry

Public Instance Methods

validates(*attributes, **validators, &block) click to toggle source
# File lib/subvalid/validator.rb, line 14
def validates(*attributes, **validators, &block)
  if validators.empty? && !block
    raise "no validations or block specified"
  end

  attributes = [:base] if attributes.empty?

  add_validations(attributes, validators, block)
end

Private Instance Methods

add_validations(attributes, validators, block) click to toggle source
# File lib/subvalid/validator.rb, line 30
def add_validations(attributes, validators, block)
  modifiers, validators = extract_modifiers(validators)
  attributes.each do |attribute|
    validators.each do |validator_key, args|
      validator = ValidatorRegistry[validator_key]
      validations[attribute] << ValidatorEntry.new(validator, modifiers, args)
    end
    validations[attribute] << ValidatorEntry.new(BlockValidator, modifiers, block) if block
  end
end
extract_modifiers(validators) click to toggle source
# File lib/subvalid/validator.rb, line 41
def extract_modifiers(validators)
  modifiers = MODIFIERS.inject([]){|acc, mod_key|
    modifier_proc = validators.delete(mod_key)
    acc <<  modifier_proc if modifier_proc
    acc
  }
  [modifiers, validators]
end
validations() click to toggle source
# File lib/subvalid/validator.rb, line 26
def validations
  @validations ||= Hash.new{|vals,attribute| vals[attribute] = [] }
end