module TaiwanDistricts

Constants

PATTERN
TAIWAN
VERSION

Public Class Methods

city(code) click to toggle source
# File lib/taiwan_districts.rb, line 22
def city(code)
  match(code)[1].ljust(5, '0')
end
list(lang = 'zhtw', parent_id = '00000') click to toggle source
# File lib/taiwan_districts.rb, line 8
def list(lang = 'zhtw', parent_id = '00000')
  result = []
  return result if parent_id.blank?
  city_id = city(parent_id)
  children = data(lang)
  children = children[city_id][:children] if children.has_key?(city_id)
  children.each_key do |id|
    result.push [children[id][:text], id]
  end

  result.sort! { |a, b| a[1] <=> b[1] }
  result
end

Private Class Methods

data(lang) click to toggle source
# File lib/taiwan_districts.rb, line 28
def data(lang)
  @list_lang = 'zh-tw' unless @list_lang 
  if @list.nil? or @list_lang != lang
    @list_lang = lang
    @list = {}
    json = JSON.parse(File.read("#{Engine.root}/db/districts.json"))
    districts = json.values.flatten
    districts.each do |district|
      id = district['id']
      text = district["#{@list_lang}"]
      if id.end_with?('000')
        @list[id] =  {:text => text, :children => {}}
      else
        city_id = city(id)
        @list[city_id] = {:text => text, :children => {}} unless @list.has_key?(city_id)
        @list[city_id][:children][id] = {:text => text}
      end
    end
  end
  @list
end
match(code) click to toggle source
# File lib/taiwan_districts.rb, line 50
def match(code)
  code.match(PATTERN)
end