Class: Hipaa::Enum::Validators::MultipleValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/hipaa/enum/validators/multiple_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.kindObject



23
24
25
# File 'lib/hipaa/enum/validators/multiple_validator.rb', line 23

def self.kind
  :custom
end

Instance Method Details

#add_error_message(record, attribute) ⇒ Object



15
16
17
# File 'lib/hipaa/enum/validators/multiple_validator.rb', line 15

def add_error_message(record, attribute)
  record.errors[attribute] << (options[:message] || "is not in #{options[:in].join ", "}")
end

#all_included?(values, allowed) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/hipaa/enum/validators/multiple_validator.rb', line 19

def all_included?(values, allowed)
  (values - allowed).empty?
end

#validate_each(record, attribute, values) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/hipaa/enum/validators/multiple_validator.rb', line 5

def validate_each(record, attribute, values)
  values = Array(values)

  if options[:allow_nil]
    add_error_message record, attribute if !all_included?(values, options[:in])
  else
    add_error_message record, attribute if values.empty? || !all_included?(values, options[:in])
  end
end