Greetings all,
I have added 2 required formats to this (helpful) validation plugin,
but unfortunately this complicates (svn) deployment.
Viney, if you’re out there, what is the best means to add date
formats without compromising the local plugin source (ie through
extension).
For others, perhaps this is a more generic problem : how to extend
the following with additional regexs?
module ActiveRecord
module Validations
module DateTime
module Parser
class DateParseError < StandardError #:nodoc:
end
class TimeParseError < StandardError #:nodoc:
end
class DateTimeParseError < StandardError #:nodoc:
end
class << self
def parse_date(value)
raise if value.blank?
return value if value.is_a?(Date)
return value.to_date if value.is_a?(Time)
raise unless value.is_a?(String)
year, month, day = case value.strip
# 22/1/06, 22\1\06 or 22.1.06
when /^(\d{1,2})[\\\/\.-](\d{1,2})[\\\/\.-](\d{2}|\d
{4})$/
ActiveRecord::Validations::DateTime.us_date_format ?
[$3, $1, $2] : [$3, $2, $1]
# 22 Feb 06 or 1 jun 2001
when /^(\d{1,2}) (\w{3,9}) (\d{2}|\d{4})$/
[$3, $2, $1]
# July 1 2005
when /^(\w{3,9}) (\d{1,2}) (\d{2}|\d{4})$/
[$3, $1, $2]
# 2006-01-01
when /^(\d{4})-(\d{2})-(\d{2})$/
[$1, $2, $3]
# 2006-Sep-01
when /^(\d{4})-(\w{3,9})-(\d{2})$/
[$1, $2, $3]
# 2006-9-01
when /^(\d{4})-(\d{1,2})-(\d{2})$/
[$1, $2, $3]
# Not a valid date string
else raise
end
Date.new(unambiguous_year(year), month_index(month),
day.to_i)
rescue
raise DateParseError
end
Thank you for your thoughts.
Jodi