123456789_123456789_123456789_123456789_123456789_

Class: TZInfo::DataSources::RubyDataSource

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
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

::TZInfo::DataSource - Inherited

.get,
.new

Initializes a new ::TZInfo::DataSource instance.

.set

Sets the currently selected data source for time zone and country data.

.create_default_data_source

Creates a ::TZInfo::DataSource instance for use as the default.

Instance Attribute Summary

Instance Method Summary

::TZInfo::DataSource - Inherited

#country_codes

Returns a frozen Array of all the available ISO 3166-1 alpha-2 country codes.

#data_timezone_identifiers

Returns a frozen Array of all the available time zone identifiers for data time zones (i.e. those that actually contain definitions).

#eager_load!

Loads all timezone and country data into memory.

#get_country_info,
#get_timezone_info

Returns a TimezoneInfo instance for the given identifier.

#inspect,
#linked_timezone_identifiers

Returns a frozen Array of all the available time zone identifiers that are links to other time zones.

#timezone_identifiers, #to_s, #load_country_info,
#load_timezone_info

Returns a TimezoneInfo instance for the given time zone identifier.

#lookup_country_info

Looks up a given code in the given hash of code to CountryInfo mappings.

#timezone_identifier_encoding,
#validate_timezone_identifier

Checks that the given identifier is a valid time zone identifier (can be found in the timezone_identifiers Array).

#build_timezone_identifiers

Combines #data_timezone_identifiers and #linked_timezone_identifiers to create an Array containing all valid time zone identifiers.

#find_timezone_identifier

If the given identifier is contained within the timezone_identifiers Array, the String instance representing that identifier from timezone_identifiers is returned.

#raise_invalid_data_source

Raises ::TZInfo::InvalidDataSource to indicate that a method has not been overridden by a particular data source implementation.

#try_with_encoding

Tries an operation using string directly.

Constructor Details

.newRubyDataSource

Initializes a new RubyDataSource instance.

Raises:

  • (TZInfoDataNotFound)

    if the tzinfo-data gem could not be found (i.e. require 'tzinfo/data' failed).

[ GitHub ]

  
# 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_codesArray<String> (readonly)

Returns a frozen Array of all the available ISO 3166-1 alpha-2 country codes. The identifiers are sorted according to String#<=>.

Returns:

  • (Array<String>)

    a frozen Array of all the available ISO 3166-1 alpha-2 country codes.

[ GitHub ]

  
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 28

attr_reader :country_codes

#data_timezone_identifiersArray<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#<=>.

Returns:

  • (Array<String>)

    a frozen Array of all the available time zone identifiers for data time zones.

[ GitHub ]

  
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 22

attr_reader :data_timezone_identifiers

#linked_timezone_identifiersArray<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#<=>.

Returns:

  • (Array<String>)

    a frozen Array of all the available time zone identifiers that are links to other time zones.

[ GitHub ]

  
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 25

attr_reader :linked_timezone_identifiers

Instance Method Details

#inspectString

Returns:

  • (String)

    the internal object state as a programmer-readable String.

[ GitHub ]

  
# 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)

Parameters:

  • code (String)

    an ISO 3166-1 alpha-2 country code.

Returns:

Raises:

[ GitHub ]

  
# 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.

Parameters:

  • identifier (String)

    A time zone identifier.

Returns:

Raises:

[ GitHub ]

  
# 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.message.encode(Encoding::UTF_8)} (loading #{valid_identifier})"
  end
end

#require_data(*file) (private)

Requires a file from tzinfo/data.

Parameters:

  • file (Array<String>)

    a relative path to a file to be required.

[ GitHub ]

  
# 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 /).

Parameters:

  • identifier (Array<string>)

    the component parts of a time zone identifier (split on /). This must have already been validated.

[ GitHub ]

  
# 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.

Parameters:

  • name (String)

    an index name.

[ GitHub ]

  
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 121

def require_index(name)
  require_data('indexes', name)
end

#to_sString

Returns:

[ GitHub ]

  
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 67

def to_s
  "Ruby DataSource: #{version_info}"
end

#version_infoString (private)

Returns:

  • (String)

    a String containing TZInfo::Data version infomation for inclusion in the #to_s and #inspect output.

[ GitHub ]

  
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 134

def version_info
  # The TZInfo::Data::VERSION constant is only available from v1.2014.8
  # onwards.
  "tzdb v#{TZInfo::Data::Version::TZDATA}#{TZInfo::Data.const_defined?(:VERSION) ? ", tzinfo-data v#{TZInfo::Data::VERSION}" : ''}"
end