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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4c698cb0b5f946d24d256fd7be15bbae5f4dc2e
4
- data.tar.gz: 8a17f513dcdf0d8b31f971d39a0b8c8aadcaacf5
3
+ metadata.gz: c644d279817669978931c34931bc44835a42c084
4
+ data.tar.gz: 8f5cea3672acb64e112cfe2c5d8727b206e989ee
5
5
  SHA512:
6
- metadata.gz: 6d26eb0dd473d5b37d42a81c1233007efdb67ecc88a93c44b6b9baca93e25b898fa72fe9b4d20027764ee71ddfbac17272ccca70a9e084607b78e813f641e345
7
- data.tar.gz: 5bc709580f3ecfb7f9a7ed498d2739e0fd7014f7c021105091f5065d5ed12e39ac67b9809817d1fd03a66ff235c49e301e0dbf69e771de00da94ca81ef7830da
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
@@ -4,58 +4,101 @@ namespace :unidom do
4
4
  namespace :geo do
5
5
  namespace :china do
6
6
 
7
- # bundle exec rake unidom:geo:china:import
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
- include ::Unidom::Common::DataHelper
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
- file_name = ENV['file']
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
- updated_count = 0
22
- created_count = 0
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
- 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
23
+ updated_count = 0
24
+ created_count = 0
25
25
 
26
- each_csv_row file_name do |region|
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
- numeric_code = region['numeric_code']
29
- alphabetic_code = region['alphabetic_code']
28
+ each_csv_row file_name do |region|
30
29
 
31
- attributes = { name: region['name'], virtual: region['virtual'], scheme_id: scheme_id, scheme_type: scheme_type, opened_at: opened_at }
32
- attributes[:alphabetic_code] = alphabetic_code if alphabetic_code.present?
30
+ numeric_code = region['numeric_code']
31
+ alphabetic_code = region['alphabetic_code']
33
32
 
34
- if region_entities.present?
35
- found_region_entities = region_entities.select { |region_entity| region_entity.numeric_code==numeric_code }
36
- if found_region_entities.present?
37
- found_region_entities.each do |found_region_entity|
38
- found_region_entity.assign_attributes attributes
39
- if found_region_entity.changed?
40
- found_region_entity.save!
41
- updated_count += 1
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
- else
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
- puts "#{created_count} China Regions were created. #{updated_count} China Regions were updated per CSV."
58
- puts "#{created_count+updated_count} China Regions were handled totally."
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
 
@@ -1,7 +1,7 @@
1
1
  module Unidom
2
2
  module Geo
3
3
  module China
4
- VERSION = '0.1'.freeze
4
+ VERSION = '0.2'.freeze
5
5
  end
6
6
  end
7
7
  end
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.1'
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-02-25 00:00:00.000000000 Z
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.2'
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.2'
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