require 'uncharted/country' module Uncharted class Country def subdivisions @subdivisions ||= [] end def territories @territories ||= find_by_type(:territory) end def states @states ||= find_by_type(:state) end def districts @districts ||= find_by_type(:district) end def self.subdivisions Territory.data end def find_by_type(type) subdivisions.select {|t| t.type == type} end end class Territory attr_reader :abbr, :code, :country, :country_code, :name, :type def initialize(code, type, name) @code = code @type = type @name = name @country_code, @abbr = code.split('-') @country = Country.find(@country_code) Territory.data[code] = self @country.subdivisions << self if @country end def to_s @abbr end def self.find(code) data[code] end def self.data @data ||= {} end end end