Class: InputSanitizer::V2::Types::FloatCheck

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

Instance Method Summary collapse

Instance Method Details

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/input_sanitizer/v2/types.rb', line 43

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
    Float(value).tap do |float|
      raise InputSanitizer::TypeMismatchError.new(value, :float) unless float == value
      raise InputSanitizer::ValueError.new(value, options[:minimum], options[:maximum]) if options[:minimum] && float < options[:minimum]
      raise InputSanitizer::ValueError.new(value, options[:minimum], options[:maximum]) if options[:maximum] && float > options[:maximum]
    end
  end
rescue ArgumentError, TypeError
  raise InputSanitizer::TypeMismatchError.new(value, :float)
end