Class: InputSanitizer::V2::Types::IntegerCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/input_sanitizer/v2/types.rb

Instance Method Summary collapse

Instance Method Details

#call(value, options = {}) ⇒ Object



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

def call(value, options = {})
  if value == nil && (options[:allow_nil] == false || options[:allow_blank] == false || options[:required] == true)
    raise InputSanitizer::BlankValueError
  elsif value == nil
    value
  else
    Integer(value).tap do |integer|
      raise InputSanitizer::TypeMismatchError.new(value, :integer) unless integer == value
      raise InputSanitizer::ValueError.new(value, options[:minimum], options[:maximum]) if options[:minimum] && integer < options[:minimum]
      raise InputSanitizer::ValueError.new(value, options[:minimum], options[:maximum]) if options[:maximum] && integer > options[:maximum]
    end
  end
rescue ArgumentError, TypeError
  raise InputSanitizer::TypeMismatchError.new(value, :integer)
end