simplify tokens to hash and generate in reverse order of token length

This commit is contained in:
Adam Meehan 2010-09-17 13:42:21 +10:00
parent 1df8ab121e
commit 1fb89d6455

View File

@ -119,7 +119,7 @@ module ValidatesTimeliness
# All tokens available for format construction. The token array is made of # All tokens available for format construction. The token array is made of
# token regexp, validation regexp and key for format proc mapping if any. # validation regexp and key for format proc mapping if any.
# If the token needs no format proc arg then the validation regexp should # If the token needs no format proc arg then the validation regexp should
# not have a capturing group, as all captured groups are passed to the # not have a capturing group, as all captured groups are passed to the
# format proc. # format proc.
@ -127,27 +127,28 @@ module ValidatesTimeliness
# The token regexp should only use a capture group if 'look-behind' anchor # The token regexp should only use a capture group if 'look-behind' anchor
# is required. The first capture group will be considered a literal and put # is required. The first capture group will be considered a literal and put
# into the validation regexp string as-is. This is a hack. # into the validation regexp string as-is. This is a hack.
@@format_tokens = [ #
{ 'ddd' => [ /d{3}/, '\w{3,9}' ] }, @@format_tokens = {
{ 'dd' => [ /d{2}/, '\d{2}', :day ] }, 'ddd' => [ '\w{3,9}' ],
{ 'd' => [ /d/, '\d{1,2}', :day ] }, 'dd' => [ '\d{2}', :day ],
{ 'ampm' => [ /ampm/, '[aApP]\.?[mM]\.?', :meridian ] }, 'd' => [ '\d{1,2}', :day ],
{ 'mmm' => [ /m{3}/, '\w{3,9}', :month ] }, 'ampm' => [ '[aApP]\.?[mM]\.?', :meridian ],
{ 'mm' => [ /m{2}/, '\d{2}', :month ] }, 'mmm' => [ '\w{3,9}', :month ],
{ 'm' => [ /m{1}/, '\d{1,2}', :month ] }, 'mm' => [ '\d{2}', :month ],
{ 'yyyy' => [ /y{4}/, '\d{4}', :year ] }, 'm' => [ '\d{1,2}', :month ],
{ 'yy' => [ /y{2}/, '\d{4}|\d{2}', :year ] }, 'yyyy' => [ '\d{4}', :year ],
{ 'hh' => [ /h{2}/, '\d{2}', :hour ] }, 'yy' => [ '\d{4}|\d{2}', :year ],
{ 'h' => [ /h{1}/, '\d{1,2}', :hour ] }, 'hh' => [ '\d{2}', :hour ],
{ 'nn' => [ /n{2}/, '\d{2}', :min ] }, 'h' => [ '\d{1,2}', :hour ],
{ 'n' => [ /n{1}/, '\d{1,2}', :min ] }, 'nn' => [ '\d{2}', :min ],
{ 'ss' => [ /s{2}/, '\d{2}', :sec ] }, 'n' => [ '\d{1,2}', :min ],
{ 's' => [ /s{1}/, '\d{1,2}', :sec ] }, 'ss' => [ '\d{2}', :sec ],
{ 'u' => [ /u{1}/, '\d{1,6}', :usec ] }, 's' => [ '\d{1,2}', :sec ],
{ 'zo' => [ /zo/, '[+-]\d{2}:?\d{2}', :offset ] }, 'u' => [ '\d{1,6}', :usec ],
{ 'tz' => [ /tz/, '[A-Z]{1,4}' ] }, 'zo' => [ '[+-]\d{2}:?\d{2}', :offset ],
{ '_' => [ /_/, '\s?' ] } 'tz' => [ '[A-Z]{1,4}' ],
] '_' => [ '\s?' ]
}
# Arguments which will be passed to the format proc if matched in the # Arguments which will be passed to the format proc if matched in the
# time string. The key must be the key from the format tokens. The array # time string. The key must be the key from the format tokens. The array
@ -331,9 +332,10 @@ module ValidatesTimeliness
format.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes format.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes
found_tokens, token_order = [], [] found_tokens, token_order = [], []
format_tokens.each do |token| tokens = format_tokens.keys.sort {|a,b| a.size <=> b.size }.reverse
token_regexp, regexp_str, arg_key = *token.values.first tokens.each do |token|
if format.gsub!(token_regexp, "%<#{found_tokens.size}>") regexp_str, arg_key = *format_tokens[token]
if format.gsub!(/#{token}/, "%<#{found_tokens.size}>")
regexp_str = "(#{regexp_str})" if arg_key regexp_str = "(#{regexp_str})" if arg_key
found_tokens << [regexp_str, arg_key] found_tokens << [regexp_str, arg_key]
end end