Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow hot switch of US and Euro formats, no compile

This is intended to assist in timezone switches in apps to match
the allowed formats without a compile hit.
  • Loading branch information...
commit 12bcad180deba96720a139c0907fdd04a47da932 1 parent ed70733
@adzap authored
Showing with 29 additions and 9 deletions.
  1. +16 −8 lib/timeliness/definitions.rb
  2. +13 −1 spec/timeliness/definitions_spec.rb
View
24 lib/timeliness/definitions.rb
@@ -130,6 +130,7 @@ module Definitions
# Mapping some common timezone abbreviations which are not mapped or
# mapped inconsistenly in ActiveSupport (TzInfo).
+ #
@timezone_mapping = {
'AEST' => 'Australia/Sydney',
'AEDT' => 'Australia/Sydney',
@@ -184,22 +185,28 @@ def remove_formats(type, *remove_formats)
# Removes US date formats so that ambiguous dates are parsed as European format
#
def use_euro_formats
- @date_format_set = FormatSet.compile(date_formats.select { |format| US_FORMAT_REGEXP !~ format })
- @datetime_format_set = FormatSet.compile(datetime_formats.select { |format| US_FORMAT_REGEXP !~ format })
+ @date_format_set = @euro_date_format_set
+ @datetime_format_set = @euro_datetime_format_set
end
# Restores default to parse ambiguous dates as US format
#
def use_us_formats
- @date_format_set = FormatSet.compile(date_formats)
- @datetime_format_set = FormatSet.compile(datetime_formats)
+ @date_format_set = @us_date_format_set
+ @datetime_format_set = @us_datetime_format_set
end
def compile_formats
- @sorted_token_keys = nil
- @time_format_set = FormatSet.compile(time_formats)
- @date_format_set = FormatSet.compile(date_formats)
- @datetime_format_set = FormatSet.compile(datetime_formats)
+ @sorted_token_keys = nil
+ @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 })
+
+ @date_format_set = @us_date_format_set
+ @datetime_format_set = @us_datetime_format_set
end
def sorted_token_keys
@@ -208,6 +215,7 @@ def sorted_token_keys
# Returns format for type and other possible matching format set based on type
# and value length. Gives minor speed-up by checking string length.
+ #
def format_sets(type, string)
case type
when :date
View
14 spec/timeliness/definitions_spec.rb
@@ -78,9 +78,15 @@
definitions.use_euro_formats
parser._parse('01/02/2000', :date).should eq [2000,2,1,nil,nil,nil,nil,nil]
end
+
+ it "should not parse formats on switch to euro after initial compile" do
+ definitions.compile_formats
+ Timeliness::FormatSet.should_not_receive(:compile)
+ definitions.use_euro_formats
+ end
end
- context "use_use_formats" do
+ context "use_us_formats" do
before do
definitions.use_euro_formats
end
@@ -90,5 +96,11 @@
definitions.use_us_formats
parser._parse('01/02/2000', :date).should eq [2000,1,2,nil,nil,nil,nil,nil]
end
+
+ it "should not parse formats on switch to euro after initial compile" do
+ definitions.compile_formats
+ Timeliness::FormatSet.should_not_receive(:compile)
+ definitions.use_us_formats
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.