Class: SingletonValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/solid/validators/singleton_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(model, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/solid/validators/singleton_validator.rb', line 6

def validate_each(model, attribute, value)
  with_option = Array.wrap(options[:with] || options[:in])

  return model.errors.add(attribute, options[:message] || "is not a class or module") unless value.is_a?(Module)

  return if with_option.any? do |type|
    raise ArgumentError, "#{type.inspect} is not a class or module" unless type.is_a?(Module)

    value == type || (value < type || value.is_a?(type))
  end

  message = "is not #{with_option.map(&:name).join(" | ")}"

  Solid::Validators.add_error(model, attribute, message, options)
end