lib/validates_timeliness/formats.rb in validates_timeliness-1.1.2 vs lib/validates_timeliness/formats.rb in validates_timeliness-1.1.3
- old
+ new
@@ -159,22 +159,22 @@
# pre or post match strings to exist if strict is false. Otherwise wrap
# regexp in start and end anchors.
# Returns 7 part time array.
def parse(string, type, strict=true)
return string unless string.is_a?(String)
-
- expressions = expression_set(type, string)
- # TODO cleanup using select
- time_array = nil
- expressions.each do |(regexp, processor)|
- regexp = strict || type == :datetime ? /\A#{regexp}\Z/ : (type == :date ? /\A#{regexp}/ : /#{regexp}\Z/)
- if matches = regexp.match(string.strip)
- time_array = processor.call(*matches[1..7])
- break
+
+ matches = nil
+ 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/
end
+ matches = full.match(string.strip)
end
- time_array
+ processor.call(*matches[1..7]) if matches
end
# Delete formats of specified type. Error raised if format not found.
def remove_formats(type, *remove_formats)
remove_formats.each do |format|
@@ -296,25 +296,9 @@
end
def month_index(month)
return month.to_i if month.to_i.nonzero?
Date::ABBR_MONTHNAMES.index(month.capitalize) || Date::MONTHNAMES.index(month.capitalize)
- end
-
- def month_names
- @@month_names = if defined?(I18n)
- I18n('dates.months')
- else
- Date::MONTHNAMES
- end
- end
-
- def abbreviated_month_names
- @@abbreviated_month_names = if defined?(I18n)
- I18n('dates.months')
- else
- Date::ABBR_MONTHNAMES
- end
end
def microseconds(usec)
(".#{usec}".to_f * 1_000_000).to_i
end