taiwan_city.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # encoding: utf-8
  2. require "taiwan_city/engine"
  3. module TaiwanCity
  4. TAIWAN = '000' # 全国
  5. PATTERN = /(\d{2})(\d{2})(\d{2})/
  6. class << self
  7. def list(parent_id = '000')
  8. result = []
  9. return result if parent_id.blank?
  10. province_id = province(parent_id)
  11. city_id = city(parent_id)
  12. children = data
  13. children = children[province_id][:children] if children.has_key?(province_id)
  14. children = children[city_id][:children] if children.has_key?(city_id)
  15. children.each_key do |id|
  16. result.push [ children[id][:text], id]
  17. end
  18. #sort
  19. result.sort! {|a, b| a[1] <=> b[1]}
  20. result
  21. end
  22. # @options[:prepend_parent] 是否显示上级区域
  23. def get(id, options = {})
  24. return '' if id.blank?
  25. prepend_parent = options[:prepend_parent] || false
  26. children = data
  27. return children[id][:text] if children.has_key?(id)
  28. province_id = province(id)
  29. province_text = children[province_id][:text]
  30. children = children[province_id][:children]
  31. return "#{prepend_parent ? province_text : ''}#{children[id][:text]}" if children.has_key?(id)
  32. city_id = city(id)
  33. city_text = children[city_id][:text]
  34. children = children[city_id][:children]
  35. return "#{prepend_parent ? (province_text + city_text) : ''}#{children[id][:text]}"
  36. end
  37. def province(code)
  38. match(code)[1].ljust(6, '0')
  39. end
  40. def city(code)
  41. id_match = match(code)
  42. "#{id_match[1]}#{id_match[2]}".ljust(6, '0')
  43. end
  44. private
  45. def data
  46. unless @list
  47. #{ '440000' =>
  48. # {
  49. # :text => '广东',
  50. # :children =>
  51. # {
  52. # '440300' =>
  53. # {
  54. # :text => '深圳',
  55. # :children =>
  56. # {
  57. # '440305' => { :text => '南山' }
  58. # }
  59. # }
  60. # }
  61. # }
  62. # }
  63. @list = {}
  64. #@see: http://github.com/RobinQu/LocationSelect-Plugin/raw/master/areas_1.0.json
  65. json = JSON.parse(File.read("#{Engine.root}/db/areas.json"))
  66. districts = json.values.flatten
  67. districts.each do |district|
  68. id = district['id']
  69. text = district['text']
  70. if id.end_with?('0000')
  71. @list[id] = {:text => text, :children => {}}
  72. elsif id.end_with?('00')
  73. province_id = province(id)
  74. @list[province_id] = {:text => nil, :children => {}} unless @list.has_key?(province_id)
  75. @list[province_id][:children][id] = {:text => text, :children => {}}
  76. else
  77. province_id = province(id)
  78. city_id = city(id)
  79. @list[province_id] = {:text => text, :children => {}} unless @list.has_key?(province_id)
  80. @list[province_id][:children][city_id] = {:text => text, :children => {}} unless @list[province_id][:children].has_key?(city_id)
  81. @list[province_id][:children][city_id][:children][id] = {:text => text}
  82. end
  83. end
  84. end
  85. @list
  86. end
  87. def match(code)
  88. code.match(PATTERN)
  89. end
  90. end
  91. end