Class: ArrayValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/louche/validators/array_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ArrayValidator

Returns a new instance of ArrayValidator.



2
3
4
5
6
# File 'lib/louche/validators/array_validator.rb', line 2

def initialize(options)
  options.reverse_merge!(message: :invalid_array)
  options.reverse_merge!(validity_method: :valid?)
  super
end

Instance Method Details

#add_error(record, attribute, value) ⇒ Object



17
18
19
# File 'lib/louche/validators/array_validator.rb', line 17

def add_error(record, attribute, value)
  record.errors.add(attribute, options.fetch(:message), value: value)
end

#validate_each(record, attribute, value) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/louche/validators/array_validator.rb', line 8

def validate_each(record, attribute, value)
  if value.is_a?(Array)
    validity_method = options.fetch(:validity_method)
    add_error(record, attribute, value) if value.empty? || !value.map.all?(&validity_method)
  else
    add_error(record, attribute, value)
  end
end