Sha256: 474e1e6e157d71bf01e2cba3cddd504873afb73cfba4d0eccdc75aa5a3ce5a06

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

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(object)
      object.is_a?(Territory) ? object : data[object]
    end

    def self.data
      @data ||= {}
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uncharted-0.0.8 lib/uncharted/territory.rb