moved format proc helper methods into formats module so they work

This commit is contained in:
Adam Meehan
2008-07-17 16:25:54 +10:00
parent c37c3e82fa
commit 33c298163e
3 changed files with 16 additions and 17 deletions

View File

@@ -52,5 +52,19 @@ module ValidatesTimeliness
:iso8601 => /#{valid_date_formats[:yyyymmdd_dashes]}T#{valid_time_formats[:hhnnss_colons]}(?:Z|[-+](\d{2}):(\d{2}))?/ :iso8601 => /#{valid_date_formats[:yyyymmdd_dashes]}T#{valid_time_formats[:hhnnss_colons]}(?:Z|[-+](\d{2}):(\d{2}))?/
} }
def self.full_hour(hour, meridian)
hour = hour.to_i
if meridian.delete('.').downcase == 'am'
hour == 12 ? 0 : hour
else
hour == 12 ? hour : hour + 12
end
end
def self.unambiguous_year(year, threshold=30)
year = "#{year.to_i < threshold ? '20' : '19'}#{year}" if year.length == 2
year.to_i
end
end end
end end

View File

@@ -19,21 +19,6 @@ module ValidatesTimeliness
end end
module ClassMethods module ClassMethods
def full_hour(hour, meridian)
hour = hour.to_i
if meridian.delete('.').downcase == 'am'
hour == 12 ? 0 : hour
else
hour == 12 ? hour : hour + 12
end
end
def unambiguous_year(year, threshold=30)
year = "#{year.to_i < threshold ? '20' : '19'}#{year}" if year.length == 2
year.to_i
end
# loop through format regexps and call proc on matches if available. Allow # loop through format regexps and call proc on matches if available. Allow
# pre or post match strings if bounded is false. Lastly fills out # pre or post match strings if bounded is false. Lastly fills out
# time_array to full 6 part datetime array. # time_array to full 6 part datetime array.
@@ -43,7 +28,7 @@ module ValidatesTimeliness
matches = regexp.match(time_string.strip) matches = regexp.match(time_string.strip)
if !matches.nil? && (!bounded || (matches.pre_match == "" && matches.post_match == "")) if !matches.nil? && (!bounded || (matches.pre_match == "" && matches.post_match == ""))
time_array = matches[1..6] if processor.nil? time_array = matches[1..6] if processor.nil?
time_array = processor.call(matches[1..6]) unless processor.nil? time_array = processor.call(*matches[1..6]) unless processor.nil?
time_array = time_array.map {|i| i.to_i } time_array = time_array.map {|i| i.to_i }
time_array += [nil] * (6 - time_array.length) time_array += [nil] * (6 - time_array.length)
break break

View File

@@ -244,7 +244,7 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error when on boundary of :after restriction" do it "should have error when on boundary of :after restriction" do
@person.birth_time = "06:00" @person.birth_time = "06:00am"
@person.should_not be_valid @person.should_not be_valid
@person.errors.on(:birth_time).should match(/must be after/) @person.errors.on(:birth_time).should match(/must be after/)
end end