changed Formats#parse to take options hash for strict and other possibilities

This commit is contained in:
Adam Meehan 2009-03-28 17:35:41 +11:00
parent c2a4f45b5a
commit a836ed8434
4 changed files with 16 additions and 13 deletions

View File

@ -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/

View File

@ -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

View File

@ -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

View File

@ -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