unidom-geo-china 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4c698cb0b5f946d24d256fd7be15bbae5f4dc2e
4
+ data.tar.gz: 8a17f513dcdf0d8b31f971d39a0b8c8aadcaacf5
5
+ SHA512:
6
+ metadata.gz: 6d26eb0dd473d5b37d42a81c1233007efdb67ecc88a93c44b6b9baca93e25b898fa72fe9b4d20027764ee71ddfbac17272ccca70a9e084607b78e813f641e345
7
+ data.tar.gz: 5bc709580f3ecfb7f9a7ed498d2739e0fd7014f7c021105091f5065d5ed12e39ac67b9809817d1fd03a66ff235c49e301e0dbf69e771de00da94ca81ef7830da
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Topbit Du
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Unidom Geo China 中国地理领域模型引擎
2
+
3
+ [![License](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT)
4
+ [![Gem Version](https://badge.fury.io/rb/unidom-geo-china.svg)](https://badge.fury.io/rb/unidom-geo-china)
5
+
6
+ Unidom (UNIfied Domain Object Model) is a series of domain model engines. The China Geo domain model engine includes the Region model and its migration.
7
+ Unidom (统一领域对象模型)是一系列的领域模型引擎。中国地理领域模型引擎包括中国大陆的行政区划模型及其数据迁移脚本。
8
+
9
+ ## Usage in Gemfile
10
+ ```ruby
11
+ gem 'unidom-geo-china'
12
+ ```
13
+
14
+ ## Run the Database Migration
15
+ ```shell
16
+ rake db:migrate
17
+ ```
18
+
19
+ ## Import Data
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=
22
+ ```
23
+
24
+ ## Call the Model
25
+ ```ruby
26
+ Unidom::Geo::China::Region.root_level # All the 31 province-level regions including Beijing, Tianjin, etc.
27
+ Unidom::Geo::China::Region.numeric_coded_as('120000').valid_at.alive.first # Tianjin (天津)
28
+ ```
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Unidom Geo China'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+ load 'rails/tasks/statistics.rake'
20
+
21
+
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,2 @@
1
+ class Unidom::Geo::China::ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ module Unidom::Geo::China::ApplicationHelper
2
+ end
@@ -0,0 +1,112 @@
1
+ # Region 是中国行政区划。
2
+ # 《GB/T 2260-2007 中华人民共和国行政区划代码》
3
+ # 数字码采用三层六位层次码结构,按层次分别表示我国各
4
+ # 省(自治区,直辖市,特别行政区)、
5
+ # 市(地区,自治州,盟)、
6
+ # 县(自治县、县级市、旗、自治旗、市辖区、林区、特区)。
7
+ # 第1、2位数字:所在省份编码,表示省、自治区、直辖市、特别行政区。
8
+ # 第3、4位数字:所在省级市(县)编码,表示市、地区、自治州、盟、直辖市所辖市辖区、县汇总码、省(自治区)直辖县级行政区划汇总码。
9
+ # 01~20、51~70表示市,直辖市则01表示市区,02以后表示直辖市辖区内的郊县;
10
+ # 21~50表示地区、自治州、盟;
11
+ # 90表示省(自治区)直辖县级行政区划汇总码。
12
+ # 第5、6位数字:所在地级县(市)编码,表示县、自治县、县级市、旗、自治旗、市辖区、林区、特区。
13
+ # 01~20表示市辖区、地区(自治州、盟)辖县级市、市辖特区以及省(自治区)直辖县级行政区划中的县级市,01通常表示辖区汇总码;
14
+ # 21~80表示县、自治县、旗、自治旗、林区、地区辖特区;
15
+ # 81~99表示省(自治区)辖县级市。
16
+ # 字母代码的编制原则和结构
17
+ # 行政区划字母代码(简称字母码)遵循科学性、统一性、实用性编码原则,参照县及县以上行政区划名称的罗马字母拼写,取相应的字母编制。
18
+ # 省、自治区、直辖市、特别行政区的字母码用两位大写字母表示。
19
+ # 市、地区、自治州、盟、县、自治县、县级市、旗、自治旗、市辖区、林区、特区的字母码用三位大写字母表示。
20
+
21
+ class Unidom::Geo::China::Region < ActiveRecord::Base
22
+
23
+ # MDUCG = MUNICIPALITY DIRECT UNDER CENTRAL GOVERNMENT
24
+ MDUCG_CODES = [ '11', '12', '31', '50' ].freeze
25
+
26
+ self.table_name = 'unidom_china_regions'
27
+
28
+ validates :numeric_code, numericality: { integer_only: true }
29
+ validates :alphabetic_code, allow_blank: true, length: { minimum: 2 }
30
+ validates :name, presence: true, length: { maximum: self.columns_hash['name'].limit }
31
+
32
+ belongs_to :scheme, polymorphic: true
33
+ has_many :locations, class_name: 'Unidom::Geo::Location', as: :region
34
+
35
+ scope :scheme_is, ->(scheme) { scheme.present? ? where(scheme: scheme) : scheme_id_is.scheme_type_is }
36
+ scope :scheme_id_is, ->(scheme_id = ::Unidom::Common::NULL_UUID) { where scheme_id: scheme_id }
37
+ scope :scheme_type_is, ->(scheme_type = '' ) { where scheme_type: scheme_type }
38
+
39
+ scope :name_is, ->(name) { where name: name }
40
+ scope :being_virtual, ->(virtual = true) { where virtual: virtual }
41
+
42
+ scope :root_level, -> { numeric_code_ending_with '0000' }
43
+
44
+ include Unidom::Common::Concerns::ModelExtension
45
+
46
+ def numeric_code_prefix
47
+ numeric_code[0..1]
48
+ end
49
+
50
+ def numeric_code_middle
51
+ numeric_code[2..3]
52
+ end
53
+
54
+ def numeric_code_suffix
55
+ numeric_code[4..5]
56
+ end
57
+
58
+ def numeric_code_middle_empty?
59
+ '00'==numeric_code_middle
60
+ end
61
+
62
+ def numeric_code_suffix_empty?
63
+ '00'==numeric_code_suffix
64
+ end
65
+
66
+ def district?
67
+ numeric_code_suffix.to_i<20
68
+ end
69
+
70
+ def under_mducg?
71
+ self.class::MDUCG_CODES.include? numeric_code_prefix
72
+ end
73
+
74
+ def mducg?
75
+ under_mducg? && numeric_code_middle_empty? && numeric_code_suffix_empty?
76
+ end
77
+
78
+ def super_regions
79
+ numeric_code_suffix_empty? ? (numeric_code_middle_empty? ? self.class.none : self.class.numeric_coded_as("#{numeric_code_prefix}0000")) : self.class.numeric_coded_as("#{numeric_code_prefix}#{numeric_code_middle}00")
80
+ end
81
+
82
+ def sub_regions
83
+ if numeric_code_suffix_empty?
84
+ prefix = numeric_code_middle_empty? ? numeric_code_prefix : "#{numeric_code_prefix}#{numeric_code_middle}"
85
+ regions = self.class.numeric_code_starting_with(prefix).not_numeric_coded_as(numeric_code)
86
+ regions = regions.numeric_code_ending_with('00') if numeric_code_middle_empty?
87
+ regions
88
+ else
89
+ self.class.none
90
+ end
91
+ end
92
+
93
+ def full_name(separator = ' ')
94
+ final_name = self.name
95
+ current_region = self
96
+ count = 0
97
+ while current_region = current_region.super_regions.first
98
+ count += 1
99
+ final_name = "#{current_region.name}#{separator}#{final_name}" unless current_region.virtual?
100
+ break if count>5
101
+ end
102
+ final_name
103
+ end
104
+
105
+ def under?(region)
106
+ return false unless region.numeric_code_prefix==numeric_code_prefix
107
+ return false if numeric_code_middle_empty?
108
+ return true if region.numeric_code_middle_empty?
109
+ numeric_code_suffix_empty? ? false : region.numeric_code_suffix_empty?
110
+ end
111
+
112
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Unidom Geo China</title>
5
+ <%= stylesheet_link_tag 'unidom/geo/china/application', media: 'all' %>
6
+ <%= javascript_include_tag 'unidom/geo/china/application' %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Unidom::Geo::China::Engine.routes.draw do
2
+ end
@@ -0,0 +1,35 @@
1
+ class CreateUnidomChinaRegions < ActiveRecord::Migration
2
+
3
+ def change
4
+
5
+ create_table :unidom_china_regions, id: :uuid do |t|
6
+
7
+ t.references :scheme, type: :uuid, null: false,
8
+ polymorphic: { null: false, default: '', limit: 200 }
9
+
10
+ t.column :numeric_code, 'char(6)', null: false, default: '0'*6
11
+ t.string :alphabetic_code, null: true, default: nil, limit: 3
12
+
13
+ t.string :name, null: false, default: '', limit: 200
14
+ t.boolean :virtual, null: false, default: false
15
+
16
+ t.text :instruction
17
+ t.text :description
18
+
19
+ t.column :state, 'char(1)', null: false, default: 'C'
20
+ t.datetime :opened_at, null: false, default: ::Time.utc(1970)
21
+ t.datetime :closed_at, null: false, default: ::Time.utc(3000)
22
+ t.boolean :defunct, null: false, default: false
23
+ t.jsonb :notation, null: false, default: {}
24
+
25
+ t.timestamps null: false
26
+
27
+ end
28
+
29
+ add_index :unidom_china_regions, :scheme_id
30
+ add_index :unidom_china_regions, [ :numeric_code, :scheme_id, :scheme_type ], unique: true, name: 'index_unidom_china_regions_on_numeric_code_and_scheme'
31
+ add_index :unidom_china_regions, [ :alphabetic_code, :scheme_id, :scheme_type ], unique: true, name: 'index_unidom_china_regions_on_alphabetic_code_and_scheme'
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,64 @@
1
+ desc 'unidom:geo:china:import Rake Task imports China regions from the given CSV file.'
2
+
3
+ namespace :unidom do
4
+ namespace :geo do
5
+ namespace :china do
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
13
+
14
+ include ::Unidom::Common::DataHelper
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']
20
+
21
+ updated_count = 0
22
+ created_count = 0
23
+
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
25
+
26
+ each_csv_row file_name do |region|
27
+
28
+ numeric_code = region['numeric_code']
29
+ alphabetic_code = region['alphabetic_code']
30
+
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?
33
+
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
42
+ end
43
+ end
44
+ else
45
+ attributes[:numeric_code] = numeric_code
46
+ ::Unidom::Geo::China::Region.create! attributes
47
+ created_count += 1
48
+ end
49
+ else
50
+ attributes[:numeric_code] = numeric_code
51
+ ::Unidom::Geo::China::Region.create! attributes
52
+ created_count += 1
53
+ end
54
+
55
+ end
56
+
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."
59
+
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,8 @@
1
+ require 'unidom/geo/china/engine'
2
+
3
+ module Unidom
4
+ module Geo
5
+ module China
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Unidom
2
+ module Geo
3
+ module China
4
+
5
+ class Engine < ::Rails::Engine
6
+
7
+ isolate_namespace ::Unidom::Geo::China
8
+
9
+ initializer :append_migrations do |app|
10
+ config.paths['db/migrate'].expanded.each { |expanded_path| app.config.paths['db/migrate'] << expanded_path } unless app.root.to_s.match root.to_s
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module Unidom
2
+ module Geo
3
+ module China
4
+ VERSION = '0.1'.freeze
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unidom-geo-china
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Topbit Du
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unidom-common
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ description: Unidom (UNIfied Domain Object Model) is a series of domain model engines.
28
+ The China Contact domain model engine includes Mobile Phone Number and its relative
29
+ models. Unidom (统一领域对象模型)是一系列的领域模型引擎。中国地理领域模型引擎包括中国大陆的行政区划及其相关的模型。
30
+ email:
31
+ - topbit.du@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - MIT-LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - app/assets/javascripts/unidom/geo/china/application.js
40
+ - app/assets/stylesheets/unidom/geo/china/application.css
41
+ - app/controllers/unidom/geo/china/application_controller.rb
42
+ - app/helpers/unidom/geo/china/application_helper.rb
43
+ - app/models/unidom/geo/china/region.rb
44
+ - app/views/layouts/unidom/geo/china/application.html.erb
45
+ - config/routes.rb
46
+ - db/migrate/20010491560010_create_unidom_china_regions.rb
47
+ - lib/tasks/data_tasks.rake
48
+ - lib/unidom/geo/china.rb
49
+ - lib/unidom/geo/china/engine.rb
50
+ - lib/unidom/geo/china/version.rb
51
+ homepage: https://github.com/topbitdu/unidom-geo-china
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.4.5.1
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Unidom China Geo Domain Model Engine 中国地理领域模型引擎
75
+ test_files: []