lib/timeliness/definitions.rb in timeliness-0.4.1 vs lib/timeliness/definitions.rb in timeliness-0.4.2
- old
+ new
@@ -153,13 +153,12 @@
US_FORMAT_REGEXP = /\Am{1,2}[^m]/
FormatNotFound = Class.new(StandardError)
DuplicateFormat = Class.new(StandardError)
class << self
- extend ThreadsafeAttr
attr_accessor :time_formats, :date_formats, :datetime_formats, :format_tokens, :format_components, :timezone_mapping
- threadsafe_attr_accessor :date_format_set, :time_format_set, :datetime_format_set
+ attr_reader :time_format_set, :date_format_set, :datetime_format_set
# Adds new formats. Must specify format type and can specify a :before
# option to nominate which format the new formats should be inserted in
# front on to take higher precedence.
#
@@ -189,38 +188,54 @@
end
end
compile_formats
end
- # Removes US date formats so that ambiguous dates are parsed as European format
+ def current_date_format=(value)
+ Thread.current["Timeliness.current_date_format"] = value
+ end
+
+ def current_date_format
+ Thread.current["Timeliness.current_date_format"] ||= @current_date_format
+ end
+
+ # Get date format set for using current thread format setting
+ def date_format_set
+ instance_variable_get(:"@#{current_date_format}_date_format_set")
+ end
+
+ # Get datetime format set for using current thread format setting
+ def datetime_format_set
+ instance_variable_get(:"@#{current_date_format}_datetime_format_set")
+ end
+
+ # Use date formats that return ambiguous dates parsed in European format
#
def use_euro_formats
- self.date_format_set = @euro_date_format_set
- self.datetime_format_set = @euro_datetime_format_set
+ self.current_date_format = :euro
end
- # Restores default to parse ambiguous dates as US format
+ # Use date formats that return ambiguous dates parsed as US format
#
def use_us_formats
- self.date_format_set = @us_date_format_set
- self.datetime_format_set = @us_datetime_format_set
+ self.current_date_format = :us
end
def compile_formats
- @sorted_token_keys = nil
- self.time_format_set = FormatSet.compile(time_formats)
+ @sorted_token_keys = nil
+ @current_date_format = Timeliness.ambiguous_date_format
+ self.current_date_format = @current_date_format
+
+ @time_format_set = FormatSet.compile(time_formats)
@us_date_format_set = FormatSet.compile(date_formats)
@us_datetime_format_set = FormatSet.compile(datetime_formats)
@euro_date_format_set = FormatSet.compile(date_formats.select { |format| US_FORMAT_REGEXP !~ format })
@euro_datetime_format_set = FormatSet.compile(datetime_formats.select { |format| US_FORMAT_REGEXP !~ format })
-
- self.date_format_set = @us_date_format_set
- self.datetime_format_set = @us_datetime_format_set
end
def sorted_token_keys
- @sorted_token_keys ||= format_tokens.keys.sort {|a,b| a.size <=> b.size }.reverse
+ @sorted_token_keys ||= format_tokens.keys.sort_by(&:size).reverse
end
# Returns format for type and other possible matching format set based on type
# and value length. Gives minor speed-up by checking string length.
#