Class: InputSanitizer::V2::Types::DatetimeCheck
- Inherits:
-
Object
- Object
- InputSanitizer::V2::Types::DatetimeCheck
- Defined in:
- lib/input_sanitizer/v2/types.rb
Instance Method Summary collapse
- #call(value, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ DatetimeCheck
constructor
A new instance of DatetimeCheck.
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( = {}) @check_date = && [: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, = {}) raise InputSanitizer::TypeMismatchError.new(value, @check_date ? :date : :datetime) unless value == nil || value.is_a?(String) if value.blank? && ([:allow_blank] == false || [:required] == true) raise InputSanitizer::BlankValueError elsif value == nil && [:allow_nil] == false raise InputSanitizer::BlankValueError elsif value.blank? value else @klass.parse(value).tap do |datetime| raise InputSanitizer::ValueError.new(value, [:minimum], [:maximum]) if [:minimum] && datetime < [:minimum] raise InputSanitizer::ValueError.new(value, [:minimum], [:maximum]) if [:maximum] && datetime > [:maximum] end end rescue ArgumentError, TypeError raise InputSanitizer::TypeMismatchError.new(value, @check_date ? :date : :datetime) end |