Class: TZInfo::RubyDataSource

Inherits:
DataSource show all
Defined in:
lib/tzinfo/ruby_data_source.rb

Overview

A DataSource that loads data from the set of Ruby modules included in the TZInfo::Data library (tzinfo-data gem).

To have TZInfo use this DataSource, call TZInfo::DataSource.set as follows:

TZInfo::DataSource.set(:ruby)

Constant Summary collapse

REQUIRE_PATH =

Base path for require.

File.join('tzinfo', 'data', 'definitions')
@@timezone_index_loaded =

Whether the timezone index has been loaded yet.

false
@@country_index_loaded =

Whether the country index has been loaded yet.

false

Instance Method Summary collapse

Methods inherited from DataSource

get, #inspect, set

Instance Method Details

#country_codesObject

Returns an array of all the available ISO 3166-1 alpha-2 country codes.



78
79
80
81
# File 'lib/tzinfo/ruby_data_source.rb', line 78

def country_codes
  load_country_index
  Data::Indexes::Countries.countries.keys.freeze
end

#data_timezone_identifiersObject

Returns an array of all the available timezone identifiers for data timezones (i.e. those that actually contain definitions).



54
55
56
57
# File 'lib/tzinfo/ruby_data_source.rb', line 54

def data_timezone_identifiers
  load_timezone_index
  Data::Indexes::Timezones.data_timezones
end

#linked_timezone_identifiersObject

Returns an array of all the available timezone identifiers that are links to other timezones.



61
62
63
64
# File 'lib/tzinfo/ruby_data_source.rb', line 61

def linked_timezone_identifiers
  load_timezone_index
  Data::Indexes::Timezones.linked_timezones
end

#load_country_info(code) ⇒ Object

Returns a CountryInfo instance for the given ISO 3166-1 alpha-2 country code. Raises InvalidCountryCode if the country could not be found or the code is invalid.

Raises:



69
70
71
72
73
74
# File 'lib/tzinfo/ruby_data_source.rb', line 69

def load_country_info(code)
  load_country_index
  info = Data::Indexes::Countries.countries[code]
  raise InvalidCountryCode, 'Invalid country code' unless info
  info
end

#load_timezone_info(identifier) ⇒ Object

Returns a TimezoneInfo instance for a given identifier. Raises InvalidTimezoneIdentifier if the timezone is not found or the identifier is invalid.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tzinfo/ruby_data_source.rb', line 21

def load_timezone_info(identifier)
  raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /^[A-Za-z0-9\+\-_]+(\/[A-Za-z0-9\+\-_]+)*$/
  
  identifier = identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__')
  
  # Untaint identifier after it has been reassigned to a new string. We
  # don't want to modify the original identifier. identifier may also be 
  # frozen and therefore cannot be untainted.
  identifier.untaint
  
  identifier = identifier.split('/')
  begin
    require_definition(identifier)
    
    m = Data::Definitions
    identifier.each {|part|
      m = m.const_get(part)
    }
    
    m.get
  rescue LoadError, NameError => e
    raise InvalidTimezoneIdentifier, e.message
  end
end

#timezone_identifiersObject

Returns an array of all the available timezone identifiers.



47
48
49
50
# File 'lib/tzinfo/ruby_data_source.rb', line 47

def timezone_identifiers
  load_timezone_index
  Data::Indexes::Timezones.timezones
end

#to_sObject

Returns the name of this DataSource.



84
85
86
# File 'lib/tzinfo/ruby_data_source.rb', line 84

def to_s
  "Ruby DataSource"
end