Module: Carmen
- Defined in:
- lib/carmen.rb
Defined Under Namespace
Classes: NonexistentCountry, StatesNotSupported, UnavailableLocale
Constant Summary collapse
- STATES =
Dir[@data_path + '/states/*.yml'].map do |file_name| [File::basename(file_name, '.yml').upcase, YAML.load_file(file_name)] end
Class Attribute Summary collapse
-
.default_country ⇒ Object
Returns the value of attribute default_country.
-
.locale ⇒ Object
Returns the value of attribute locale.
Class Method Summary collapse
-
.countries(options = {}) ⇒ Object
Returns a list of all countries.
-
.country_code(country_name, options = {}) ⇒ Object
Returns the country code corresponding to the supplied country name Carmen::country_code(‘Canada’) => ‘CA’.
-
.country_codes ⇒ Object
Returns an array of all country codes Carmen::country_codes => [‘AF’, ‘AX’, ‘AL’, … ].
-
.country_name(country_code, options = {}) ⇒ Object
Returns the country name corresponding to the supplied country code Carmen::country_name(‘TV’) => ‘Tuvalu’.
-
.country_names ⇒ Object
Returns an array of all country codes Carmen::country_name => [‘Afghanistan’, ‘Aland Islands’, ‘Albania’, … ].
-
.state_code(state_name, country_code = Carmen.default_country) ⇒ Object
Returns the state code corresponding to the supplied state name within the specified country Carmen::state_code(‘IL’, ‘US’) => Illinois.
-
.state_codes(country_code = Carmen.default_country) ⇒ Object
Returns an array of state codes within the specified country code Carmen::state_codes(‘US’) => [‘AL’, ‘AR’, … ].
-
.state_name(state_code, country_code = Carmen.default_country) ⇒ Object
Returns the state name corresponding to the supplied state code within the specified country Carmen::state_code(‘New Hampshire’) => ‘NH’.
-
.state_names(country_code = Carmen.default_country) ⇒ Object
Returns an array of state names within the specified country code Carmen::state_names(‘US’) => [‘Alabama’, ‘Arkansas’, … ].
-
.states(country_code = Carmen.default_country) ⇒ Object
Returns an array structure of state names and codes within the specified country code Carmen::states(‘US’) => [[‘Alabama’, ‘AL’], [‘Arkansas’, ‘AR’], … ].
-
.states?(country_code) ⇒ Boolean
Returns whether states are supported for the given country code Carmen::states?(‘US’) => true Carmen::states?(‘ZZ’) => false.
Class Attribute Details
.default_country ⇒ Object
Returns the value of attribute default_country.
11 12 13 |
# File 'lib/carmen.rb', line 11 def default_country @default_country end |
.locale ⇒ Object
Returns the value of attribute locale.
11 12 13 |
# File 'lib/carmen.rb', line 11 def locale @locale end |
Class Method Details
.countries(options = {}) ⇒ Object
Returns a list of all countries
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/carmen.rb', line 33 def self.countries(={}) # Use specified locale or fall back to default locale locale = .delete(:locale) || @locale # Load the country list for the specified locale @countries ||= {} unless @countries[locale] # Check if data in the specified locale is available localized_data = File.join(@data_path, "countries.#{locale}.yml") unless File.exists?(localized_data) raise(UnavailableLocale, "Could not load countries for '#{locale}' locale") end # As the data exists, load it @countries[locale] ||= YAML.load_file(File.join(@data_path, "countries.#{locale}.yml")) end # Return data @countries[locale] end |
.country_code(country_name, options = {}) ⇒ Object
Returns the country code corresponding to the supplied country name
Carmen::country_code('Canada') => 'CA'
62 63 64 |
# File 'lib/carmen.rb', line 62 def self.country_code(country_name, ={}) search_collection(countries(), country_name, 0, 1) end |
.country_codes ⇒ Object
Returns an array of all country codes
Carmen::country_codes => ['AF', 'AX', 'AL', ... ]
68 69 70 |
# File 'lib/carmen.rb', line 68 def self.country_codes countries.map {|c| c[1] } end |
.country_name(country_code, options = {}) ⇒ Object
Returns the country name corresponding to the supplied country code
Carmen::country_name('TV') => 'Tuvalu'
56 57 58 |
# File 'lib/carmen.rb', line 56 def self.country_name(country_code, ={}) search_collection(countries(), country_code, 1, 0) end |
.country_names ⇒ Object
Returns an array of all country codes
Carmen::country_name => ['Afghanistan', 'Aland Islands', 'Albania', ... ]
74 75 76 |
# File 'lib/carmen.rb', line 74 def self.country_names countries.map {|c| c[0] } end |
.state_code(state_name, country_code = Carmen.default_country) ⇒ Object
Returns the state code corresponding to the supplied state name within the specified country
Carmen::state_code('IL', 'US') => Illinois
86 87 88 |
# File 'lib/carmen.rb', line 86 def self.state_code(state_name, country_code = Carmen.default_country) search_collection(self.states(country_code), state_name, 0, 1) end |
.state_codes(country_code = Carmen.default_country) ⇒ Object
Returns an array of state codes within the specified country code
Carmen::state_codes('US') => ['AL', 'AR', ... ]
98 99 100 |
# File 'lib/carmen.rb', line 98 def self.state_codes(country_code = Carmen.default_country) self.states(country_code).map{|name, code| code} end |
.state_name(state_code, country_code = Carmen.default_country) ⇒ Object
Returns the state name corresponding to the supplied state code within the specified country
Carmen::state_code('New Hampshire') => 'NH'
80 81 82 |
# File 'lib/carmen.rb', line 80 def self.state_name(state_code, country_code = Carmen.default_country) search_collection(self.states(country_code), state_code, 1, 0) end |
.state_names(country_code = Carmen.default_country) ⇒ Object
Returns an array of state names within the specified country code
Carmen::state_names('US') => ['Alabama', 'Arkansas', ... ]
92 93 94 |
# File 'lib/carmen.rb', line 92 def self.state_names(country_code = Carmen.default_country) self.states(country_code).map{|name, code| name} end |
.states(country_code = Carmen.default_country) ⇒ Object
Returns an array structure of state names and codes within the specified country code
Carmen::states('US') => [['Alabama', 'AL'], ['Arkansas', 'AR'], ... ]
104 105 106 107 108 |
# File 'lib/carmen.rb', line 104 def self.states(country_code = Carmen.default_country) raise NonexistentCountry unless country_codes.include?(country_code) raise StatesNotSupported unless states?(country_code) search_collection(STATES, country_code, 0, 1) end |
.states?(country_code) ⇒ Boolean
113 114 115 116 117 |
# File 'lib/carmen.rb', line 113 def self.states?(country_code) STATES.any? do |array| k,v = array k == country_code end end |