Class: DataValidator::InclusionValidator

Inherits:
BaseValidator show all
Defined in:
lib/validations/inclusion.rb

Constant Summary collapse

ERROR_MESSAGE =
'An object with the method #include? or a proc or lambda is required, ' <<
'and must be supplied as the :in option of the configuration hash'

Instance Attribute Summary

Attributes inherited from BaseValidator

#errors, #name, #options, #value

Instance Method Summary collapse

Methods inherited from BaseValidator

#add_error, #initialize

Constructor Details

This class inherits a constructor from DataValidator::BaseValidator

Instance Method Details

#check_validity!Object



8
9
10
11
12
# File 'lib/validations/inclusion.rb', line 8

def check_validity!
  unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
    raise ArgumentError, ERROR_MESSAGE
  end
end

#validateObject



14
15
16
17
18
19
# File 'lib/validations/inclusion.rb', line 14

def validate
  delimiter = options[:in]
  unless delimiter.send(inclusion_method(delimiter), value)
    add_error :inclusion, value: value
  end
end