mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
little cleanup and consistency
This commit is contained in:
parent
1001d29c01
commit
904c202fb4
@ -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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user