district_cn 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +21 -0
 - data/.rspec +1 -0
 - data/Gemfile +4 -0
 - data/LICENSE.txt +22 -0
 - data/README.md +116 -0
 - data/Rakefile +6 -0
 - data/district_cn.gemspec +29 -0
 - data/lib/areas.json +6 -0
 - data/lib/district_cn/act_as_area_field.rb +24 -0
 - data/lib/district_cn/as_options.rb +46 -0
 - data/lib/district_cn/code.rb +107 -0
 - data/lib/district_cn/db.rb +81 -0
 - data/lib/district_cn/version.rb +3 -0
 - data/lib/district_cn.rb +40 -0
 - data/spec/act_as_area_field_spec.rb +67 -0
 - data/spec/code_spec.rb +104 -0
 - data/spec/db_spec.rb +32 -0
 - data/spec/district_cn_spec.rb +22 -0
 - data/spec/spec_helper.rb +10 -0
 - metadata +182 -0
 
    
        data/.gitignore
    ADDED
    
    | 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Ignore bundler config
         
     | 
| 
      
 2 
     | 
    
         
            +
            /.bundle
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            # Ignore the default SQLite database.
         
     | 
| 
      
 5 
     | 
    
         
            +
            *.sqlite3
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            # Ignore all logfiles and tempfiles.
         
     | 
