little cleanup and consistency

This commit is contained in:
Adam Meehan 2009-02-09 12:11:56 +11:00
parent 1001d29c01
commit 904c202fb4
3 changed files with 33 additions and 35 deletions

View File

@ -168,9 +168,9 @@ module ValidatesTimeliness
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 strict
full ||= case type full ||= case type
when :datetime then /\A#{regexp}\Z/ when :date then /\A#{regexp}/
when :date then /\A#{regexp}/ when :time then /#{regexp}\Z/
else /#{regexp}\Z/ when :datetime then /\A#{regexp}\Z/
end end
matches = full.match(string.strip) matches = full.match(string.strip)
end end
@ -268,17 +268,17 @@ module ValidatesTimeliness
# datetime attributes to allow date string as datetime # datetime attributes to allow date string as datetime
def expression_set(type, string) def expression_set(type, string)
case type case type
when :date when :date
date_expressions date_expressions
when :time when :time
time_expressions time_expressions
when :datetime when :datetime
# gives a speed-up for date string as datetime attributes # gives a speed-up for date string as datetime attributes
if string.length < 11 if string.length < 11
date_expressions + datetime_expressions date_expressions + datetime_expressions
else else
datetime_expressions + date_expressions datetime_expressions + date_expressions
end end
end end
end end

View File

@ -91,10 +91,10 @@ module ValidatesTimeliness
return true if restriction.nil? return true if restriction.nil?
case comparator case comparator
when Symbol when Symbol
value.send(comparator, restriction) value.send(comparator, restriction)
when Proc when Proc
comparator.call(value, restriction) comparator.call(value, restriction)
end end
end end
@ -111,13 +111,11 @@ module ValidatesTimeliness
end end
def error_messages 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 end
def custom_error_messages 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) if md = /(.*)_message$/.match(k.to_s)
msgs[md[1].to_sym] = v msgs[md[1].to_sym] = v
end end
@ -127,18 +125,18 @@ module ValidatesTimeliness
def restriction_value(restriction, record) def restriction_value(restriction, record)
case restriction case restriction
when Time, Date, DateTime when Time, Date, DateTime
restriction restriction
when Symbol when Symbol
restriction_value(record.send(restriction), record) restriction_value(record.send(restriction), record)
when Proc when Proc
restriction_value(restriction.call(record), record) restriction_value(restriction.call(record), record)
when Array when Array
restriction.map {|r| restriction_value(r, record) }.sort restriction.map {|r| restriction_value(r, record) }.sort
when Range when Range
restriction_value([restriction.first, restriction.last], record) restriction_value([restriction.first, restriction.last], record)
else else
record.class.parse_date_time(restriction, type, false) record.class.parse_date_time(restriction, type, false)
end end
end end

View File

@ -35,7 +35,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
@person.birth_time @person.birth_time
end 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.should_receive(:read_date_time_attribute)
@person.birth_date_and_time = "2000-01-01 12:00" @person.birth_date_and_time = "2000-01-01 12:00"
@person.birth_date_and_time @person.birth_date_and_time