Class: ActiveEntity::Validations::SubsetValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_entity/validations/subset.rb

Overview

:nodoc:

Constant Summary collapse

ERROR_MESSAGE =
"An object with the method #include? or a proc, lambda or symbol is required, " \
"and must be supplied as the :in (or :within) option of the configuration hash"

Instance Method Summary collapse

Instance Method Details

#check_validity!Object



9
10
11
12
13
# File 'lib/active_entity/validations/subset.rb', line 9

def check_validity!
  unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym)
    raise ArgumentError, ERROR_MESSAGE
  end
end

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/active_entity/validations/subset.rb', line 15

def validate_each(record, attribute, value)
  if value && !value.respond_to?(:to_a)
    raise ArgumentError, "#{record} can't respond `to_a`."
  end

  unless subset?(record, value)
    record.errors.add(attribute, :non_subset, **options.except(:in, :within).merge(value: value))
  end
end