Class: ArrayFormatValidator

Inherits:
ArrayValidatorBase show all
Defined in:
lib/active_model_validators_ex/array_format_validator.rb

Instance Method Summary collapse

Methods inherited from ArrayValidatorBase

#validate_each

Constructor Details

#initialize(options) ⇒ ArrayFormatValidator

Returns a new instance of ArrayFormatValidator.



4
5
6
7
8
9
10
# File 'lib/active_model_validators_ex/array_format_validator.rb', line 4

def initialize(options)
  unless options.key?(:with) and options[:with].is_a?(Regexp)
    raise 'options must contain a key :with, where the value is a Regexp'
  end

  super(options)
end

Instance Method Details

#custom_validations(record, attribute, value) ⇒ Object



12
13
14
15
16
# File 'lib/active_model_validators_ex/array_format_validator.rb', line 12

def custom_validations(record, attribute, value)
  unless value.all? { |val| !val.match(options[:with]).nil? }
    record.errors.add(attribute, :array_format, options)
  end
end