From 33c298163eb0020917db45e5687eea8e0ce959e1 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Thu, 17 Jul 2008 16:25:54 +1000 Subject: [PATCH] moved format proc helper methods into formats module so they work --- lib/validates_timeliness/formats.rb | 14 ++++++++++++++ lib/validates_timeliness/validations.rb | 17 +---------------- spec/validations_spec.rb | 2 +- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/validates_timeliness/formats.rb b/lib/validates_timeliness/formats.rb index 99417e7..0a0e720 100644 --- a/lib/validates_timeliness/formats.rb +++ b/lib/validates_timeliness/formats.rb @@ -52,5 +52,19 @@ module ValidatesTimeliness :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 diff --git a/lib/validates_timeliness/validations.rb b/lib/validates_timeliness/validations.rb index 351d2a8..9d62ec9 100644 --- a/lib/validates_timeliness/validations.rb +++ b/lib/validates_timeliness/validations.rb @@ -19,21 +19,6 @@ module ValidatesTimeliness end 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 # pre or post match strings if bounded is false. Lastly fills out # time_array to full 6 part datetime array. @@ -43,7 +28,7 @@ module ValidatesTimeliness matches = regexp.match(time_string.strip) if !matches.nil? && (!bounded || (matches.pre_match == "" && matches.post_match == "")) 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 += [nil] * (6 - time_array.length) break diff --git a/spec/validations_spec.rb b/spec/validations_spec.rb index be526a5..cfc28df 100644 --- a/spec/validations_spec.rb +++ b/spec/validations_spec.rb @@ -244,7 +244,7 @@ describe ValidatesTimeliness::Validations do end 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.errors.on(:birth_time).should match(/must be after/) end