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>
:reek:DuplicateMethodCall.
- #subdivision_for_string?(subdivision_str) ⇒ Boolean
-
#subdivision_names(locale = :en) ⇒ Array<String>
:reek:FeatureEnvy.
-
#subdivision_names_with_codes(locale = :en) ⇒ Array<Array>
:reek:FeatureEnvy.
-
#subdivision_types ⇒ Array<String>
The list of subdivision types for this country.
-
#subdivisions ⇒ Array<ISO3166::Subdivision>
:reek:DuplicateMethodCall.
-
#subdivisions? ⇒ Boolean
trueif 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 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_types ⇒ Array<String>
:reek:DuplicateMethodCall
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
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
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
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_types ⇒ Array<String>
Returns 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 |
#subdivisions ⇒ Array<ISO3166::Subdivision>
:reek:DuplicateMethodCall
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.
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.
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 |