diff --git a/lib/validates_timeliness/formats.rb b/lib/validates_timeliness/formats.rb index 5fcae45..1e3f3ba 100644 --- a/lib/validates_timeliness/formats.rb +++ b/lib/validates_timeliness/formats.rb @@ -168,9 +168,9 @@ module ValidatesTimeliness exp, processor = expression_set(type, string).find do |regexp, proc| full = /\A#{regexp}\Z/ if strict full ||= case type - when :datetime then /\A#{regexp}\Z/ - when :date then /\A#{regexp}/ - else /#{regexp}\Z/ + when :date then /\A#{regexp}/ + when :time then /#{regexp}\Z/ + when :datetime then /\A#{regexp}\Z/ end matches = full.match(string.strip) end @@ -268,17 +268,17 @@ module ValidatesTimeliness # datetime attributes to allow date string as datetime def expression_set(type, string) case type - when :date - date_expressions - when :time - time_expressions - when :datetime - # gives a speed-up for date string as datetime attributes - if string.length < 11 - date_expressions + datetime_expressions - else - datetime_expressions + date_expressions - end + when :date + date_expressions + when :time + time_expressions + when :datetime + # gives a speed-up for date string as datetime attributes + if string.length < 11 + date_expressions + datetime_expressions + else + datetime_expressions + date_expressions + end end end diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 0a9a181..be04891 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -91,10 +91,10 @@ module ValidatesTimeliness return true if restriction.nil? case comparator - when Symbol - value.send(comparator, restriction) - when Proc - comparator.call(value, restriction) + when Symbol + value.send(comparator, restriction) + when Proc + comparator.call(value, restriction) end end @@ -111,13 +111,11 @@ module ValidatesTimeliness end def error_messages - return @error_messages if defined?(@error_messages) - @error_messages = ValidatesTimeliness.default_error_messages.merge(custom_error_messages) + @error_messages ||= ValidatesTimeliness.default_error_messages.merge(custom_error_messages) end def custom_error_messages - return @custom_error_messages if defined?(@custom_error_messages) - @custom_error_messages = configuration.inject({}) {|msgs, (k, v)| + @custom_error_messages ||= configuration.inject({}) {|msgs, (k, v)| if md = /(.*)_message$/.match(k.to_s) msgs[md[1].to_sym] = v end @@ -127,18 +125,18 @@ module ValidatesTimeliness def restriction_value(restriction, record) case restriction - when Time, Date, DateTime - restriction - when Symbol - restriction_value(record.send(restriction), record) - when Proc - restriction_value(restriction.call(record), record) - when Array - restriction.map {|r| restriction_value(r, record) }.sort - when Range - restriction_value([restriction.first, restriction.last], record) - else - record.class.parse_date_time(restriction, type, false) + when Time, Date, DateTime + restriction + when Symbol + restriction_value(record.send(restriction), record) + when Proc + restriction_value(restriction.call(record), record) + when Array + restriction.map {|r| restriction_value(r, record) }.sort + when Range + restriction_value([restriction.first, restriction.last], record) + else + record.class.parse_date_time(restriction, type, false) end end diff --git a/spec/active_record/attribute_methods_spec.rb b/spec/active_record/attribute_methods_spec.rb index 2139634..9abe808 100644 --- a/spec/active_record/attribute_methods_spec.rb +++ b/spec/active_record/attribute_methods_spec.rb @@ -35,7 +35,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do @person.birth_time end - it "should call rea_date_time_attribute when datetime attribute is retrieved" do + it "should call read_date_time_attribute when datetime attribute is retrieved" do @person.should_receive(:read_date_time_attribute) @person.birth_date_and_time = "2000-01-01 12:00" @person.birth_date_and_time