Class: InputSanitizer::V2::Types::DatetimeCheck

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DatetimeCheck

Returns a new instance of DatetimeCheck.



140
141
142
143
# File 'lib/input_sanitizer/v2/types.rb', line 140

def initialize(options = {})
  @check_date = options && options[:check_date]
  @klass = @check_date ? Date : DateTime
end

Instance Method Details

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/input_sanitizer/v2/types.rb', line 145

def call(value, options = {})
  raise InputSanitizer::TypeMismatchError.new(value, @check_date ? :date : :datetime) unless value == nil || value.is_a?(String)

  if value.blank? && (options[:allow_blank] == false || options[:required] == true)
    raise InputSanitizer::BlankValueError
  elsif value == nil && options[:allow_nil] == false
    raise InputSanitizer::BlankValueError
  elsif value.blank?
    value
  else
    @klass.parse(value).tap do |datetime|
      raise InputSanitizer::ValueError.new(value, options[:minimum], options[:maximum]) if options[:minimum] && datetime < options[:minimum]
      raise InputSanitizer::ValueError.new(value, options[:minimum], options[:maximum]) if options[:maximum] && datetime > options[:maximum]
    end
  end
rescue ArgumentError, TypeError
  raise InputSanitizer::TypeMismatchError.new(value, @check_date ? :date : :datetime)
end