0.01
No commit activity in last 3 years
No release in over 3 years
地区码查询
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 1.7.5
 Project Readme

Gem Version Build Status Dependency Status Coverage Status Code Climate

DistrictCn

地区码查询

Installation

Add this line to your application's Gemfile:

gem 'district_cn'

And then execute:

$ bundle

Or install it yourself as:

$ gem install district_cn

Usage

DistrictCn::Code

# new
DistrictCn.code(331002) 
# or DistrictCn::Code.new(331002)

# Instance methods 
code = DistrictCn.code(331000)
=> #<DistrictCn::Code:0x007fd5c48e3590 @value=331000, @id="331000"> 
code.value                     #=> 331000   #原始输入的地区码
code.id                        #=> "331000" #code.value.to_s
code.name                      #=> "台州市"
code.area_name(default = "-")  #=> "浙江省-台州市"

code.province?                 #=> false
code.province                  #=> {:text=>"浙江省",:children=>{...}}
code.province_id               #=> "330000"
code.province_name             #=> "浙江省"

code.city?                     #=> true
code.city                      #=> {:text=>"台州市",:children=>{...}}
code.city_id                   #=> "331000"
code.city_name                 #=> "台州市"

code.district?                 #=> false
code.district                  #=> nil
code.district_id               #=> nil
code.district_name             #=> nil

code.children
#=>[["黄岩区", "331003"],
#   ["椒江区", "331002"],
#   ["临海市", "331082"],
#   ["路桥区", "331004"],
#   ["三门县", "331022"],
#   ["市辖区", "331001"],
#   ["天台县", "331023"],
#   ["温岭市", "331081"],
#   ["仙居县", "331024"],
#   ["玉环县", "331021"]]

code.as_options
# => 返回生成select需要的数据

DistrictCn

codes = DistrictCn.search("浙江")
#=> [#<DistrictCn::Code:0x007fd5c483f990 @value="330000", @id="330000">]
codes.first.name
#=> "浙江省"

DistrictCn.tree #树状结构数据
DistrictCn.list #数据列表

使用act_as_area_field 简化调用(ActiveRecord适用)

#company.rb
class Company < ActiveRecord::Base
  attr_accessor :region_code
  attr_accessible :region_code

  act_as_area_field :region_code #增加这一行
  validates :region_code, presence: true
end
company = Company.new
company.region_code = 331002
#不使用act_as_area_field
company.region_code 
#=> 331002
District::Cn.code(company.region_code).name
#=> "椒江区"

#使用act_as_area_field
company.region_code 
#返回District::Cn::code对像
#=> #<District::Cn::code:0x007fb7a4c0e960 @value=331002, @id="331002">
company.region_code.name
#=> "椒江区"

Test

rake spec

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request