| 
      
 8 
     | 
    
         
            +
            /log/*.log
         
     | 
| 
      
 9 
     | 
    
         
            +
            /tmp
         
     | 
| 
      
 10 
     | 
    
         
            +
            /public/assets
         
     | 
| 
      
 11 
     | 
    
         
            +
            /public/uploads
         
     | 
| 
      
 12 
     | 
    
         
            +
            /public/uploads/*.*
         
     | 
| 
      
 13 
     | 
    
         
            +
            /config/database.yml
         
     | 
| 
      
 14 
     | 
    
         
            +
            *.swp
         
     | 
| 
      
 15 
     | 
    
         
            +
            .DS_Store
         
     | 
| 
      
 16 
     | 
    
         
            +
            .DS_Store?
         
     | 
| 
      
 17 
     | 
    
         
            +
            Gemfile.lock
         
     | 
| 
      
 18 
     | 
    
         
            +
            /doc/TODO
         
     | 
| 
      
 19 
     | 
    
         
            +
            /spec/dummy/log/*.log
         
     | 
| 
      
 20 
     | 
    
         
            +
            /spec/dummy/tmp/
         
     | 
| 
      
 21 
     | 
    
         
            +
            /.idea
         
     | 
    
        data/.rspec
    ADDED
    
    | 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --drb  --color -f n
         
     | 
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2013 kehao
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            MIT License
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 6 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 7 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 8 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 9 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 10 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 11 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 14 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 17 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 18 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 19 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 20 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 21 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 22 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,116 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            [](http://badge.fury.io/rb/area_cn)
         
     | 
| 
      
 2 
     | 
    
         
            +
            # DistrictCn
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            地区码查询
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            ## Installation
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            Add this line to your application's Gemfile:
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                gem 'district_cn'
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            And then execute:
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                $ bundle
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            Or install it yourself as:
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                $ gem install district_cn
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
            ## Usage
         
     | 
| 
      
 21 
     | 
    
         
            +
            ### DistrictCn::Code
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 24 
     | 
    
         
            +
            # new
         
     | 
| 
      
 25 
     | 
    
         
            +
            DistrictCn.Code(331002) # or DistrictCn::Code.new(331002)
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
            # Instance methods 
         
     | 
| 
      
 28 
     | 
    
         
            +
            code = DistrictCn.code(331000)
         
     | 
| 
      
 29 
     | 
    
         
            +
            => #<DistrictCn::Code:0x007fd5c48e3590 @value=331000, @id="331000"> 
         
     | 
| 
      
 30 
     | 
    
         
            +
            code.value                     #=> 331000   #原始输入的地区码
         
     | 
| 
      
 31 
     | 
    
         
            +
            code.id                        #=> "331000" #code.value.to_s
         
     | 
| 
      
 32 
     | 
    
         
            +
            code.name                      #=> "台州市"
         
     | 
| 
      
 33 
     | 
    
         
            +
            code.area_name(default = "-")  #=> "浙江省-台州市"
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
            code.province?                 #=> false
         
     | 
| 
      
 36 
     | 
    
         
            +
            code.province                  #=> {:text=>"浙江省",:children=>{...}}
         
     | 
| 
      
 37 
     | 
    
         
            +
            code.province_id               #=> "330000"
         
     | 
| 
      
 38 
     | 
    
         
            +
            code.province_name             #=> "浙江省"
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            code.city?                     #=> true
         
     | 
| 
      
 41 
     | 
    
         
            +
            code.city                      #=> {:text=>"台州市",:children=>{...}}
         
     | 
| 
      
 42 
     | 
    
         
            +
            code.city_id                   #=> "331000"
         
     | 
| 
      
 43 
     | 
    
         
            +
            code.city_name                 #=> "台州市"
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
            code.district?                 #=> false
         
     | 
| 
      
 46 
     | 
    
         
            +
            code.district                  #=> nil
         
     | 
| 
      
 47 
     | 
    
         
            +
            code.district_id               #=> nil
         
     | 
| 
      
 48 
     | 
    
         
            +
            code.district_name             #=> nil
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
            code.children
         
     | 
| 
      
 51 
     | 
    
         
            +
            #=>[["黄岩区", "331003"],
         
     | 
| 
      
 52 
     | 
    
         
            +
            #   ["椒江区", "331002"],
         
     | 
| 
      
 53 
     | 
    
         
            +
            #   ["临海市", "331082"],
         
     | 
| 
      
 54 
     | 
    
         
            +
            #   ["路桥区", "331004"],
         
     | 
| 
      
 55 
     | 
    
         
            +
            #   ["三门县", "331022"],
         
     | 
| 
      
 56 
     | 
    
         
            +
            #   ["市辖区", "331001"],
         
     | 
| 
      
 57 
     | 
    
         
            +
            #   ["天台县", "331023"],
         
     | 
| 
      
 58 
     | 
    
         
            +
            #   ["温岭市", "331081"],
         
     | 
| 
      
 59 
     | 
    
         
            +
            #   ["仙居县", "331024"],
         
     | 
| 
      
 60 
     | 
    
         
            +
            #   ["玉环县", "331021"]]
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
            id.as_options
         
     | 
| 
      
 63 
     | 
    
         
            +
            # => 返回生成select需要的数据
         
     | 
| 
      
 64 
     | 
    
         
            +
            ```
         
     | 
| 
      
 65 
     | 
    
         
            +
            ### DistrictCn
         
     | 
| 
      
 66 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 67 
     | 
    
         
            +
            codes = DistrictCn.search("浙江")
         
     | 
| 
      
 68 
     | 
    
         
            +
            #=> [#<DistrictCn::Code:0x007fd5c483f990 @value="330000", @id="330000">]
         
     | 
| 
      
 69 
     | 
    
         
            +
            codes.first.name
         
     | 
| 
      
 70 
     | 
    
         
            +
            #=> "浙江省"
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
            DistrictCn.tree #树状结构数据
         
     | 
| 
      
 73 
     | 
    
         
            +
            DistrictCn.list #数据列表
         
     | 
| 
      
 74 
     | 
    
         
            +
            ```
         
     | 
| 
      
 75 
     | 
    
         
            +
            ## 使用act_as_area_field 简化调用
         
     | 
| 
      
 76 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 77 
     | 
    
         
            +
            #company.rb
         
     | 
| 
      
 78 
     | 
    
         
            +
            class Company < ActiveRecord::Base
         
     | 
| 
      
 79 
     | 
    
         
            +
              attr_accessor :region_code
         
     | 
| 
      
 80 
     | 
    
         
            +
              attr_accessible :region_code
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
              act_as_area_field :region_code #增加这一行
         
     | 
| 
      
 83 
     | 
    
         
            +
              validates :region_code, presence: true
         
     | 
| 
      
 84 
     | 
    
         
            +
            end
         
     | 
| 
      
 85 
     | 
    
         
            +
            ```
         
     | 
| 
      
 86 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 87 
     | 
    
         
            +
            company = Company.new
         
     | 
| 
      
 88 
     | 
    
         
            +
            company.region_code = 331002
         
     | 
| 
      
 89 
     | 
    
         
            +
            #不使用act_as_area_field
         
     | 
| 
      
 90 
     | 
    
         
            +
            company.region_code 
         
     | 
| 
      
 91 
     | 
    
         
            +
            #=> 331002
         
     | 
| 
      
 92 
     | 
    
         
            +
            District::Cn.code(company.region_code).name
         
     | 
| 
      
 93 
     | 
    
         
            +
            #=> "椒江区"
         
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
            #使用act_as_area_field
         
     | 
| 
      
 96 
     | 
    
         
            +
            company.region_code 
         
     | 
| 
      
 97 
     | 
    
         
            +
            #返回District::Cn::code对像
         
     | 
| 
      
 98 
     | 
    
         
            +
            #=> #<District::Cn::code:0x007fb7a4c0e960 @value=331002, @id="331002">
         
     | 
| 
      
 99 
     | 
    
         
            +
            company.region_code.name
         
     | 
| 
      
 100 
     | 
    
         
            +
            #=> "椒江区"
         
     | 
| 
      
 101 
     | 
    
         
            +
            ```
         
     | 
| 
      
 102 
     | 
    
         
            +
             
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
            ## Test
         
     | 
| 
      
 106 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 107 
     | 
    
         
            +
            rake spec
         
     | 
| 
      
 108 
     | 
    
         
            +
            ```
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
            ## Contributing
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
            1. Fork it
         
     | 
| 
      
 113 
     | 
    
         
            +
            2. Create your feature branch (`git checkout -b my-new-feature`)
         
     | 
| 
      
 114 
     | 
    
         
            +
            3. Commit your changes (`git commit -am 'Add some feature'`)
         
     | 
| 
      
 115 
     | 
    
         
            +
            4. Push to the branch (`git push origin my-new-feature`)
         
     | 
| 
      
 116 
     | 
    
         
            +
            5. Create new Pull Request
         
     | 
    
        data/Rakefile
    ADDED
    
    
    
        data/district_cn.gemspec
    ADDED
    
    | 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # coding: utf-8
         
     | 
| 
      
 2 
     | 
    
         
            +
            lib = File.expand_path('../lib', __FILE__)
         
     | 
| 
      
 3 
     | 
    
         
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'district_cn/version'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            Gem::Specification.new do |spec|
         
     | 
| 
      
 7 
     | 
    
         
            +
              spec.name          = "district_cn"
         
     | 
| 
      
 8 
     | 
    
         
            +
              spec.version       = DistrictCn::VERSION
         
     | 
| 
      
 9 
     | 
    
         
            +
              spec.authors       = ["kehao"]
         
     | 
| 
      
 10 
     | 
    
         
            +
              spec.email         = ["kehao.qiu@gmail.com"]
         
     | 
| 
      
 11 
     | 
    
         
            +
              spec.description   = %q{地区码查询}
         
     | 
| 
      
 12 
     | 
    
         
            +
              spec.summary       = %q{地区码查询}
         
     | 
| 
      
 13 
     | 
    
         
            +
            #  spec.homepage      = ""
         
     | 
| 
      
 14 
     | 
    
         
            +
              spec.license       = "MIT"
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
              spec.files         = `git ls-files`.split($/)
         
     | 
| 
      
 17 
     | 
    
         
            +
              spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
         
     | 
| 
      
 18 
     | 
    
         
            +
              spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
         
     | 
| 
      
 19 
     | 
    
         
            +
              spec.require_paths = ["lib"]
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              spec.add_development_dependency "bundler", "~> 1.3"
         
     | 
| 
      
 22 
     | 
    
         
            +
              spec.add_development_dependency "rake"
         
     | 
| 
      
 23 
     | 
    
         
            +
              spec.add_development_dependency "rspec"
         
     | 
| 
      
 24 
     | 
    
         
            +
              spec.add_development_dependency "activerecord", '>= 3.2.13'
         
     | 
| 
      
 25 
     | 
    
         
            +
              spec.add_development_dependency "sqlite3"
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
              spec.add_dependency 'activesupport', '>= 3.2.13'
         
     | 
| 
      
 28 
     | 
    
         
            +
              spec.add_dependency 'json', '>= 1.7.5'
         
     | 
| 
      
 29 
     | 
    
         
            +
            end
         
     |