Module: CMDx::Validators::Exclusion

Extended by:
Exclusion
Included in:
Exclusion
Defined in:
lib/cmdx/validators/exclusion.rb

Overview

Inverse of Inclusion: the value must not be within the given enumerable or ‘Range`.

Instance Method Summary collapse

Instance Method Details

#call(value, options = EMPTY_HASH) ⇒ Validators::Failure?

Parameters:

  • value (Object)
  • options (Hash{Symbol => Object}) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :in (Range, Array, Set, Enumerable)

    disallowed values

  • :within (Range, Array, Set, Enumerable)

    alias for ‘:in`

  • :message (String)

    global failure-message override

  • :of_message (String)

    override for enumerable failures

  • :in_message, (String)

    :within_message overrides for range failures

Returns:

Raises:

  • (ArgumentError)

    when neither ‘:in` nor `:within` is given



20
21
22
23
24
25
26
27
28
29
# File 'lib/cmdx/validators/exclusion.rb', line 20

def call(value, options = EMPTY_HASH)
  values = options[:in] || options[:within]
  raise ArgumentError, "exclusion validator requires :in or :within option" if values.nil?

  if values.is_a?(Range)
    within_failure(values.begin, values.end, options) if values.cover?(value)
  elsif Array(values).any? { |v| v === value }
    of_failure(values, options)
  end
end