Class: Errapi::Validations::Inclusion

Inherits:
Base
  • Object
show all
Includes:
Clusivity
Defined in:
lib/errapi/validations/inclusion.rb

Defined Under Namespace

Classes: Factory

Instance Method Summary collapse

Methods inherited from Base

#actual_option_value, #callable_option_type_error, #callable_option_value?, #callable_option_value_error, #exactly_one_option?

Constructor Details

#initialize(options = {}) ⇒ Inclusion

Returns a new instance of Inclusion.



11
12
13
14
15
16
17
18
# File 'lib/errapi/validations/inclusion.rb', line 11

def initialize options = {}
  unless key = exactly_one_option?(OPTIONS, options)
    raise ArgumentError, "Either :in or :within must be supplied (but not both)."
  end

  @delimiter = options[key]
  check_delimiter! OPTIONS_DESCRIPTION
end

Instance Method Details

#validate(value, context, options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/errapi/validations/inclusion.rb', line 20

def validate value, context, options = {}
  allowed_values = members OPTIONS_DESCRIPTION, options
  unless include? allowed_values, value
    context.add_error reason: :not_included, check_value: allowed_values
  end
end