push dummy date value assignment into Formats.parse and allow custom values to be used

This commit is contained in:
Adam Meehan
2009-09-12 13:07:01 +10:00
parent d3c5101f92
commit 6db8b7d908
4 changed files with 54 additions and 18 deletions

View File

@@ -23,6 +23,7 @@ module ValidatesTimeliness
# Set the threshold value for a two digit year to be considered last century
#
# Default: 30
#
# Example:
@@ -32,6 +33,14 @@ module ValidatesTimeliness
cattr_accessor :ambiguous_year_threshold
self.ambiguous_year_threshold = 30
# Set the dummy date part for a time type value. Should be an array of 3 values
# being year, month and day in that order.
#
# Default: [ 2000, 1, 1 ] same as ActiveRecord
#
cattr_accessor :dummy_date_for_time_type
self.dummy_date_for_time_type = [ 2000, 1, 1 ]
# Format tokens:
# y = year
# m = month
@@ -196,7 +205,11 @@ module ValidatesTimeliness
break(proc) if matches = full.match(string.strip)
end
last = options[:include_offset] ? 8 : 7
processor.call(*matches[1..last]) if matches
if matches
values = processor.call(*matches[1..last])
values[0..2] = dummy_date_for_time_type if type == :time
return values
end
end
# Delete formats of specified type. Error raised if format not found.

View File

@@ -10,22 +10,17 @@ module ValidatesTimeliness
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, options.reverse_merge(:strict => true))
return nil if time_array.nil?
if type == :time
# Rails dummy time date part is defined as 2000-01-01
time_array[0..2] = 2000, 1, 1
else
# Enforce date part validity which Time class does not
return nil unless Date.valid_civil?(*time_array[0..2])
end
if type == :date
Date.new(*time_array[0..2])
Date.new(*time_array[0..2]) rescue nil
else
make_time(time_array[0..7])
end
end
def make_time(time_array)
# Enforce date part validity which Time class does not
return nil unless Date.valid_civil?(*time_array[0..2])
if Time.respond_to?(:zone) && ValidatesTimeliness.use_time_zones
Time.zone.local(*time_array)
else