Class: Waw::Validation::DateTimeValidator
- Defined in:
- lib/waw/validation/datetime_validator.rb
Instance Method Summary collapse
- #convert_and_validate(*values) ⇒ Object
- #date_format ⇒ Object
-
#initialize(options = {}) ⇒ DateTimeValidator
constructor
Creates a validator instance with options.
-
#seems_a_date?(value) ⇒ Boolean
Returns a Date object if value seems a date, nil otherwise.
-
#seems_a_time?(value, date) ⇒ Boolean
Returns a Date object if value seems a date, nil otherwise.
- #validate(*values) ⇒ Object
Methods inherited from Validator
Methods included from Helpers
#all_missing?, #any_missing?, #argument_safe, #error, #is_missing?, #missings_to_nil, #no_missing?, #to_validator
Constructor Details
#initialize(options = {}) ⇒ DateTimeValidator
Creates a validator instance with options
8 9 10 |
# File 'lib/waw/validation/datetime_validator.rb', line 8 def initialize( = {}) @options = end |
Instance Method Details
#convert_and_validate(*values) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/waw/validation/datetime_validator.rb', line 38 def convert_and_validate(*values) date = seems_a_date?(values[0]) if date.nil? [false, values] else if time = seems_a_time?(values[1], date) [true, [date, time]] else [false, values] end end end |
#date_format ⇒ Object
12 13 14 |
# File 'lib/waw/validation/datetime_validator.rb', line 12 def date_format @options[:date_format] || '%Y/%m/%d' end |
#seems_a_date?(value) ⇒ Boolean
Returns a Date object if value seems a date, nil otherwise
17 18 19 20 21 |
# File 'lib/waw/validation/datetime_validator.rb', line 17 def seems_a_date?(value) Date.strptime(value.to_s, date_format) rescue ArgumentError => ex nil end |
#seems_a_time?(value, date) ⇒ Boolean
Returns a Date object if value seems a date, nil otherwise
24 25 26 27 28 29 30 31 32 |
# File 'lib/waw/validation/datetime_validator.rb', line 24 def seems_a_time?(value, date) if value.to_s.empty? false else Time.parse(value.to_s, date) end rescue ArgumentError => ex nil end |
#validate(*values) ⇒ Object
34 35 36 |
# File 'lib/waw/validation/datetime_validator.rb', line 34 def validate(*values) convert_and_validate(*values)[0] end |