Browse Source

config rspec.

saberma 10 years ago
parent
commit
8cc5a049e2

+ 2 - 0
.rspec

@@ -0,0 +1,2 @@
+--color
+--format progress

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


+ 95 - 0
lib/china_city.rb

@@ -1,4 +1,99 @@
+# encoding: utf-8
 require "china_city/engine"
 
 module ChinaCity
+  CHINA = '000000' # 全国
+  PATTERN = /(\d{2})(\d{2})(\d{2})/
+
+  class << self
+    def list(parent_id = '000000')
+      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]
+      end
+
+      #sort
+      result.sort! {|a, b| a[1] <=> b[1]}
+      result
+    end
+
+    # @options[:prepend_parent] 是否显示上级区域
+    def get(id, options = {})
+      return '' if id.blank?
+      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')
+    end
+
+    def city(code)
+      id_match = match(code)
+      "#{id_match[1]}#{id_match[2]}".ljust(6, '0')
+    end
+
+    private
+    def data
+      unless @list
+        #{ '440000' =>
+        #  {
+        #    :text => '广东',
+        #    :children =>
+        #      {
+        #        '440300' =>
+        #          {
+        #            :text => '深圳',
+        #            :children =>
+        #              {
+        #                '440305' => { :text => '南山' }
+        #              }
+        #           }
+        #       }
+        #   }
+        # }
+        @list = {}
+        #@see: http://github.com/RobinQu/LocationSelect-Plugin/raw/master/areas_1.0.json
+        json = JSON.parse(File.read("#{Engine.root}/db/areas.json"))
+        districts = json.values.flatten
+        districts.each do |district|
+          id = district['id']
+          text = district['text']
+          if id.end_with?('0000')
+            @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}
+          end
+        end
+      end
+      @list
+    end
+
+    def match(code)
+      code.match(PATTERN)
+    end
+  end
 end

+ 2 - 2
spec/dummy/config/environments/development.rb

@@ -14,13 +14,13 @@ Dummy::Application.configure do
   config.action_controller.perform_caching = false
 
   # Don't care if the mailer can't send.
-  config.action_mailer.raise_delivery_errors = false
+  # config.action_mailer.raise_delivery_errors = false
 
   # Print deprecation notices to the Rails logger.
   config.active_support.deprecation = :log
 
   # Raise an error on page load if there are pending migrations
-  config.active_record.migration_error = :page_load
+  # config.active_record.migration_error = :page_load
 
   # Debug mode disables concatenation and preprocessing of assets.
   # This option may cause significant delays in view rendering with a large

+ 1 - 1
spec/dummy/config/environments/test.rb

@@ -29,7 +29,7 @@ Dummy::Application.configure do
   # Tell Action Mailer not to deliver emails to the real world.
   # The :test delivery method accumulates sent emails in the
   # ActionMailer::Base.deliveries array.
-  config.action_mailer.delivery_method = :test
+  # config.action_mailer.delivery_method = :test
 
   # Print deprecation notices to the stderr.
   config.active_support.deprecation = :stderr

+ 33 - 0
spec/lib/china_city_spec.rb

@@ -0,0 +1,33 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe ChinaCity do
+  it 'should be list' do
+    # 省
+    ChinaCity.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"]]
+
+    #市
+    ChinaCity.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"]]
+
+    #区
+    ChinaCity.list('440300').should eql [["市辖区", "440301"], ["罗湖区", "440303"], ["福田区", "440304"], ["南山区", "440305"], ["宝安区", "440306"], ["龙岗区", "440307"], ["盐田区", "440308"]]
+  end
+
+  it 'should be get' do
+    ChinaCity.get('440000').should eql '广东省'
+    ChinaCity.get('440300').should eql '深圳市'
+    ChinaCity.get('440305').should eql '南山区'
+    ChinaCity.get('440000', prepend_parent: true).should eql '广东省'
+    ChinaCity.get('440300', prepend_parent: true).should eql '广东省深圳市'
+    ChinaCity.get('440305', prepend_parent: true).should eql '广东省深圳市南山区'
+  end
+
+  it 'should be parse' do # 可以直接获取省、市
+    ChinaCity.province('440000').should eql '440000' # 省
+    ChinaCity.city('440000').should eql '440000'
+    ChinaCity.province('440300').should eql '440000' # 市
+    ChinaCity.city('440300').should eql '440300'
+    ChinaCity.province('440305').should eql '440000' # 区
+    ChinaCity.city('440305').should eql '440300'
+  end
+end

+ 22 - 0
spec/spec_helper.rb

@@ -0,0 +1,22 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+ENV["RAILS_ENV"] = "test"
+require File.expand_path("../dummy/config/environment.rb",  __FILE__)
+
+require "rspec/rails"
+
+RSpec.configure do |config|
+  config.treat_symbols_as_metadata_keys_with_true_values = true
+  config.run_all_when_everything_filtered = true
+  config.filter_run :focus
+
+  # Run specs in random order to surface order dependencies. If you find an
+  # order dependency and want to debug it, you can fix the order by providing
+  # the seed, which is printed after each run.
+  #     --seed 1234
+  config.order = 'random'
+end

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