Class: TZInfo::RubyDataSource
- Inherits:
-
DataSource
- Object
- DataSource
- TZInfo::RubyDataSource
- 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
- @@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
-
#country_codes ⇒ Object
Returns an array of all the available ISO 3166-1 alpha-2 country codes.
-
#data_timezone_identifiers ⇒ Object
Returns an array of all the available timezone identifiers for data timezones (i.e. those that actually contain definitions).
-
#initialize ⇒ RubyDataSource
constructor
Initializes a new RubyDataSource instance.
-
#linked_timezone_identifiers ⇒ Object
Returns an array of all the available timezone identifiers that are links to other timezones.
-
#load_country_info(code) ⇒ Object
Returns a CountryInfo instance for the given ISO 3166-1 alpha-2 country code.
-
#load_timezone_info(identifier) ⇒ Object
Returns a TimezoneInfo instance for a given identifier.
-
#timezone_identifiers ⇒ Object
Returns an array of all the available timezone identifiers.
-
#to_s ⇒ Object
Returns the name of this DataSource.
Methods inherited from DataSource
Constructor Details
#initialize ⇒ RubyDataSource
Initializes a new RubyDataSource instance.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tzinfo/ruby_data_source.rb', line 16 def initialize tzinfo_data = File.join('tzinfo', 'data') begin require(tzinfo_data) 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 rescue LoadError @base_path = tzinfo_data end end |
Instance Method Details
#country_codes ⇒ Object
Returns an array of all the available ISO 3166-1 alpha-2 country codes.
93 94 95 96 |
# File 'lib/tzinfo/ruby_data_source.rb', line 93 def country_codes load_country_index Data::Indexes::Countries.countries.keys.freeze end |
#data_timezone_identifiers ⇒ Object
Returns an array of all the available timezone identifiers for data timezones (i.e. those that actually contain definitions).
69 70 71 72 |
# File 'lib/tzinfo/ruby_data_source.rb', line 69 def data_timezone_identifiers load_timezone_index Data::Indexes::Timezones.data_timezones end |
#linked_timezone_identifiers ⇒ Object
Returns an array of all the available timezone identifiers that are links to other timezones.
76 77 78 79 |
# File 'lib/tzinfo/ruby_data_source.rb', line 76 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.
84 85 86 87 88 89 |
# File 'lib/tzinfo/ruby_data_source.rb', line 84 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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tzinfo/ruby_data_source.rb', line 36 def load_timezone_info(identifier) raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /\A[A-Za-z0-9+\-_]+(\/[A-Za-z0-9+\-_]+)*\z/ 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. RubyCoreSupport.untaint(identifier) 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. end end |
#timezone_identifiers ⇒ Object
Returns an array of all the available timezone identifiers.
62 63 64 65 |
# File 'lib/tzinfo/ruby_data_source.rb', line 62 def timezone_identifiers load_timezone_index Data::Indexes::Timezones.timezones end |
#to_s ⇒ Object
Returns the name of this DataSource.
99 100 101 |
# File 'lib/tzinfo/ruby_data_source.rb', line 99 def to_s "Ruby DataSource" end |