Module: ISO3166::CountrySubdivisionMethods
- Included in:
- Country
- Defined in:
- lib/countries/country/country_subdivision_methods.rb
Instance Method Summary collapse
-
#find_subdivision_by_name(subdivision_str) ⇒ Subdivision
The first subdivision matching the provided string.
-
#humanized_subdivision_types ⇒ Array<String>
The list of humanized subdivision types for this country.
- #subdivision_for_string?(subdivision_str) ⇒ Boolean
-
#subdivision_names(locale = 'en') ⇒ Array<String>
A list of subdivision names for this country.
-
#subdivision_names_with_codes(locale = 'en') ⇒ Array<Array>
This Country’s subdivision pairs of names and codes.
-
#subdivision_types ⇒ Array<String>
The list of subdivision types for this country.
-
#subdivisions ⇒ Array<ISO3166::Subdivision>
The list of subdivisions for this Country.
-
#subdivisions? ⇒ Boolean
true
if this Country has any Subdivisions. -
#subdivisions_of_types(types) ⇒ Array<ISO3166::Subdivision>
The list of subdivisions of the given type(s) for this Country.
Instance Method Details
#find_subdivision_by_name(subdivision_str) ⇒ Subdivision
Returns 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_types ⇒ Array<String>
Returns 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
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.
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.
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_types ⇒ Array<String>
Returns 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 |
#subdivisions ⇒ Array<ISO3166::Subdivision>
Returns the list of subdivisions for this Country.
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.
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.
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 |