Skip to content

Commit

Permalink
Add a top level eager_load! method for Rails compatibility.
Browse files Browse the repository at this point in the history
Allows TZInfo to be eager loaded in Rails with:

config.eager_load_namespaces << TZInfo

Calls the eager_load! method (renamed from preload!) of the currently
selected DataSource.
  • Loading branch information
philr committed Jul 18, 2022
1 parent 94be919 commit c4f177c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
12 changes: 12 additions & 0 deletions lib/tzinfo.rb
Expand Up @@ -3,6 +3,18 @@

# The top level module for TZInfo.
module TZInfo
class << self
# Instructs the current {DataSource} to load all timezone and country data
# into memory (initializing the {DataSource} first if not previously
# accessed or set).
#
# This may be desirable in production environments to improve copy-on-write
# performance and to avoid flushing the constant cache every time a new
# timezone or country is loaded from {DataSources::RubyDataSource}.
def eager_load!
DataSource.get.eager_load!
end
end
end

# Object#untaint is a deprecated no-op in Ruby >= 2.7 and will be removed in
Expand Down
11 changes: 6 additions & 5 deletions lib/tzinfo/data_source.rb
Expand Up @@ -247,11 +247,12 @@ def country_codes
raise_invalid_data_source('country_codes')
end

# Loads all timezone and country data into memory. This may be desirable in
# production environments to improve copy-on-write performance and to
# avoid flushing the constant cache every time a new timezone or country
# is loaded from {DataSources::RubyDataSource}.
def preload!
# Loads all timezone and country data into memory.
#
# This may be desirable in production environments to improve copy-on-write
# performance and to avoid flushing the constant cache every time a new
# timezone or country is loaded from {DataSources::RubyDataSource}.
def eager_load!
timezone_identifiers.each {|identifier| load_timezone_info(identifier) }
country_codes.each {|code| load_country_info(code) }
nil
Expand Down
8 changes: 4 additions & 4 deletions test/tc_data_source.rb
Expand Up @@ -87,7 +87,7 @@ def call_lookup_country_info(hash, code, encoding = Encoding::UTF_8)
end
end

class PreloadTestDataSource < GetTimezoneIdentifiersTestDataSource
class EagerLoadTestDataSource < GetTimezoneIdentifiersTestDataSource
attr_reader :country_codes_called
attr_reader :loaded_timezones
attr_reader :loaded_countries
Expand Down Expand Up @@ -581,13 +581,13 @@ def test_lookup_country_info_case
end
end

def test_preload
def test_eager_load
data_timezone_identifiers = ['Data/Zone1', 'Data/Zone2']
linked_timezone_identifiers = ['Linked/Zone1', 'Linked/Zone2']
all_timezone_identifiers = data_timezone_identifiers + linked_timezone_identifiers
country_codes = ['AA', 'BB', 'CC']
ds = PreloadTestDataSource.new(data_timezone_identifiers, linked_timezone_identifiers, country_codes)
assert_nil(ds.preload!)
ds = EagerLoadTestDataSource.new(data_timezone_identifiers, linked_timezone_identifiers, country_codes)
assert_nil(ds.eager_load!)
assert_equal(1, ds.data_timezone_identifiers_called)
assert_equal(1, ds.linked_timezone_identifiers_called)
assert_equal(all_timezone_identifiers, ds.loaded_timezones)
Expand Down
21 changes: 21 additions & 0 deletions test/tc_tzinfo.rb
@@ -0,0 +1,21 @@
# encoding: UTF-8
# frozen_string_literal: true

require_relative 'test_utils'

class TCTZInfo < Minitest::Test
def test_eager_load
test_data_source = Minitest::Mock.new
test_data_source.expect(:kind_of?, true, [DataSource])
test_data_source.expect(:eager_load!, nil)

orig_data_source = DataSource.get
DataSource.set(test_data_source)
begin
assert_nil(TZInfo.eager_load!)
test_data_source.verify
ensure
DataSource.set(orig_data_source)
end
end
end

0 comments on commit c4f177c

Please sign in to comment.