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
12
13
# File 'lib/countries/country/country_subdivision_methods.rb', line 7

def find_subdivision_by_name(subdivision_str)
  matched_subdivisions = subdivisions.select do |key, value|
    subdivision_str == key || value.match?(subdivision_str)
  end.values

  matched_subdivisions.min_by { |subdivision| subdivision_types.index(subdivision.type) }
end

#humanized_subdivision_typesArray<String>

:reek:DuplicateMethodCall

Returns:

  • (Array<String>)

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



48
49
50
51
52
53
54
# File 'lib/countries/country/country_subdivision_methods.rb', line 48

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

#subdivision_for_string?(subdivision_str) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/countries/country/country_subdivision_methods.rb', line 15

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

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

:reek:FeatureEnvy

Parameters:

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

    The locale to use for translations.

Returns:

  • (Array<String>)

    A list of subdivision names for this country.



66
67
68
# File 'lib/countries/country/country_subdivision_methods.rb', line 66

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

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

:reek:FeatureEnvy

Parameters:

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

    The locale to use for translations.

Returns:

  • (Array<Array>)

    This Country’s subdivision pairs of names and codes.



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

def subdivision_names_with_codes(locale = :en)
  subdivisions.map { |key, value| [value.translations[locale] || value.name, key] }
end

#subdivision_typesArray<String>

Returns the list of subdivision types for this country.

Returns:

  • (Array<String>)

    the list of subdivision types for this country



42
43
44
# File 'lib/countries/country/country_subdivision_methods.rb', line 42

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

#subdivisionsArray<ISO3166::Subdivision>

:reek:DuplicateMethodCall

Returns:



27
28
29
30
31
32
33
# File 'lib/countries/country/country_subdivision_methods.rb', line 27

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)


21
22
23
# File 'lib/countries/country/country_subdivision_methods.rb', line 21

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:



37
38
39
# File 'lib/countries/country/country_subdivision_methods.rb', line 37

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