Class: ActiveModel::Validations::ArrayValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::ArrayValidator
- Defined in:
- lib/can_has_validations/validators/array_validator.rb
Defined Under Namespace
Modules: DefaultKeys
Instance Attribute Summary collapse
-
#validators ⇒ Object
readonly
Returns the value of attribute validators.
Instance Method Summary collapse
-
#initialize(options) ⇒ ArrayValidator
constructor
A new instance of ArrayValidator.
- #validate_each(record, attribute, array_values) ⇒ Object
Constructor Details
#initialize(options) ⇒ ArrayValidator
Returns a new instance of ArrayValidator.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/can_has_validations/validators/array_validator.rb', line 28 def initialize() record_class = [:class] super record_class.prepend DefaultKeys defaults = .dup validations = defaults.slice!(*record_class.send(:_validates_default_keys), :attributes) raise ArgumentError, "You need to supply at least one array validation" if validations.empty? defaults[:attributes] = attributes @validators = validations.map do |key, | next unless key = "#{key.to_s.camelize}Validator" begin klass = key.include?("::".freeze) ? key.constantize : record_class.const_get(key) rescue NameError raise ArgumentError, "Unknown validator: '#{key}'" end validator = klass.new(defaults.merge(())) end end |
Instance Attribute Details
#validators ⇒ Object (readonly)
Returns the value of attribute validators.
26 27 28 |
# File 'lib/can_has_validations/validators/array_validator.rb', line 26 def validators @validators end |
Instance Method Details
#validate_each(record, attribute, array_values) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/can_has_validations/validators/array_validator.rb', line 54 def validate_each(record, attribute, array_values) @validators.each do |validator| error_count = count_errors(record) Array(array_values).each do |value| validator.validate_each(record, attribute, value) # to avoid repeating error messages, stop after a single error unless validator.[:multiple_errors] break if error_count != count_errors(record) end end end end |