Class: Waw::Validation::DateValidator

Inherits:
Validator show all
Defined in:
lib/waw/validation/date_validator.rb

Instance Method Summary collapse

Methods inherited from Validator

#&, #===, #=~, #initialize, #not, #|

Methods included from Helpers

#all_missing?, #any_missing?, #argument_safe, #error, #is_missing?, #missings_to_nil, #no_missing?, #to_validator

Constructor Details

This class inherits a constructor from Waw::Validation::Validator

Instance Method Details

#convert_and_validate(*values) ⇒ Object



24
25
26
27
# File 'lib/waw/validation/date_validator.rb', line 24

def convert_and_validate(*values)
  dates = values.collect{|v| seems_a_date?(v)}
  dates.include?(nil) ? [false, values] : [true, dates]
end

#seems_a_date?(value) ⇒ Boolean

Returns a Date object if value seems a date, nil otherwise

Returns:



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/waw/validation/date_validator.rb', line 8

def seems_a_date?(value)
  if /^\d{4}\/\d{2}\/\d{2}$/ =~ value.to_s
    Date.strptime(value.to_s, "%Y/%m/%d")
  elsif /^\d{2}\/\d{2}\/\d{4}$/ =~ value.to_s
    Date.strptime(value.to_s, "%d/%m/%Y")
  else
    nil
  end
rescue ArgumentError => ex
  nil
end

#validate(*values) ⇒ Object



20
21
22
# File 'lib/waw/validation/date_validator.rb', line 20

def validate(*values)
  values.all?{|val| seems_a_date?(val)}
end