Skip to content

Commit

Permalink
add: country subdivision string type
Browse files Browse the repository at this point in the history
  • Loading branch information
danini-the-panini committed Nov 15, 2021
1 parent af5926f commit 4a03dbb
Show file tree
Hide file tree
Showing 4 changed files with 3,969 additions and 4 deletions.
26 changes: 24 additions & 2 deletions lib/kdl/types/country.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'kdl/types/country/iso3166_countries'
require 'kdl/types/country/iso3166_subdivisions'

module KDL
module Types
Expand All @@ -21,7 +22,6 @@ def self.call(value, type = 'country-3')

new(country, type: type)
end

end
Country3 = Country
MAPPING['country-3'] = Country3
Expand All @@ -42,6 +42,28 @@ def self.call(value, type = 'country-2')
end
MAPPING['country-2'] = Country2

# TODO: country-subdivision??
class CountrySubdivision < Value
attr_reader :country, :name

def initialize(value, type: 'country-subdivision', country:, name:, **kwargs)
super(value, type: type, **kwargs)
@country = country
@name = name
end

def self.call(value, type = 'country-subdivision')
return nil unless value.is_a? ::KDL::Value::String

country2 = value.value.split('-').first
country = Country::COUNTRIES2[country2.upcase]
raise ArgumentError, 'invalid country' unless country

subdivision = COUNTRY_SUBDIVISIONS.dig(country2.upcase, value.value)
raise ArgumentError, 'invalid country subdivision' unless subdivision

new(value.value, type: type, name: subdivision, country: country)
end
end
MAPPING['country-subdivision'] = CountrySubdivision
end
end
Loading

0 comments on commit 4a03dbb

Please sign in to comment.