chinese_regions_rails 0.0.2.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +10 -0
- data/README.md +24 -0
- data/Rakefile +11 -0
- data/lib/chinese_regions_rails.rb +6 -0
- data/lib/chinese_regions_rails/db.rb +32 -0
- data/lib/chinese_regions_rails/railtie.rb +12 -0
- data/lib/chinese_regions_rails/version.rb +3 -0
- data/lib/generators/chinese_regions/install_generator.rb +14 -0
- data/lib/regions.db +0 -0
- data/lib/tasks/seed.rake +32 -0
- data/spec/chinese_regions_rails/db_spec.rb +16 -0
- data/spec/helper.rb +1 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 869404e06d0cdeab7beb3396f12edc2b2044fa45
|
4
|
+
data.tar.gz: e22fedd70b26d1ba507e3dc14d504c104ab8f62b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 17a7760767a5e08d1153b02d7bd482c99f3f89dd9ecdadecdb1c44d0cc74b34ac06a0e4b41d0b8c30d609538a62508acf32ae842373d1855771565a408caa63b
|
7
|
+
data.tar.gz: 9b8943c5fabf36f4cecd44b23a1132a4b8c5a35cd4abfdb960086c2e784026dd107c15077a08aef17a7d6fff9d0dc46a91ce768daeaadb7ac1492c594e88ce72
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# chinese_regions_rails
|
2
|
+
|
3
|
+
中国省市区县数据库,包含 行政编码,邮政编码,地区拼音和简拼。 数据来源 https://github.com/xixilive/chinese_regions_db
|
4
|
+
|
5
|
+
## 使用
|
6
|
+
|
7
|
+
Gemfile
|
8
|
+
|
9
|
+
```
|
10
|
+
gem 'chinese_regions_rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
generate `Region` model
|
14
|
+
|
15
|
+
```
|
16
|
+
rails g chinese_regions:install
|
17
|
+
```
|
18
|
+
|
19
|
+
db migration and seed
|
20
|
+
|
21
|
+
```
|
22
|
+
rake db:migrate
|
23
|
+
rake db:seed:regions
|
24
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
Dir['lib/tasks/*.rake'].each { |rake| load rake }
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new do |t|
|
7
|
+
t.pattern = "spec/chinese_regions_rails/**/*_spec.rb"
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :spec
|
11
|
+
task :test => :spec
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "sqlite3"
|
2
|
+
|
3
|
+
module ChineseRegionsRails
|
4
|
+
class DB
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def query_regions
|
9
|
+
db.execute("select * from regions") do |row|
|
10
|
+
hash = {code: row[0],
|
11
|
+
parent_code: row[1],
|
12
|
+
name: row[2],
|
13
|
+
alias: row[3],
|
14
|
+
pinyin: row[4],
|
15
|
+
abbr: row[5],
|
16
|
+
zip: row[6],
|
17
|
+
level: row[7]
|
18
|
+
}
|
19
|
+
|
20
|
+
yield(hash) if block_given?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def db
|
25
|
+
db_file = File.expand_path("../../regions.db", __FILE__)
|
26
|
+
db = SQLite3::Database.new db_file
|
27
|
+
db
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module ChineseRegions
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
|
6
|
+
desc "Generate region model. Default model name is 'Region'"
|
7
|
+
|
8
|
+
def install
|
9
|
+
class_name = "region"
|
10
|
+
generate "model", "#{class_name} code:string parent_code:string name:string alias:string pinyin:string abbr:string zip:string level:integer"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
data/lib/regions.db
ADDED
Binary file
|
data/lib/tasks/seed.rake
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# default model regions
|
2
|
+
# rake db:seed:regions
|
3
|
+
|
4
|
+
# TODO: rake db:seed:regions xxx_regions
|
5
|
+
# custom model to xxx_regions
|
6
|
+
require 'active_support/core_ext/string/inflections'
|
7
|
+
require "chinese_regions_rails"
|
8
|
+
|
9
|
+
namespace :db do
|
10
|
+
|
11
|
+
namespace :seed do
|
12
|
+
# TODO: reseed
|
13
|
+
|
14
|
+
desc "seed regions data to db"
|
15
|
+
task regions: :environment do
|
16
|
+
args ||= {}
|
17
|
+
args[:model_class] = 'region'
|
18
|
+
region_class = args[:model_class].classify.constantize
|
19
|
+
|
20
|
+
ChineseRegionsRails::DB.query_regions do |region_param|
|
21
|
+
region_class.create(region_param)
|
22
|
+
end
|
23
|
+
|
24
|
+
puts "insert regions"
|
25
|
+
puts "total : #{format("%4d", region_class.count)}"
|
26
|
+
(1..3).each do |level|
|
27
|
+
puts "level #{level}: #{format("%4d", region_class.where(level: level).count)}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
require "chinese_regions_rails/db"
|
4
|
+
|
5
|
+
describe "db" do
|
6
|
+
it "db" do
|
7
|
+
db = ChineseRegionsRails::DB.db
|
8
|
+
expect(db).to be_truthy
|
9
|
+
end
|
10
|
+
|
11
|
+
it "query regions" do
|
12
|
+
ChineseRegionsRails::DB.query_regions do |region_param|
|
13
|
+
expect(region_param).to be_truthy
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rspec'
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chinese_regions_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2.rc1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Basten Gao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: sqlite3
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ~>
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.3'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.3'
|
47
|
+
description: 中国省市区县数据库,包含 行政编码,邮政编码,地区拼音和简拼。 数据来源 https://github.com/xixilive/chinese_regions_db
|
48
|
+
email: bastengao@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- Gemfile
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/chinese_regions_rails.rb
|
57
|
+
- lib/chinese_regions_rails/db.rb
|
58
|
+
- lib/chinese_regions_rails/railtie.rb
|
59
|
+
- lib/chinese_regions_rails/version.rb
|
60
|
+
- lib/generators/chinese_regions/install_generator.rb
|
61
|
+
- lib/regions.db
|
62
|
+
- lib/tasks/seed.rake
|
63
|
+
- spec/chinese_regions_rails/db_spec.rb
|
64
|
+
- spec/helper.rb
|
65
|
+
homepage: https://github.com/bastengao/chinese_regions_rails
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.9.3
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.8.11
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.2.2
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: 中国省市区县数据库
|
89
|
+
test_files:
|
90
|
+
- spec/chinese_regions_rails/db_spec.rb
|
91
|
+
- spec/helper.rb
|