Class: TZInfo::DataSources::RubyDataSource
- Inherits:
-
TZInfo::DataSource
- Object
- TZInfo::DataSource
- TZInfo::DataSources::RubyDataSource
- Defined in:
- lib/tzinfo/data_sources/ruby_data_source.rb
Overview
A DataSource implementation that loads data from the set of Ruby modules included in the tzinfo-data gem.
TZInfo will use RubyDataSource by default if the tzinfo-data gem is available on the load path. It can also be selected by calling TZInfo::DataSource.set as follows:
TZInfo::DataSource.set(:ruby)
Instance Attribute Summary collapse
-
#country_codes ⇒ Array<String>
readonly
Returns a frozen
Array
of all the available ISO 3166-1 alpha-2 country codes. -
#data_timezone_identifiers ⇒ Array<String>
readonly
Returns a frozen
Array
of all the available time zone identifiers for data time zones (i.e. those that actually contain definitions). -
#linked_timezone_identifiers ⇒ Array<String>
readonly
Returns a frozen
Array
of all the available time zone identifiers that are links to other time zones.
Instance Method Summary collapse
-
#initialize ⇒ RubyDataSource
constructor
Initializes a new RubyDataSource instance.
-
#inspect ⇒ String
The internal object state as a programmer-readable
String
. -
#load_country_info(code) ⇒ DataSources::CountryInfo
protected
A CountryInfo instance for the given ISO 3166-1 alpha-2 country code.
-
#load_timezone_info(identifier) ⇒ TimezoneInfo
protected
Returns a TimezoneInfo instance for the given time zone identifier.
-
#to_s ⇒ String
A description of the TZInfo::DataSource.
Methods inherited from TZInfo::DataSource
#eager_load!, get, #get_country_info, #get_timezone_info, #lookup_country_info, set, #timezone_identifier_encoding, #timezone_identifiers, #validate_timezone_identifier
Constructor Details
#initialize ⇒ RubyDataSource
Initializes a new TZInfo::DataSources::RubyDataSource instance.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 34 def initialize super begin require('tzinfo/data') rescue LoadError raise TZInfoDataNotFound, "The tzinfo-data gem could not be found (require 'tzinfo/data' failed)." end if TZInfo::Data.const_defined?(:LOCATION) # Format 2 @base_path = File.join(TZInfo::Data::LOCATION, 'tzinfo', 'data') else # Format 1 data_file = File.join('', 'tzinfo', 'data.rb') path = $".reverse_each.detect {|p| p.end_with?(data_file) } if path @base_path = RubyCoreSupport.untaint(File.join(File.dirname(path), 'data')) else @base_path = 'tzinfo/data' end end require_index('timezones') require_index('countries') @data_timezone_identifiers = Data::Indexes::Timezones.data_timezones @linked_timezone_identifiers = Data::Indexes::Timezones.linked_timezones @countries = Data::Indexes::Countries.countries @country_codes = @countries.keys.sort!.freeze end |
Instance Attribute Details
#country_codes ⇒ Array<String> (readonly)
Returns a frozen Array
of all the available ISO 3166-1 alpha-2 country
codes. The identifiers are sorted according to String#<=>
.
28 29 30 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 28 def country_codes @country_codes end |
#data_timezone_identifiers ⇒ Array<String> (readonly)
Returns a frozen Array
of all the available time zone identifiers for
data time zones (i.e. those that actually contain definitions). The
identifiers are sorted according to String#<=>
.
22 23 24 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 22 def data_timezone_identifiers @data_timezone_identifiers end |
#linked_timezone_identifiers ⇒ Array<String> (readonly)
Returns a frozen Array
of all the available time zone identifiers that
are links to other time zones. The identifiers are sorted according to
String#<=>
.
25 26 27 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 25 def linked_timezone_identifiers @linked_timezone_identifiers end |
Instance Method Details
#inspect ⇒ String
Returns the internal object state as a programmer-readable
String
.
72 73 74 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 72 def inspect "#<TZInfo::DataSources::RubyDataSource: #{version_info}>" end |
#load_country_info(code) ⇒ DataSources::CountryInfo (protected)
Returns a CountryInfo instance for the given ISO 3166-1 alpha-2 country code.
104 105 106 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 104 def load_country_info(code) lookup_country_info(@countries, code) end |
#load_timezone_info(identifier) ⇒ TimezoneInfo (protected)
Returns a TimezoneInfo instance for the given time zone identifier. The result will either be a ConstantOffsetDataTimezoneInfo, a TransitionsDataTimezoneInfo or a LinkedTimezoneInfo depending on the type of time zone.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 88 def load_timezone_info(identifier) valid_identifier = validate_timezone_identifier(identifier) split_identifier = valid_identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__').split('/') begin require_definition(split_identifier) m = Data::Definitions split_identifier.each {|part| m = m.const_get(part) } m.get rescue LoadError, NameError => e raise InvalidTimezoneIdentifier, "#{e..encode(Encoding::UTF_8)} (loading #{valid_identifier})" end end |
#to_s ⇒ String
Returns a description of the TZInfo::DataSource.
67 68 69 |
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 67 def to_s "Ruby DataSource: #{version_info}" end |