Module: Reaction::HasValidators::ClassMethods

Defined in:
lib/reaction/has_validators.rb

Instance Method Summary collapse

Instance Method Details

#add_validator(name, validator, options = {}) ⇒ Object



17
18
19
20
# File 'lib/reaction/has_validators.rb', line 17

def add_validator(name, validator, options = {})
  klass = class_for_validator(validator)
  validators(name) << klass.new(options)
end

#all_validatorsObject



8
9
10
# File 'lib/reaction/has_validators.rb', line 8

def all_validators
  @all_validators ||= {}
end

#class_for_validator(validator) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/reaction/has_validators.rb', line 29

def class_for_validator(validator)
  return validator if validator.is_a?(Class)
  name = validator.to_s.split('_').map(&:capitalize).join
  const_get("#{name}Validator")
rescue NameError
  raise ArgumentError.new("Unknown param validator: #{validator}")
end

#cleanup_validatorsObject



37
38
39
40
41
42
43
# File 'lib/reaction/has_validators.rb', line 37

def cleanup_validators
  all_validators.each do |name, validators|
    validators.each do |validator|
      validator.cleanup
    end
  end
end

#set_validators(name, new_validators = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/reaction/has_validators.rb', line 22

def set_validators(name, new_validators = {})
  validators(name).clear
  new_validators.each do |validator, options|
    add_validator(name, validator, options)
  end
end

#validators(name) ⇒ Object



12
13
14
15
# File 'lib/reaction/has_validators.rb', line 12

def validators(name)
  all_validators[name.to_sym] ||= []
  all_validators[name.to_sym]
end