mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
small refactor and cleanup of formats class
This commit is contained in:
parent
6af61917dd
commit
7c9ec695f4
@ -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
|
||||||
@ -164,16 +162,17 @@ module ValidatesTimeliness
|
|||||||
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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user