mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 23:06:42 +00:00
changed Formats#parse to take options hash for strict and other possibilities
This commit is contained in:
parent
c2a4f45b5a
commit
a836ed8434
@ -161,12 +161,13 @@ module ValidatesTimeliness
|
|||||||
# pre or post match strings to exist if strict is false. Otherwise wrap
|
# pre or post match strings to exist if strict is false. Otherwise wrap
|
||||||
# regexp in start and end anchors.
|
# regexp in start and end anchors.
|
||||||
# Returns 7 part time array.
|
# Returns 7 part time array.
|
||||||
def parse(string, type, strict=true)
|
def parse(string, type, options={})
|
||||||
return string unless string.is_a?(String)
|
return string unless string.is_a?(String)
|
||||||
|
options.reverse_merge!(:strict => true)
|
||||||
|
|
||||||
matches = nil
|
matches = nil
|
||||||
exp, processor = expression_set(type, string).find do |regexp, proc|
|
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
|
full ||= case type
|
||||||
when :date then /\A#{regexp}/
|
when :date then /\A#{regexp}/
|
||||||
when :time then /#{regexp}\Z/
|
when :time then /#{regexp}\Z/
|
||||||
|
|||||||
@ -3,11 +3,13 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
def parse(raw_value, type, strict=true)
|
def parse(raw_value, type, options={})
|
||||||
return nil if raw_value.blank?
|
return nil if raw_value.blank?
|
||||||
return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
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?
|
raise if time_array.nil?
|
||||||
|
|
||||||
# Rails dummy time date part is defined as 2000-01-01
|
# Rails dummy time date part is defined as 2000-01-01
|
||||||
|
|||||||
@ -36,7 +36,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def call(record, attr_name, value)
|
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
|
raw_value = raw_value(record, attr_name) || value
|
||||||
|
|
||||||
return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank])
|
return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank])
|
||||||
@ -169,7 +169,7 @@ module ValidatesTimeliness
|
|||||||
when Range
|
when Range
|
||||||
evaluate_option_value([value.first, value.last], type, record)
|
evaluate_option_value([value.first, value.last], type, record)
|
||||||
else
|
else
|
||||||
ValidatesTimeliness::Parser.parse(value, type, false)
|
ValidatesTimeliness::Parser.parse(value, type, :strict => false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -139,37 +139,37 @@ describe ValidatesTimeliness::Formats do
|
|||||||
describe "extracting values" do
|
describe "extracting values" do
|
||||||
|
|
||||||
it "should return time array from date string" 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]
|
time_array.should == [0,0,0,12,13,14,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return date array from time string" do
|
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]
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return datetime array from string value" do
|
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]
|
time_array.should == [2000,2,1,12,13,14,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should parse date string when type is datetime" do
|
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]
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should ignore time when extracting date and strict is false" do
|
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]
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should ignore time when extracting date from format with trailing year and strict is false" do
|
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]
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should ignore date when extracting time and strict is false" do
|
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]
|
time_array.should == [0,0,0,12,12,0,0]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user