unidom-geo-china 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/app/models/unidom/geo/china/region.rb +1 -0
- data/app/models/unidom/geo/china/town.rb +15 -0
- data/db/migrate/20010491560020_create_unidom_china_towns.rb +32 -0
- data/lib/tasks/data_tasks.rake +76 -33
- data/lib/unidom/geo/china/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c644d279817669978931c34931bc44835a42c084
|
4
|
+
data.tar.gz: 8f5cea3672acb64e112cfe2c5d8727b206e989ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 680ce9d86dec8bec6d500b856704748ebd61cdcba4a802286139c94553fd03847f39e99c3229fda70977d8832dc50a7ea9faec348d3826ff48b2f6f2c651b660
|
7
|
+
data.tar.gz: d64016110315bbbac2fa61ddefd6f3864e5430e472a4e0a9d78ef114d314a84f9b46bebc4610a963a39ea5f9cb207ef6edce8d2aa247566e88b7b1a4bba66844
|
data/README.md
CHANGED
@@ -18,7 +18,8 @@ rake db:migrate
|
|
18
18
|
|
19
19
|
## Import Data
|
20
20
|
```shell
|
21
|
-
bundle exec rake unidom:geo:china:import file=/china-region-data/NBS-county/20141031.csv from_date=2014-10-31 scheme_id= scheme_type=
|
21
|
+
bundle exec rake unidom:geo:china:region:import file=/china-region-data/NBS-county/20141031.csv from_date=2014-10-31 scheme_id= scheme_type=
|
22
|
+
bundle exec rake unidom:geo:china:town:import file=/china-region-data/NBS-town/20141031.csv from_date=2014-10-31 scheme_id= scheme_type=
|
22
23
|
```
|
23
24
|
|
24
25
|
## Call the Model
|
@@ -31,6 +31,7 @@ class Unidom::Geo::China::Region < ActiveRecord::Base
|
|
31
31
|
|
32
32
|
belongs_to :scheme, polymorphic: true
|
33
33
|
has_many :locations, class_name: 'Unidom::Geo::Location', as: :region
|
34
|
+
has_many :towns, class_name: 'Unidom::Geo::China::Town'
|
34
35
|
|
35
36
|
scope :scheme_is, ->(scheme) { scheme.present? ? where(scheme: scheme) : scheme_id_is.scheme_type_is }
|
36
37
|
scope :scheme_id_is, ->(scheme_id = ::Unidom::Common::NULL_UUID) { where scheme_id: scheme_id }
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Town 是乡或者镇。
|
2
|
+
|
3
|
+
class Unidom::Geo::China::Town < ActiveRecord::Base
|
4
|
+
|
5
|
+
self.table_name = 'unidom_china_towns'
|
6
|
+
|
7
|
+
include Unidom::Common::Concerns::ModelExtension
|
8
|
+
|
9
|
+
validates :name, presence: true, length: { in: 2..self.columns_hash['name'].limit }
|
10
|
+
|
11
|
+
belongs_to :region, class_name: 'Unidom::Geo::China::Region'
|
12
|
+
|
13
|
+
scope :region_is, ->(region) { where region_id: to_id(region) }
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class CreateUnidomChinaTowns < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def change
|
4
|
+
|
5
|
+
create_table :unidom_china_towns, id: :uuid do |t|
|
6
|
+
|
7
|
+
t.references :region, type: :uuid, null: false
|
8
|
+
|
9
|
+
t.column :numeric_code, 'char(9)', null: false, default: '0'*9
|
10
|
+
|
11
|
+
t.string :name, null: false, default: '', limit: 200
|
12
|
+
t.boolean :virtual, null: false, default: false
|
13
|
+
|
14
|
+
t.text :instruction
|
15
|
+
t.text :description
|
16
|
+
|
17
|
+
t.column :state, 'char(1)', null: false, default: 'C'
|
18
|
+
t.datetime :opened_at, null: false, default: ::Time.utc(1970)
|
19
|
+
t.datetime :closed_at, null: false, default: ::Time.utc(3000)
|
20
|
+
t.boolean :defunct, null: false, default: false
|
21
|
+
t.jsonb :notation, null: false, default: {}
|
22
|
+
|
23
|
+
t.timestamps null: false
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
add_index :unidom_china_towns, :region_id
|
28
|
+
add_index :unidom_china_towns, :numeric_code
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/lib/tasks/data_tasks.rake
CHANGED
@@ -4,58 +4,101 @@ namespace :unidom do
|
|
4
4
|
namespace :geo do
|
5
5
|
namespace :china do
|
6
6
|
|
7
|
-
|
8
|
-
# file=/data.csv
|
9
|
-
# from_date=2020-01-01
|
10
|
-
# scheme_id=
|
11
|
-
# scheme_type=
|
12
|
-
task import: :environment do
|
7
|
+
namespace :region do
|
13
8
|
|
14
|
-
|
9
|
+
# bundle exec rake unidom:geo:china:region:import
|
10
|
+
# file=/data.csv
|
11
|
+
# from_date=2020-01-01
|
12
|
+
# scheme_id=
|
13
|
+
# scheme_type=
|
14
|
+
task import: :environment do
|
15
15
|
|
16
|
-
|
17
|
-
scheme_id = ENV['scheme_id']||::Unidom::Common::NULL_UUID
|
18
|
-
scheme_type = ENV['scheme_type']||''
|
19
|
-
opened_at = parse_time ENV['from_date']
|
16
|
+
include ::Unidom::Common::DataHelper
|
20
17
|
|
21
|
-
|
22
|
-
|
18
|
+
file_name = ENV['file']
|
19
|
+
scheme_id = ENV['scheme_id']||::Unidom::Common::NULL_UUID
|
20
|
+
scheme_type = ENV['scheme_type']||''
|
21
|
+
opened_at = parse_time ENV['from_date']
|
23
22
|
|
24
|
-
|
23
|
+
updated_count = 0
|
24
|
+
created_count = 0
|
25
25
|
|
26
|
-
|
26
|
+
region_entities = ::Unidom::Geo::China::Region.scheme_id_is(scheme_id).scheme_type_is(scheme_type).select('id, name, virtual, numeric_code, alphabetic_code, scheme_id, scheme_type, opened_at, closed_at, defunct, updated_at').to_a
|
27
27
|
|
28
|
-
|
29
|
-
alphabetic_code = region['alphabetic_code']
|
28
|
+
each_csv_row file_name do |region|
|
30
29
|
|
31
|
-
|
32
|
-
|
30
|
+
numeric_code = region['numeric_code']
|
31
|
+
alphabetic_code = region['alphabetic_code']
|
33
32
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
attributes = { name: region['name'], virtual: region['virtual'], scheme_id: scheme_id, scheme_type: scheme_type, opened_at: opened_at }
|
34
|
+
attributes[:alphabetic_code] = alphabetic_code if alphabetic_code.present?
|
35
|
+
|
36
|
+
if region_entities.present?
|
37
|
+
found_region_entities = region_entities.select { |region_entity| region_entity.numeric_code==numeric_code }
|
38
|
+
if found_region_entities.present?
|
39
|
+
found_region_entities.each do |found_region_entity|
|
40
|
+
found_region_entity.assign_attributes attributes
|
41
|
+
if found_region_entity.changed?
|
42
|
+
found_region_entity.save!
|
43
|
+
updated_count += 1
|
44
|
+
end
|
42
45
|
end
|
46
|
+
else
|
47
|
+
attributes[:numeric_code] = numeric_code
|
48
|
+
::Unidom::Geo::China::Region.create! attributes
|
49
|
+
created_count += 1
|
43
50
|
end
|
44
51
|
else
|
45
52
|
attributes[:numeric_code] = numeric_code
|
46
53
|
::Unidom::Geo::China::Region.create! attributes
|
47
54
|
created_count += 1
|
48
55
|
end
|
49
|
-
|
50
|
-
attributes[:numeric_code] = numeric_code
|
51
|
-
::Unidom::Geo::China::Region.create! attributes
|
52
|
-
created_count += 1
|
56
|
+
|
53
57
|
end
|
54
58
|
|
59
|
+
puts "#{created_count} China Regions were created. #{updated_count} China Regions were updated per CSV."
|
60
|
+
puts "#{created_count+updated_count} China Regions were handled totally."
|
61
|
+
|
55
62
|
end
|
56
63
|
|
57
|
-
|
58
|
-
|
64
|
+
end
|
65
|
+
|
66
|
+
namespace :town do
|
67
|
+
|
68
|
+
# bundle exec rake unidom:geo:china:town:import
|
69
|
+
# file=/data.csv
|
70
|
+
# from_date=2020-01-01
|
71
|
+
task import: :environment do
|
72
|
+
|
73
|
+
include ::Unidom::Common::DataHelper
|
74
|
+
|
75
|
+
file_name = ENV['file']
|
76
|
+
opened_at = parse_time ENV['from_date']
|
77
|
+
|
78
|
+
updated_count = 0
|
79
|
+
created_count = 0
|
80
|
+
|
81
|
+
each_csv_row file_name do |region|
|
82
|
+
|
83
|
+
numeric_code = region['numeric_code']
|
84
|
+
|
85
|
+
town = Unidom::Geo::China::Town.numeric_coded_as(numeric_code).first
|
86
|
+
county = Unidom::Geo::China::Region.numeric_coded_as(numeric_code[0..5]).valid_at.alive.first
|
87
|
+
if town.present?
|
88
|
+
town.assign_attributes region: county, name: region['name'], virtual: region['virtual']
|
89
|
+
town.save!
|
90
|
+
updated_count += 1
|
91
|
+
else
|
92
|
+
town = Unidom::Geo::China::Town.create! region: county, numeric_code: numeric_code, name: region['name'], virtual: region['virtual'], opened_at: opened_at
|
93
|
+
created_count += 1
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
puts "#{created_count} China Regions were created. #{updated_count} China Regions were updated per CSV."
|
99
|
+
puts "#{created_count+updated_count} China Regions were handled totally."
|
100
|
+
|
101
|
+
end
|
59
102
|
|
60
103
|
end
|
61
104
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unidom-geo-china
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Topbit Du
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: unidom-common
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.5'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.5'
|
27
27
|
description: Unidom (UNIfied Domain Object Model) is a series of domain model engines.
|
28
28
|
The China Contact domain model engine includes Mobile Phone Number and its relative
|
29
29
|
models. Unidom (统一领域对象模型)是一系列的领域模型引擎。中国地理领域模型引擎包括中国大陆的行政区划及其相关的模型。
|
@@ -41,9 +41,11 @@ files:
|
|
41
41
|
- app/controllers/unidom/geo/china/application_controller.rb
|
42
42
|
- app/helpers/unidom/geo/china/application_helper.rb
|
43
43
|
- app/models/unidom/geo/china/region.rb
|
44
|
+
- app/models/unidom/geo/china/town.rb
|
44
45
|
- app/views/layouts/unidom/geo/china/application.html.erb
|
45
46
|
- config/routes.rb
|
46
47
|
- db/migrate/20010491560010_create_unidom_china_regions.rb
|
48
|
+
- db/migrate/20010491560020_create_unidom_china_towns.rb
|
47
49
|
- lib/tasks/data_tasks.rake
|
48
50
|
- lib/unidom/geo/china.rb
|
49
51
|
- lib/unidom/geo/china/engine.rb
|