diff --git a/lib/validates_timeliness/formats.rb b/lib/validates_timeliness/formats.rb index 1e3f3ba..69a830f 100644 --- a/lib/validates_timeliness/formats.rb +++ b/lib/validates_timeliness/formats.rb @@ -161,12 +161,13 @@ module ValidatesTimeliness # pre or post match strings to exist if strict is false. Otherwise wrap # regexp in start and end anchors. # Returns 7 part time array. - def parse(string, type, strict=true) + def parse(string, type, options={}) return string unless string.is_a?(String) + options.reverse_merge!(:strict => true) matches = nil exp, processor = expression_set(type, string).find do |regexp, proc| - full = /\A#{regexp}\Z/ if strict + full = /\A#{regexp}\Z/ if options[:strict] full ||= case type when :date then /\A#{regexp}/ when :time then /#{regexp}\Z/ diff --git a/lib/validates_timeliness/parser.rb b/lib/validates_timeliness/parser.rb index 2f50f3e..90b47c5 100644 --- a/lib/validates_timeliness/parser.rb +++ b/lib/validates_timeliness/parser.rb @@ -3,11 +3,13 @@ module ValidatesTimeliness class << self - def parse(raw_value, type, strict=true) + def parse(raw_value, type, options={}) return nil if raw_value.blank? return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date) - time_array = ValidatesTimeliness::Formats.parse(raw_value, type, strict) + options.reverse_merge!(:strict => true) + + time_array = ValidatesTimeliness::Formats.parse(raw_value, type, options) raise if time_array.nil? # Rails dummy time date part is defined as 2000-01-01 diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 5b5d36a..2f2badd 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -36,7 +36,7 @@ module ValidatesTimeliness end def call(record, attr_name, value) - value = ValidatesTimeliness::Parser.parse(value, type, false) if value.is_a?(String) + value = ValidatesTimeliness::Parser.parse(value, type, :strict => false) if value.is_a?(String) raw_value = raw_value(record, attr_name) || value return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank]) @@ -169,7 +169,7 @@ module ValidatesTimeliness when Range evaluate_option_value([value.first, value.last], type, record) else - ValidatesTimeliness::Parser.parse(value, type, false) + ValidatesTimeliness::Parser.parse(value, type, :strict => false) end end diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index a166a76..3e71da6 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -139,37 +139,37 @@ describe ValidatesTimeliness::Formats do describe "extracting values" do it "should return time array from date string" do - time_array = formats.parse('12:13:14', :time, true) + time_array = formats.parse('12:13:14', :time, :strict => true) time_array.should == [0,0,0,12,13,14,0] end it "should return date array from time string" do - time_array = formats.parse('2000-02-01', :date, true) + time_array = formats.parse('2000-02-01', :date, :strict => true) time_array.should == [2000,2,1,0,0,0,0] end it "should return datetime array from string value" do - time_array = formats.parse('2000-02-01 12:13:14', :datetime, true) + time_array = formats.parse('2000-02-01 12:13:14', :datetime, :strict => true) time_array.should == [2000,2,1,12,13,14,0] end it "should parse date string when type is datetime" do - time_array = formats.parse('2000-02-01', :datetime, false) + time_array = formats.parse('2000-02-01', :datetime, :strict => false) time_array.should == [2000,2,1,0,0,0,0] end it "should ignore time when extracting date and strict is false" do - time_array = formats.parse('2000-02-01 12:12', :date, false) + time_array = formats.parse('2000-02-01 12:12', :date, :strict => false) time_array.should == [2000,2,1,0,0,0,0] end it "should ignore time when extracting date from format with trailing year and strict is false" do - time_array = formats.parse('01-02-2000 12:12', :date, false) + time_array = formats.parse('01-02-2000 12:12', :date, :strict => false) time_array.should == [2000,2,1,0,0,0,0] end it "should ignore date when extracting time and strict is false" do - time_array = formats.parse('2000-02-01 12:12', :time, false) + time_array = formats.parse('2000-02-01 12:12', :time, :strict => false) time_array.should == [0,0,0,12,12,0,0] end end