Class: TZInfo::Country
- Inherits:
-
Object
- Object
- TZInfo::Country
- Includes:
- Comparable
- Defined in:
- lib/tzinfo/country.rb
Overview
An ISO 3166 country. Can be used to get a list of Timezones for a country. For example:
us = Country.get('US')
us.zone_identifiers
us.zones
us.zone_info
Class Method Summary collapse
-
._load(data) ⇒ Object
Loads a marshalled Country.
-
.all ⇒ Object
Returns an Array of all the defined Countries.
-
.all_codes ⇒ Object
Returns an Array of all the valid country codes.
-
.get(identifier) ⇒ Object
Gets a Country by its ISO 3166 code.
-
.new(identifier) ⇒ Object
If identifier is a CountryInfo object, initializes the Country instance, otherwise calls get(identifier).
Instance Method Summary collapse
-
#<=>(c) ⇒ Object
Compare two Countries based on their code.
-
#_dump(limit) ⇒ Object
Dumps this Country for marshalling.
-
#code ⇒ Object
The ISO 3166 country code.
-
#eql?(c) ⇒ Boolean
Returns true if and only if the code of c is equal to the code of this Country.
-
#hash ⇒ Object
Returns a hash value for this Country.
-
#inspect ⇒ Object
Returns internal object state as a programmer-readable string.
-
#name ⇒ Object
The name of the country.
-
#to_s ⇒ Object
Alias for name.
-
#zone_identifiers ⇒ Object
(also: #zone_names)
Returns a frozen array of all the zone identifiers for the country.
-
#zone_info ⇒ Object
Returns a frozen array of all the timezones for the for the country as CountryTimezone instances (containing extra information about each zone).
-
#zones ⇒ Object
An array of all the Timezones for this country.
Class Method Details
._load(data) ⇒ Object
Loads a marshalled Country.
161 162 163 |
# File 'lib/tzinfo/country.rb', line 161 def self._load(data) Country.get(data) end |
.all ⇒ Object
Returns an Array of all the defined Countries.
83 84 85 86 |
# File 'lib/tzinfo/country.rb', line 83 def self.all load_index Indexes::Countries.countries.keys.collect {|code| get(code)} end |
.all_codes ⇒ Object
Returns an Array of all the valid country codes.
77 78 79 80 |
# File 'lib/tzinfo/country.rb', line 77 def self.all_codes load_index Indexes::Countries.countries.keys end |
.get(identifier) ⇒ Object
Gets a Country by its ISO 3166 code. Raises an InvalidCountryCode exception if it couldn’t be found.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/tzinfo/country.rb', line 50 def self.get(identifier) instance = @@countries[identifier] unless instance load_index info = Indexes::Countries.countries[identifier] raise InvalidCountryCode.new, 'Invalid identifier' unless info instance = Country.new(info) @@countries[identifier] = instance end instance end |
.new(identifier) ⇒ Object
If identifier is a CountryInfo object, initializes the Country instance, otherwise calls get(identifier).
66 67 68 69 70 71 72 73 74 |
# File 'lib/tzinfo/country.rb', line 66 def self.new(identifier) if identifier.kind_of?(CountryInfo) instance = super() instance.send :setup, identifier instance else get(identifier) end end |
Instance Method Details
#<=>(c) ⇒ Object
Compare two Countries based on their code. Returns -1 if c is less than self, 0 if c is equal to self and +1 if c is greater than self.
140 141 142 |
# File 'lib/tzinfo/country.rb', line 140 def <=>(c) code <=> c.code end |
#_dump(limit) ⇒ Object
Dumps this Country for marshalling.
156 157 158 |
# File 'lib/tzinfo/country.rb', line 156 def _dump(limit) code end |
#code ⇒ Object
The ISO 3166 country code.
89 90 91 |
# File 'lib/tzinfo/country.rb', line 89 def code @info.code end |
#eql?(c) ⇒ Boolean
Returns true if and only if the code of c is equal to the code of this Country.
146 147 148 |
# File 'lib/tzinfo/country.rb', line 146 def eql?(c) self == c end |
#hash ⇒ Object
Returns a hash value for this Country.
151 152 153 |
# File 'lib/tzinfo/country.rb', line 151 def hash code.hash end |
#inspect ⇒ Object
Returns internal object state as a programmer-readable string.
104 105 106 |
# File 'lib/tzinfo/country.rb', line 104 def inspect "#<#{self.class}: #{@info.code}>" end |
#name ⇒ Object
The name of the country.
94 95 96 |
# File 'lib/tzinfo/country.rb', line 94 def name @info.name end |
#to_s ⇒ Object
Alias for name.
99 100 101 |
# File 'lib/tzinfo/country.rb', line 99 def to_s name end |
#zone_identifiers ⇒ Object Also known as: zone_names
Returns a frozen array of all the zone identifiers for the country. These are in an order that
(1) makes some geographical sense, and
(2) puts the most populous zones first, where that does not contradict (1).
112 113 114 |
# File 'lib/tzinfo/country.rb', line 112 def zone_identifiers @info.zone_identifiers end |
#zone_info ⇒ Object
Returns a frozen array of all the timezones for the for the country as CountryTimezone instances (containing extra information about each zone). These are in an order that
(1) makes some geographical sense, and
(2) puts the most populous zones first, where that does not contradict (1).
134 135 136 |
# File 'lib/tzinfo/country.rb', line 134 def zone_info @info.zones end |
#zones ⇒ Object
An array of all the Timezones for this country. Returns TimezoneProxy objects to avoid the overhead of loading Timezone definitions until a conversion is actually required. The Timezones are returned in an order that
(1) makes some geographical sense, and
(2) puts the most populous zones first, where that does not contradict (1).
123 124 125 126 127 |
# File 'lib/tzinfo/country.rb', line 123 def zones zone_identifiers.collect {|id| Timezone.get_proxy(id) } end |