class PSGC::Region

Constants

NOT_A_PROVINCE
REGION_DATA

Public Class Methods

[](id) click to toggle source
# File lib/psgc/region.rb, line 16
def [](id)
  case
  when id.kind_of?(Fixnum)
    map_by_num[id]
  when id.kind_of?(String)
    map_by_string[id]
  else
    raise "\"#{id}\" expected either String or Fixnum (was #{id.class})"
  end
end
all() click to toggle source
# File lib/psgc/region.rb, line 12
def all
  @@all ||= load_regions
end
new(*args) click to toggle source
Calls superclass method
# File lib/psgc/region.rb, line 5
def initialize(*args)
  super(*args)
end

Private Class Methods

load_regions() click to toggle source
# File lib/psgc/region.rb, line 29
def load_regions
  regions = CSV.open(REGION_DATA) do |csv|
    csv.shift # skip header row
    csv.read.map {|row| PSGC::Region.new(row[0], row[1])}
  end
end
map_by_num() click to toggle source
# File lib/psgc/region.rb, line 36
def map_by_num
  @@map_by_num ||= all.inject({}) {|hash, region| hash[region.id.to_i] = region; hash }
end
map_by_string() click to toggle source
# File lib/psgc/region.rb, line 40
def map_by_string
  @@map_by_string ||= all.inject({}) {|hash, region| hash[region.id] = region; hash }
end

Public Instance Methods

code() click to toggle source
# File lib/psgc/region.rb, line 45
def code
  "#{id}0000000"
end
provinces() click to toggle source
# File lib/psgc/region.rb, line 49
def provinces
  @provinces ||= load_provinces
end

Private Instance Methods

load_provinces() click to toggle source
# File lib/psgc/region.rb, line 61
def load_provinces
  CSV.open(province_data_path) do |csv|
    csv.shift # skip header row
    csv.read.map {|row| to_province_or_district(*row)}
  end
end
province_data_path() click to toggle source
# File lib/psgc/region.rb, line 57
def province_data_path
  File.join(PSGC::DATA_DIR, id, 'provinces.csv')
end
to_province_or_district(id, name) click to toggle source

TODO Move to ProvinceOrDistrict

# File lib/psgc/region.rb, line 69
def to_province_or_district(id, name)
  if name.end_with? NOT_A_PROVINCE
    PSGC::District.new id, name.chomp(NOT_A_PROVINCE)
  else
    PSGC::Province.new id, name
  end
end