Module: Carmen
- Defined in:
- lib/carmen.rb,
lib/carmen/railtie.rb,
lib/carmen/version.rb
Defined Under Namespace
Classes: NonexistentCountry, Railtie, StatesNotSupported, UnavailableLocale
Constant Summary collapse
- VERSION =
'0.2.14'
Class Attribute Summary collapse
-
.default_country ⇒ Object
Returns the value of attribute default_country.
-
.default_locale ⇒ Object
Returns the value of attribute default_locale.
-
.excluded_countries ⇒ Object
Returns the value of attribute excluded_countries.
-
.excluded_states ⇒ Object
Returns the value of attribute excluded_states.
-
.priority_countries ⇒ Object
Returns the value of attribute priority_countries.
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, optionally using the specified locale.
-
.country_names(options = {}) ⇒ Object
Returns an array of all country names, optionally using the specified locale.
-
.state_code(state_name, country_code = Carmen.default_country, options = {}) ⇒ 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, options = {}) ⇒ Object
Returns the state name corresponding to the supplied state code within the default country Carmen::state_code(‘New Hampshire’) => ‘NH’.
-
.state_names(country_code = Carmen.default_country, options = {}) ⇒ Object
Returns an array of state names within the default code Carmen::state_names(‘US’) => [‘Alabama’, ‘Arkansas’, … ].
-
.states(country_code = Carmen.default_country, options = {}) ⇒ Object
Returns an array structure of state names and codes within the specified country code, or within the default country if none is provided.
-
.states?(country_code, options = {}) ⇒ 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.
21 22 23 |
# File 'lib/carmen.rb', line 21 def default_country @default_country end |
.default_locale ⇒ Object
Returns the value of attribute default_locale.
21 22 23 |
# File 'lib/carmen.rb', line 21 def default_locale @default_locale end |
.excluded_countries ⇒ Object
Returns the value of attribute excluded_countries.
21 22 23 |
# File 'lib/carmen.rb', line 21 def excluded_countries @excluded_countries end |
.excluded_states ⇒ Object
Returns the value of attribute excluded_states.
21 22 23 |
# File 'lib/carmen.rb', line 21 def excluded_states @excluded_states end |
.priority_countries ⇒ Object
Returns the value of attribute priority_countries.
21 22 23 |
# File 'lib/carmen.rb', line 21 def priority_countries @priority_countries end |
Class Method Details
.countries(options = {}) ⇒ Object
Returns a list of all countries
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/carmen.rb', line 47 def self.countries(={}) # Use specified locale or fall back to default locale locale = (.delete(:locale) || @default_locale).to_s # 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) localized_data = File.join(@data_path, "countries", "#{@default_locale}.yml") # raise(UnavailableLocale, "Could not load countries for '#{locale}' locale") end # As the data exists, load it @countries[locale] = YAML.load_file(localized_data) end # Return data after filtering excluded countries and prepending prepended countries result = @countries[locale].reject { |c| excluded_countries.include?( c[1] ) } priority_countries.map { |code| [ search_collection(result, code, 1, 0), code ] } + result end |
.country_code(country_name, options = {}) ⇒ Object
Returns the country code corresponding to the supplied country name
Carmen::country_code('Canada') => 'CA'
80 81 82 |
# File 'lib/carmen.rb', line 80 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', ... ]
86 87 88 |
# File 'lib/carmen.rb', line 86 def self.country_codes countries.map {|c| c[1] } end |
.country_name(country_code, options = {}) ⇒ Object
74 75 76 |
# File 'lib/carmen.rb', line 74 def self.country_name(country_code, ={}) search_collection(countries(), country_code, 1, 0) end |
.country_names(options = {}) ⇒ Object
Returns an array of all country names, optionally using the specified locale.
Carmen::country_names => ['Afghanistan', 'Aland Islands', 'Albania', ... ]
Carmen::country_names(:locale => :de) => ['Afghanistan', 'Åland', 'Albanien', ... ]
93 94 95 |
# File 'lib/carmen.rb', line 93 def self.country_names(={}) countries().map {|c| c[0] } end |
.state_code(state_name, country_code = Carmen.default_country, options = {}) ⇒ Object
Returns the state code corresponding to the supplied state name within the specified country
Carmen::state_code('IL', 'US') => Illinois
105 106 107 |
# File 'lib/carmen.rb', line 105 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', ... ]
117 118 119 |
# File 'lib/carmen.rb', line 117 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, options = {}) ⇒ Object
Returns the state name corresponding to the supplied state code within the default country
Carmen::state_code('New Hampshire') => 'NH'
99 100 101 |
# File 'lib/carmen.rb', line 99 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, options = {}) ⇒ Object
Returns an array of state names within the default code
Carmen::state_names('US') => ['Alabama', 'Arkansas', ... ]
111 112 113 |
# File 'lib/carmen.rb', line 111 def self.state_names(country_code = Carmen.default_country, ={}) self.states(country_code, ).map{|name, code| name} end |
.states(country_code = Carmen.default_country, options = {}) ⇒ Object
Returns an array structure of state names and codes within the specified country code, or within the default country if none is provided.
Carmen::states('US') => [['Alabama', 'AL'], ['Arkansas', 'AR'], ... ]
Carmen::states => [['Alabama', 'AL'], ['Arkansas', 'AR'], ... ]
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/carmen.rb', line 125 def self.states(country_code = Carmen.default_country, ={}) raise NonexistentCountry.new("Country not found for code #{country_code}") unless country_codes.include?(country_code) results = if states?(country_code) search_collection(@states, country_code, 0, 1) else [] end if excluded_states[country_code] results.reject { |s| excluded_states[country_code].include?(s[1]) } else results end end |
.states?(country_code, options = {}) ⇒ Boolean
143 144 145 146 147 |
# File 'lib/carmen.rb', line 143 def self.states?(country_code, ={}) @states.any? do |array| k,v = array k == country_code end end |