Sha256: 0ac094340d1b60fac5454684663a2413140b151c9d526d05c1ccee39d3ebb113

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

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, :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 name(options = {})
      I18n.t("territories.#{@country_code}#{@abbr}", {locale: options[:locale] || I18n.locale}, default: @name)
    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.10 lib/uncharted/territory.rb