Skip to content

Commit c4f177c

Browse files
committed
Add a top level eager_load! method for Rails compatibility.
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.
1 parent 94be919 commit c4f177c

4 files changed

Lines changed: 43 additions & 9 deletions

File tree

‎lib/tzinfo.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

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

820
# Object#untaint is a deprecated no-op in Ruby >= 2.7 and will be removed in

‎lib/tzinfo/data_source.rb‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,12 @@ def country_codes
247247
raise_invalid_data_source('country_codes')
248248
end
249249

250-
# Loads all timezone and country data into memory. This may be desirable in
251-
# production environments to improve copy-on-write performance and to
252-
# avoid flushing the constant cache every time a new timezone or country
253-
# is loaded from {DataSources::RubyDataSource}.
254-
def preload!
250+
# Loads all timezone and country data into memory.
251+
#
252+
# This may be desirable in production environments to improve copy-on-write
253+
# performance and to avoid flushing the constant cache every time a new
254+
# timezone or country is loaded from {DataSources::RubyDataSource}.
255+
def eager_load!
255256
timezone_identifiers.each {|identifier| load_timezone_info(identifier) }
256257
country_codes.each {|code| load_country_info(code) }
257258
nil

‎test/tc_data_source.rb‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def call_lookup_country_info(hash, code, encoding = Encoding::UTF_8)
8787
end
8888
end
8989

90-
class PreloadTestDataSource < GetTimezoneIdentifiersTestDataSource
90+
class EagerLoadTestDataSource < GetTimezoneIdentifiersTestDataSource
9191
attr_reader :country_codes_called
9292
attr_reader :loaded_timezones
9393
attr_reader :loaded_countries
@@ -581,13 +581,13 @@ def test_lookup_country_info_case
581581
end
582582
end
583583

584-
def test_preload
584+
def test_eager_load
585585
data_timezone_identifiers = ['Data/Zone1', 'Data/Zone2']
586586
linked_timezone_identifiers = ['Linked/Zone1', 'Linked/Zone2']
587587
all_timezone_identifiers = data_timezone_identifiers + linked_timezone_identifiers
588588
country_codes = ['AA', 'BB', 'CC']
589-
ds = PreloadTestDataSource.new(data_timezone_identifiers, linked_timezone_identifiers, country_codes)
590-
assert_nil(ds.preload!)
589+
ds = EagerLoadTestDataSource.new(data_timezone_identifiers, linked_timezone_identifiers, country_codes)
590+
assert_nil(ds.eager_load!)
591591
assert_equal(1, ds.data_timezone_identifiers_called)
592592
assert_equal(1, ds.linked_timezone_identifiers_called)
593593
assert_equal(all_timezone_identifiers, ds.loaded_timezones)

‎test/tc_tzinfo.rb‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
4+
require_relative 'test_utils'
5+
6+
class TCTZInfo < Minitest::Test
7+
def test_eager_load
8+
test_data_source = Minitest::Mock.new
9+
test_data_source.expect(:kind_of?, true, [DataSource])
10+
test_data_source.expect(:eager_load!, nil)
11+
12+
orig_data_source = DataSource.get
13+
DataSource.set(test_data_source)
14+
begin
15+
assert_nil(TZInfo.eager_load!)
16+
test_data_source.verify
17+
ensure
18+
DataSource.set(orig_data_source)
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)