Module: ISO3166::CountryClassMethods

Included in:
Country
Defined in:
lib/countries/country/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#all(&block) ⇒ Object Also known as: countries



34
35
36
37
# File 'lib/countries/country/class_methods.rb', line 34

def all(&block)
  block ||= proc { |_alpha2, data| ISO3166::Country.new(data) }
  ISO3166::Data.cache.map(&block)
end

#all_names_with_codes(locale = :en) ⇒ Object

:reek:UtilityFunction :reek:ManualDispatch



43
44
45
46
47
48
# File 'lib/countries/country/class_methods.rb', line 43

def all_names_with_codes(locale = :en)
  Country.all.map do |country|
    lc = country.translation(locale) || country.iso_short_name
    [lc.respond_to?('html_safe') ? lc.html_safe : lc, country.alpha2]
  end.sort
end

#all_translated(locale = :en) ⇒ Object



54
55
56
# File 'lib/countries/country/class_methods.rb', line 54

def all_translated(locale = :en)
  translations(locale).values
end

#codesObject

:reek:UtilityFunction



30
31
32
# File 'lib/countries/country/class_methods.rb', line 30

def codes
  ISO3166::Data.codes
end

#collect_countries_with(query_val, query_method = :alpha2, result_method = :itself) ⇒ Array

Returns An array of countries matching the provided query, or the result of applying ‘result_method` to the array of `Country` objects.

Parameters:

  • query_val (String)

    A value to query using ‘query_method`

  • query_method (Symbol) (defaults to: :alpha2)

    An optional query method, defaults to Country#alpha2

  • result_method (Symbol) (defaults to: :itself)

    An optional method of ‘Country` to apply to the result set.

Returns:

  • (Array)

    An array of countries matching the provided query, or the result of applying ‘result_method` to the array of `Country` objects



74
75
76
77
78
79
# File 'lib/countries/country/class_methods.rb', line 74

def collect_countries_with(query_val, query_method = :alpha2, result_method = :itself)
  return nil unless [query_method, result_method].map { |method| method_defined? method }.all?

  all.select { |country| country.send(query_method)&.include? query_val }
     .map { |country| country.send(result_method) }
end

#collect_likely_countries_by_subdivision_name(subdivision_str, result_method = :itself) ⇒ Array

Returns An array of countries with subdivisions matching the provided name, or the result of applying ‘result_method` to the array of `Country` objects.

Parameters:

  • subdivision_str (String)

    A subdivision name or code to search for. Search includes translated subdivision names.

  • result_method (Symbol) (defaults to: :itself)

    An optional method of ‘Country` to apply to the result set.

Returns:

  • (Array)

    An array of countries with subdivisions matching the provided name, or the result of applying ‘result_method` to the array of `Country` objects



84
85
86
87
88
89
# File 'lib/countries/country/class_methods.rb', line 84

def collect_likely_countries_by_subdivision_name(subdivision_str, result_method = :itself)
  return nil unless method_defined? result_method

  all.select { |country| country.subdivision_for_string?(subdivision_str) }
     .map { |country| country.send(result_method) }
end

#new(country_data) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/countries/country/class_methods.rb', line 20

def new(country_data)
  return super if country_data.is_a?(Hash)

  country_code = country_data.to_s
  country_code = country_code.upcase if country_code.match?(/[a-z]/)

  super if codes.include?(country_code)
end

#pluck(*attributes) ⇒ Object



50
51
52
# File 'lib/countries/country/class_methods.rb', line 50

def pluck(*attributes)
  all.map { |country| country.data.fetch_values(*attributes.map(&:to_s)) }
end

#translations(locale = :en) ⇒ Object

:reek:UtilityFunction :reek:FeatureEnvy



60
61
62
63
64
65
66
67
68
# File 'lib/countries/country/class_methods.rb', line 60

def translations(locale = :en)
  locale = locale.to_sym if locale.is_a?(String)
  locale = locale.downcase if locale.match?(/[A-Z]/)

  file_path = ISO3166::Data.datafile_path(%W[locales #{locale}.json])
  translations = JSON.parse(File.read(file_path))

  translations.merge(custom_countries_translations(locale))
end