Module: CountryCodes

Defined in:
lib/country_codes.rb,
lib/country_codes/country.rb,
lib/country_codes/version.rb,
lib/country_codes/lookup_table.rb,
lib/country_codes/internal/helpers.rb

Defined Under Namespace

Modules: Internal Classes: Country, LookupTable

Constant Summary collapse

SEARCHABLE_FIELDS =

A list of allowed search fields (see instance).

%i[officialName otherNames nationality sovereignty alpha2 alpha3].freeze
VERSION =
'0.1.8'
LIB_VERSION =
'0.0.13'

Class Method Summary collapse

Class Method Details

.find(term, locale: 'en', search_fields: %i[officialName otherNames nationality])) ⇒ Country?

Searches for a country that matches a given term. If nothing was found, returns nil. Check instance for the description of named parameters.



32
33
34
# File 'lib/country_codes.rb', line 32

def self.find(term, locale: 'en', search_fields: %i[officialName otherNames nationality])
  instance(locale: locale, search_fields: search_fields).find(term)
end

.instance(locale: 'en', search_fields: %i[officialName otherNames nationality])) ⇒ LookupTable

Creates an instance of LookupTable to be used for querying.



16
17
18
19
20
21
22
# File 'lib/country_codes.rb', line 16

def self.instance(locale: 'en', search_fields: %i[officialName otherNames nationality])
  unless (search_fields - SEARCHABLE_FIELDS).empty?
    raise ArgumentError, "Unknown search fields: #{search_fields - SEARCHABLE_FIELDS}"
  end

  CountryCodes::LookupTable.new(locale, search_fields.map(&:to_s))
end