Browse Source

修改為臺灣地區資料

motephyr 10 years ago
parent
commit
faf10bc66b
5 changed files with 30 additions and 68 deletions
  1. 1 12
      README.md
  2. 9 16
      app/views/taiwan_city/data/index.html.erb
  3. 0 1
      db/areas.json
  4. 8 24
      lib/taiwan_city.rb
  5. 12 15
      spec/lib/taiwan_city_spec.rb

+ 1 - 12
README.md

@@ -47,24 +47,13 @@
 ## 贡献
 
 ```bash
-git clone git@github.com:saberma/taiwan_city.git
+git clone https://github.com/motephyr/taiwan_city.git
 cd taiwan_city
 rake appraisal:install
 cd spec/dummy
 rails server # http://localhost:3000/taiwan_city
 ```
 
-## 测试
-
-```bash
-rvm use 2.0.0
-rake appraisal:rails4 spec
-rake appraisal:rails32 spec
-rvm use 1.9.3
-bundle install
-rake appraisal:rails31 spec
-```
-
 ## 感謝作者
 
 * [saberma](https://github.com/saberma)

+ 9 - 16
app/views/taiwan_city/data/index.html.erb

@@ -1,28 +1,21 @@
 <%-
-  province = '440000'
-  city = '440300'
-  district = '440305'
-  provinces  = TaiwanCity.list
-  cities     = TaiwanCity.list(province)
+  city = '01000'
+  district = '01100'
+  cities     = TaiwanCity.list
   districtes = TaiwanCity.list(city)
 %>
 <div class='rails-helper city-group'>
-  <%= select_tag :province, options_for_select(provinces, province) , prompt: '--省份--', class: ['city-select', 'city-province'] %>
-  <%= select_tag :city    , options_for_select(cities, city)        , prompt: '--城市--', class: ['city-select', 'city-city'] %>
-  <%= select_tag :district, options_for_select(districtes, district), prompt: '--地区--', class: ['city-select', 'city-district'] %>
+  <%= select_tag :city    , options_for_select(cities, city)        , prompt: '--縣市--', class: ['city-select', 'city-city'] %>
+  <%= select_tag :district, options_for_select(districtes, district), prompt: '--鄉鎮市區--', class: ['city-select', 'city-district'] %>
 </div>
 
 <div class='html-tag city-group'>
-  <select class='city-select city-province'>
-    <option>--省份--</option>
-    <%= options_for_select(TaiwanCity.list, province) %>
-  </select>
   <select class='city-select city-city'>
-    <option>--市--</option>
-    <%= options_for_select(cities, city) %>
+    <option>--縣市--</option>
+    <%= options_for_select(TaiwanCity.list, city) %>
   </select>
   <select class='city-select city-district'>
-    <option>--地区--</option>
+    <option>--鄉鎮市區--</option>
     <%= options_for_select(districtes, district) %>
   </select>
-  </div>
+</div>

File diff suppressed because it is too large
+ 0 - 1
db/areas.json


+ 8 - 24
lib/taiwan_city.rb

@@ -2,17 +2,15 @@
 require "taiwan_city/engine"
 
 module TaiwanCity
-  TAIWAN = '000' # 全国
-  PATTERN = /(\d{2})(\d{2})(\d{2})/
+  TAIWAN = '00000' # 全国
+  PATTERN = /(\d{2})(\d{3})/
 
   class << self
-    def list(parent_id = '000')
+    def list(parent_id = '00000')
       result = []
       return result if parent_id.blank?
-      province_id = province(parent_id)
       city_id = city(parent_id)
       children = data
-      children = children[province_id][:children] if children.has_key?(province_id)
       children = children[city_id][:children] if children.has_key?(city_id)
       children.each_key do |id|
         result.push [ children[id][:text], id]
@@ -29,23 +27,15 @@ module TaiwanCity
       prepend_parent = options[:prepend_parent] || false
       children = data
       return children[id][:text] if children.has_key?(id)
-      province_id = province(id)
-      province_text = children[province_id][:text]
-      children = children[province_id][:children]
-      return "#{prepend_parent ? province_text : ''}#{children[id][:text]}" if children.has_key?(id)
       city_id = city(id)
       city_text = children[city_id][:text]
       children = children[city_id][:children]
-      return "#{prepend_parent ? (province_text + city_text) : ''}#{children[id][:text]}"
-    end
-
-    def province(code)
-      match(code)[1].ljust(6, '0')
+      return "#{prepend_parent ? (city_text) : ''}#{children[id][:text]}"
     end
 
     def city(code)
       id_match = match(code)
-      "#{id_match[1]}#{id_match[2]}".ljust(6, '0')
+      "#{id_match[1]}#{id_match[2]}".ljust(5, '0')
     end
 
     private
@@ -74,18 +64,12 @@ module TaiwanCity
         districts.each do |district|
           id = district['id']
           text = district['text']
-          if id.end_with?('0000')
+          if id.end_with?('000')
             @list[id] =  {:text => text, :children => {}}
-          elsif id.end_with?('00')
-            province_id = province(id)
-            @list[province_id] = {:text => nil, :children => {}} unless @list.has_key?(province_id)
-            @list[province_id][:children][id] = {:text => text, :children => {}}
           else
-            province_id = province(id)
             city_id = city(id)
-            @list[province_id] = {:text => text, :children => {}} unless @list.has_key?(province_id)
-            @list[province_id][:children][city_id] = {:text => text, :children => {}} unless @list[province_id][:children].has_key?(city_id)
-            @list[province_id][:children][city_id][:children][id] = {:text => text}
+            @list[city_id] = {:text => text, :children => {}} unless @list.has_key?(city_id)
+            @list[city_id][:children][id] = {:text => text}
           end
         end
       end

+ 12 - 15
spec/lib/taiwan_city_spec.rb

@@ -4,30 +4,27 @@ require 'spec_helper'
 describe TaiwanCity do
   it 'should be list' do
     # 省
-    TaiwanCity.list.should eql [["北京市", "110000"], ["天津市", "120000"], ["河北省", "130000"], ["山西省", "140000"], ["内蒙古自治区", "150000"], ["辽宁省", "210000"], ["吉林省", "220000"], ["黑龙江省", "230000"], ["上海市", "310000"], ["江苏省", "320000"], ["浙江省", "330000"], ["安徽省", "340000"], ["福建省", "350000"], ["江西省", "360000"], ["山东省", "370000"], ["河南省", "410000"], ["湖北省", "420000"], ["湖南省", "430000"], ["广东省", "440000"], ["广西壮族自治区", "450000"], ["海南省", "460000"], ["重庆市", "500000"], ["四川省", "510000"], ["贵州省", "520000"], ["云南省", "530000"], ["西藏自治区", "540000"], ["陕西省", "610000"], ["甘肃省", "620000"], ["青海省", "630000"], ["宁夏回族自治区", "640000"], ["新疆维吾尔自治区", "650000"]]
+    #TaiwanCity.list.should eql [["北京市", "110000"], ["天津市", "120000"], ["河北省", "130000"], ["山西省", "140000"], ["内蒙古自治区", "150000"], ["辽宁省", "210000"], ["吉林省", "220000"], ["黑龙江省", "230000"], ["上海市", "310000"], ["江苏省", "320000"], ["浙江省", "330000"], ["安徽省", "340000"], ["福建省", "350000"], ["江西省", "360000"], ["山东省", "370000"], ["河南省", "410000"], ["湖北省", "420000"], ["湖南省", "430000"], ["广东省", "440000"], ["广西壮族自治区", "450000"], ["海南省", "460000"], ["重庆市", "500000"], ["四川省", "510000"], ["贵州省", "520000"], ["云南省", "530000"], ["西藏自治区", "540000"], ["陕西省", "610000"], ["甘肃省", "620000"], ["青海省", "630000"], ["宁夏回族自治区", "640000"], ["新疆维吾尔自治区", "650000"]]
 
     #市
-    TaiwanCity.list('440000').should eql [["广州市", "440100"], ["韶关市", "440200"], ["深圳市", "440300"], ["珠海市", "440400"], ["汕头市", "440500"], ["佛山市", "440600"], ["江门市", "440700"], ["湛江市", "440800"], ["茂名市", "440900"], ["肇庆市", "441200"], ["惠州市", "441300"], ["梅州市", "441400"], ["汕尾市", "441500"], ["河源市", "441600"], ["阳江市", "441700"], ["清远市", "441800"], ["东莞市", "441900"], ["中山市", "442000"], ["潮州市", "445100"], ["揭阳市", "445200"], ["云浮市", "445300"]]
+    #TaiwanCity.list('440000').should eql [["广州市", "440100"], ["韶关市", "440200"], ["深圳市", "440300"], ["珠海市", "440400"], ["汕头市", "440500"], ["佛山市", "440600"], ["江门市", "440700"], ["湛江市", "440800"], ["茂名市", "440900"], ["肇庆市", "441200"], ["惠州市", "441300"], ["梅州市", "441400"], ["汕尾市", "441500"], ["河源市", "441600"], ["阳江市", "441700"], ["清远市", "441800"], ["东莞市", "441900"], ["中山市", "442000"], ["潮州市", "445100"], ["揭阳市", "445200"], ["云浮市", "445300"]]
 
     #区
-    TaiwanCity.list('440300').should eql [["市辖区", "440301"], ["罗湖区", "440303"], ["福田区", "440304"], ["南山区", "440305"], ["宝安区", "440306"], ["龙岗区", "440307"], ["盐田区", "440308"]]
+    #TaiwanCity.list('440300').should eql [["市辖区", "440301"], ["罗湖区", "440303"], ["福田区", "440304"], ["南山区", "440305"], ["宝安区", "440306"], ["龙岗区", "440307"], ["盐田区", "440308"]]
   end
 
   it 'should be get' do
-    TaiwanCity.get('440000').should eql '广东省'
-    TaiwanCity.get('440300').should eql '深圳市'
-    TaiwanCity.get('440305').should eql '南山区'
-    TaiwanCity.get('440000', prepend_parent: true).should eql '广东省'
-    TaiwanCity.get('440300', prepend_parent: true).should eql '广东省深圳市'
-    TaiwanCity.get('440305', prepend_parent: true).should eql '广东省深圳市南山区'
+    # TaiwanCity.get('440000').should eql '广东省'
+    # TaiwanCity.get('440300').should eql '深圳市'
+    # TaiwanCity.get('440305').should eql '南山区'
+    # TaiwanCity.get('440000', prepend_parent: true).should eql '广东省'
+    # TaiwanCity.get('440300', prepend_parent: true).should eql '广东省深圳市'
+    # TaiwanCity.get('440305', prepend_parent: true).should eql '广东省深圳市南山区'
   end
 
   it 'should be parse' do # 可以直接获取省、市
-    TaiwanCity.province('440000').should eql '440000' # 省
-    TaiwanCity.city('440000').should eql '440000'
-    TaiwanCity.province('440300').should eql '440000' # 市
-    TaiwanCity.city('440300').should eql '440300'
-    TaiwanCity.province('440305').should eql '440000' # 区
-    TaiwanCity.city('440305').should eql '440300'
+    # TaiwanCity.city('440000').should eql '440000'
+    # TaiwanCity.city('440300').should eql '440300'
+    # TaiwanCity.city('440305').should eql '440300'
   end
 end

Some files were not shown because too many files changed in this diff