small refactor and cleanup of formats class

This commit is contained in:
Adam Meehan 2009-01-12 21:42:14 +11:00
parent 6af61917dd
commit 7c9ec695f4

View File

@ -10,16 +10,14 @@ module ValidatesTimeliness
# string values. # string values.
# #
class Formats class Formats
cattr_accessor :time_formats cattr_accessor :time_formats,
cattr_accessor :date_formats :date_formats,
cattr_accessor :datetime_formats :datetime_formats,
:time_expressions,
cattr_accessor :time_expressions :date_expressions,
cattr_accessor :date_expressions :datetime_expressions,
cattr_accessor :datetime_expressions :format_tokens,
:format_proc_args
cattr_accessor :format_tokens
cattr_accessor :format_proc_args
# Format tokens: # Format tokens:
# y = year # y = year
@ -139,13 +137,13 @@ module ValidatesTimeliness
# should just be the arg name. # should just be the arg name.
# #
@@format_proc_args = { @@format_proc_args = {
:year => [0, 'y', 'unambiguous_year(y)'], :year => [0, 'y', 'unambiguous_year(y)'],
:month => [1, 'm', 'month_index(m)'], :month => [1, 'm', 'month_index(m)'],
:day => [2, 'd', 'd'], :day => [2, 'd', 'd'],
:hour => [3, 'h', 'full_hour(h,md)'], :hour => [3, 'h', 'full_hour(h,md)'],
:min => [4, 'n', 'n'], :min => [4, 'n', 'n'],
:sec => [5, 's', 's'], :sec => [5, 's', 's'],
:usec => [6, 'u', 'microseconds(u)'], :usec => [6, 'u', 'microseconds(u)'],
:meridian => [nil, 'md', nil] :meridian => [nil, 'md', nil]
} }
@ -163,17 +161,18 @@ module ValidatesTimeliness
# Returns 7 part time array. # Returns 7 part time array.
def parse(string, type, strict=true) def parse(string, type, strict=true)
return string unless string.is_a?(String) return string unless string.is_a?(String)
expressions = expression_set(type, string) matches = nil
time_array = nil exp, processor = expression_set(type, string).find do |regexp, proc|
expressions.each do |(regexp, processor)| full = /\A#{regexp}\Z/ if strict
regexp = strict || type == :datetime ? /\A#{regexp}\Z/ : (type == :date ? /\A#{regexp}/ : /#{regexp}\Z/) full ||= case type
if matches = regexp.match(string.strip) when :datetime then /\A#{regexp}\Z/
time_array = processor.call(*matches[1..7]) when :date then /\A#{regexp}/
break else /#{regexp}\Z/
end end
matches = full.match(string.strip)
end end
return time_array processor.call(*matches[1..7]) if matches
end end
# Delete formats of specified type. Error raised if format not found. # Delete formats of specified type. Error raised if format not found.
@ -223,7 +222,7 @@ module ValidatesTimeliness
def format_expression_generator(string_format) def format_expression_generator(string_format)
regexp = string_format.dup regexp = string_format.dup
order = {} order = {}
regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes ]/ regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes
format_tokens.each do |token| format_tokens.each do |token|
token_name = token.keys.first token_name = token.keys.first
@ -260,7 +259,7 @@ module ValidatesTimeliness
end end
def compile_formats(formats) def compile_formats(formats)
formats.collect { |format| regexp, format_proc = format_expression_generator(format) } formats.map { |format| regexp, format_proc = format_expression_generator(format) }
end end
# Pick expression set and combine date and datetimes for # Pick expression set and combine date and datetimes for