lib/validates_timeliness/formats.rb in adzap-validates_timeliness-2.1.0 vs lib/validates_timeliness/formats.rb in adzap-validates_timeliness-2.2.0
- old
+ new
@@ -21,19 +21,28 @@
:format_tokens,
:format_proc_args
# Set the threshold value for a two digit year to be considered last century
+ #
# Default: 30
#
# Example:
# year = '29' is considered 2029
# year = '30' is considered 1930
#
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
# d = day
# h = hour
@@ -177,10 +186,11 @@
def parse(string, type, options={})
return string unless string.is_a?(String)
options.reverse_merge!(:strict => true)
sets = if options[:format]
+ options[:strict] = true
[ send("#{type}_expressions").assoc(options[:format]) ]
else
expression_set(type, string)
end
@@ -193,10 +203,14 @@
when :datetime then /\A#{regexp}\Z/
end
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.
def remove_formats(type, *remove_formats)
remove_formats.each do |format|