Class: TZInfo::DataSources::RubyDataSource
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::TZInfo::DataSource
|
|
Instance Chain:
self,
::TZInfo::DataSource
|
|
Inherits: |
TZInfo::DataSource
|
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)
Class Method Summary
-
.new ⇒ RubyDataSource
constructor
Initializes a new
RubyDataSource
instance.
::TZInfo::DataSource
- Inherited
.get, | |
.new | Initializes a new |
.set | Sets the currently selected data source for time zone and country data. |
.create_default_data_source | Creates a |
Instance Attribute Summary
-
#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
- #inspect ⇒ String
- #to_s ⇒ String
- #load_country_info(code) ⇒ DataSources::CountryInfo protected
-
#load_timezone_info(identifier) ⇒ TimezoneInfo
protected
Returns a
TimezoneInfo
instance for the given time zone identifier. -
#require_data(*file)
private
Requires a file from tzinfo/data.
-
#require_definition(identifier)
private
Requires a zone definition by its identifier (split on /).
-
#require_index(name)
private
Requires an index by its name.
- #version_info ⇒ String private
::TZInfo::DataSource
- Inherited
#country_codes | Returns a frozen |
#data_timezone_identifiers | Returns a frozen |
#eager_load! | Loads all timezone and country data into memory. |
#get_country_info, | |
#get_timezone_info | Returns a |
#inspect, | |
#linked_timezone_identifiers | Returns a frozen |
#timezone_identifiers, #to_s, #load_country_info, | |
#load_timezone_info | Returns a |
#lookup_country_info | Looks up a given code in the given hash of code to |
#timezone_identifier_encoding, | |
#validate_timezone_identifier | Checks that the given identifier is a valid time zone identifier (can be found in the |
#build_timezone_identifiers | Combines #data_timezone_identifiers and #linked_timezone_identifiers to create an |
#find_timezone_identifier | If the given |
#raise_invalid_data_source | Raises |
#try_with_encoding | Tries an operation using |
Constructor Details
.new ⇒ RubyDataSource
Initializes a new RubyDataSource
instance.
# 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#<=>
.
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 28
attr_reader :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). The
identifiers are sorted according to String#<=>
.
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 22
attr_reader :data_timezone_identifiers
#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#<=>
.
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 25
attr_reader :linked_timezone_identifiers
Instance Method Details
#inspect ⇒ String
# 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)
# 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.
# 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
#require_data(*file) (private)
Requires a file from tzinfo/data.
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 128
def require_data(*file) require(File.join(@base_path, *file)) end
#require_definition(identifier) (private)
Requires a zone definition by its identifier (split on /).
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 114
def require_definition(identifier) require_data('definitions', *identifier) end
#require_index(name) (private)
Requires an index by its name.
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 121
def require_index(name) require_data('indexes', name) end
#to_s ⇒ String
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 67
def to_s "Ruby DataSource: #{version_info}" end