Module: ISO3166::CountrySubdivisionMethods

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

Instance Method Summary collapse

Instance Method Details

#find_subdivision_by_name(subdivision_str) ⇒ Subdivision

Returns The first subdivision matching the provided string.

Parameters:

  • subdivision_str (String)

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

Returns:

  • (Subdivision)

    The first subdivision matching the provided string



7
8
9
10
11
# File 'lib/countries/country/country_subdivision_methods.rb', line 7

def find_subdivision_by_name(subdivision_str)
  subdivisions.select do |k, v|
    subdivision_str == k || v.name == subdivision_str || v.translations.values.include?(subdivision_str)
  end.values.first
end

#humanized_subdivision_typesArray<String>

Returns the list of humanized subdivision types for this country. Uses ActiveSupport’s ‘#humanize` if available.

Returns:

  • (Array<String>)

    the list of humanized subdivision types for this country. Uses ActiveSupport’s ‘#humanize` if available



44
45
46
47
48
49
50
# File 'lib/countries/country/country_subdivision_methods.rb', line 44

def humanized_subdivision_types
  if String.instance_methods.include?(:humanize)
    subdivisions.map { |_k, v| v['type'].humanize.freeze }.uniq
  else
    subdivisions.map { |_k, v| humanize_string(v['type']) }.uniq
  end
end

#subdivision_for_string?(subdivision_str) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/countries/country/country_subdivision_methods.rb', line 13

def subdivision_for_string?(subdivision_str)
  subdivisions.transform_values(&:translations)
              .any? { |k, v| subdivision_str == k || v.values.include?(subdivision_str) }
end

#subdivision_names(locale = 'en') ⇒ Array<String>

Returns A list of subdivision names for this country.

Parameters:

  • locale (String) (defaults to: 'en')

    The locale to use for translations.

Returns:

  • (Array<String>)

    A list of subdivision names for this country.



60
61
62
# File 'lib/countries/country/country_subdivision_methods.rb', line 60

def subdivision_names(locale = 'en')
  subdivisions.map { |_k, v| v.translations[locale] || v.name }
end

#subdivision_names_with_codes(locale = 'en') ⇒ Array<Array>

Returns This Country’s subdivision pairs of names and codes.

Parameters:

  • locale (String) (defaults to: 'en')

    The locale to use for translations.

Returns:

  • (Array<Array>)

    This Country’s subdivision pairs of names and codes.



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

def subdivision_names_with_codes(locale = 'en')
  subdivisions.map { |k, v| [v.translations[locale] || v.name, k] }
end

#subdivision_typesArray<String>

Returns the list of subdivision types for this country.

Returns:

  • (Array<String>)

    the list of subdivision types for this country



39
40
41
# File 'lib/countries/country/country_subdivision_methods.rb', line 39

def subdivision_types
  subdivisions.map { |_k, v| v['type'] }.uniq
end

#subdivisionsArray<ISO3166::Subdivision>

Returns the list of subdivisions for this Country.

Returns:



24
25
26
27
28
29
30
# File 'lib/countries/country/country_subdivision_methods.rb', line 24

def subdivisions
  @subdivisions ||= if data['subdivisions']
                      ISO3166::Data.create_subdivisions(data['subdivisions'])
                    else
                      ISO3166::Data.subdivisions(alpha2)
                    end
end

#subdivisions?Boolean

true if this Country has any Subdivisions.

Returns:

  • (Boolean)


19
20
21
# File 'lib/countries/country/country_subdivision_methods.rb', line 19

def subdivisions?
  !subdivisions.empty?
end

#subdivisions_of_types(types) ⇒ Array<ISO3166::Subdivision>

Returns the list of subdivisions of the given type(s) for this Country.

Parameters:

  • types (Array<String>)

    The locale to use for translations.

Returns:



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

def subdivisions_of_types(types)
  subdivisions.select { |_k, v| types.include?(v.type) }
end