lib/validates_timeliness/formats.rb in validates_timeliness-1.1.1 vs lib/validates_timeliness/formats.rb in validates_timeliness-1.1.2

- old
+ new

@@ -8,21 +8,19 @@ # # Formats can be added or removed to customize the set of valid date or time # string values. # class Formats - cattr_accessor :time_formats - cattr_accessor :date_formats - cattr_accessor :datetime_formats + cattr_accessor :time_formats, + :date_formats, + :datetime_formats, + :time_expressions, + :date_expressions, + :datetime_expressions, + :format_tokens, + :format_proc_args - cattr_accessor :time_expressions - cattr_accessor :date_expressions - cattr_accessor :datetime_expressions - - cattr_accessor :format_tokens - cattr_accessor :format_proc_args - # Format tokens: # y = year # m = month # d = day # h = hour @@ -137,17 +135,17 @@ # # The code can be used to manipulate the arg value if required, otherwise # should just be the arg name. # @@format_proc_args = { - :year => [0, 'y', 'unambiguous_year(y)'], - :month => [1, 'm', 'month_index(m)'], - :day => [2, 'd', 'd'], - :hour => [3, 'h', 'full_hour(h,md)'], - :min => [4, 'n', 'n'], - :sec => [5, 's', 's'], - :usec => [6, 'u', 'microseconds(u)'], + :year => [0, 'y', 'unambiguous_year(y)'], + :month => [1, 'm', 'month_index(m)'], + :day => [2, 'd', 'd'], + :hour => [3, 'h', 'full_hour(h,md)'], + :min => [4, 'n', 'n'], + :sec => [5, 's', 's'], + :usec => [6, 'u', 'microseconds(u)'], :meridian => [nil, 'md', nil] } class << self @@ -163,19 +161,20 @@ # 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 end end - return time_array + time_array end # Delete formats of specified type. Error raised if format not found. def remove_formats(type, *remove_formats) remove_formats.each do |format| @@ -221,11 +220,11 @@ # Compile formats into validation regexps and format procs def format_expression_generator(string_format) regexp = string_format.dup order = {} - regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes ]/ + regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes format_tokens.each do |token| token_name = token.keys.first token_regexp, regexp_str, arg_key = *token.values.first @@ -258,11 +257,11 @@ proc_string = "lambda {|#{args.join(',')}| md||=nil; [#{arr.map {|i| i.nil? ? 'nil' : i }.join(',')}].map {|i| i.to_i } }" eval proc_string end 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 # Pick expression set and combine date and datetimes for # datetime attributes to allow date string as datetime def expression_set(type, string) @@ -297,9 +296,25 @@ 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