unidom-region-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: e74b75d5ef9c0aa9e94a8977e764c0a1abeecfb6
4
+ data.tar.gz: 2cd4094801e33d047957f47c7ef78fccac8fb50b
5
+ SHA512:
6
+ metadata.gz: dbf52066e80a9b78d2d3966895d8ff83ede28ea25edff7b4d6d4e744b8dce4014ff4312c2f460004d028cde3e95657b5730f8cf53e227b367b7cb542b53a1b64
7
+ data.tar.gz: 2c6d6f1d4c7d463991f89e44e13bd212667320d7b6916669e2885d52117aca99544862e80757b14162c27912e65c741461b0d2e634eb192ec114eebc9d5bb58c
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,18 @@
1
+ # Unidom Region China
2
+
3
+ Unidom (UNIfied Domain Object Model) is a series of domain model engines. The China Region domain model engine includes the Region model and its migration.
4
+ Unidom (统一领域对象模型)是一系列的领域模型引擎。中国行政区划领域模型引擎包括中国大陆的行政区划模型及其数据迁移脚本。
5
+
6
+ ## Usage in Gemfile:
7
+ ```ruby
8
+ gem 'unidom-region-china'
9
+ ```
10
+ ## Run the Database Migration:
11
+ ```shell
12
+ rake db:migrate
13
+ ```
14
+
15
+ ## Call the Model:
16
+ ```ruby
17
+ Unidom::Region::China::Region.numeric_coded_as('120000').valid_at.alive.first # Tianjing (天津)
18
+ ```
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 Region 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::Region::China::ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ module Unidom::Region::China::ApplicationHelper
2
+ end
@@ -0,0 +1,110 @@
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::Region::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 :schema, polymorphic: true
33
+ has_many :locations, class_name: 'Unidom::Geo::Location', as: :region
34
+
35
+ scope :schema_is, ->(schema) { schema.present? ? where(schema: schema) : schema_id_is.schema_type_is }
36
+ scope :schema_id_is, ->(schema_id = ::Unidom::Common::NULL_UUID) { where schema_id: schema_id }
37
+ scope :schema_type_is, ->(schema_type = '' ) { where schema_type: schema_type }
38
+
39
+ scope :name_is, ->(name) { where name: name }
40
+ scope :being_virtual, ->(virtual = true) { where virtual: virtual }
41
+
42
+ include ::Unidom::Common::Concerns::ModelExtension
43
+
44
+ def numeric_code_prefix
45
+ numeric_code[0..1]
46
+ end
47
+
48
+ def numeric_code_middle
49
+ numeric_code[2..3]
50
+ end
51
+
52
+ def numeric_code_suffix
53
+ numeric_code[4..5]
54
+ end
55
+
56
+ def numeric_code_middle_empty?
57
+ '00'==numeric_code_middle
58
+ end
59
+
60
+ def numeric_code_suffix_empty?
61
+ '00'==numeric_code_suffix
62
+ end
63
+
64
+ def district?
65
+ numeric_code_suffix.to_i<20
66
+ end
67
+
68
+ def under_mducg?
69
+ self.class::MDUCG_CODES.include? numeric_code_prefix
70
+ end
71
+
72
+ def mducg?
73
+ under_mducg? && numeric_code_middle_empty? && numeric_code_suffix_empty?
74
+ end
75
+
76
+ def super_regions
77
+ 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")
78
+ end
79
+
80
+ def sub_regions
81
+ if numeric_code_suffix_empty?
82
+ prefix = numeric_code_middle_empty? ? numeric_code_prefix : "#{numeric_code_prefix}#{numeric_code_middle}"
83
+ regions = self.class.numeric_code_starting_with(prefix).not_numeric_coded_as(numeric_code)
84
+ regions = regions.numeric_code_ending_with('00') if numeric_code_middle_empty?
85
+ regions
86
+ else
87
+ self.class.none
88
+ end
89
+ end
90
+
91
+ def full_name(separator = ' ')
92
+ final_name = self.name
93
+ current_region = self
94
+ count = 0
95
+ while current_region = current_region.super_regions.first
96
+ count += 1
97
+ final_name = "#{current_region.name}#{separator}#{final_name}" unless current_region.virtual?
98
+ break if count>5
99
+ end
100
+ final_name
101
+ end
102
+
103
+ def under?(region)
104
+ return false unless region.numeric_code_prefix==numeric_code_prefix
105
+ return false if numeric_code_middle_empty?
106
+ return true if region.numeric_code_middle_empty?
107
+ numeric_code_suffix_empty? ? false : region.numeric_code_suffix_empty?
108
+ end
109
+
110
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Unidom Region China</title>
5
+ <%= stylesheet_link_tag "china/application", media: "all" %>
6
+ <%= javascript_include_tag "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::Region::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 :schema, 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, :schema_id
30
+ add_index :unidom_china_regions, [ :numeric_code, :schema_id, :schema_type ], unique: true, name: 'index_unidom_china_regions_on_numeric_code_and_schema'
31
+ add_index :unidom_china_regions, [ :alphabetic_code, :schema_id, :schema_type ], unique: true, name: 'index_unidom_china_regions_on_alphabetic_code_and_schema'
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,64 @@
1
+ desc 'unidom:region:china:import Rake Task imports China regions from the given CSV file.'
2
+
3
+ namespace :unidom do
4
+ namespace :region do
5
+ namespace :china do
6
+
7
+ # bundle exec rake unidom:region:china:import
8
+ # file=/data.csv
9
+ # from_date=2020-01-01
10
+ # schema_id=
11
+ # schema_type=
12
+ task import: :environment do
13
+
14
+ include ::Unidom::Common::DataHelper
15
+
16
+ file_name = ENV['file']
17
+ schema_id = ENV['schema_id']||::Unidom::Common::NULL_UUID
18
+ schema_type = ENV['schema_type']||''
19
+ opened_at = parse_time ENV['from_date']
20
+
21
+ updated_count = 0
22
+ created_count = 0
23
+
24
+ region_entities = ::Unidom::Region::China::Region.schema_id_is(schema_id).schema_type_is(schema_type).select('id, name, virtual, numeric_code, alphabetic_code, schema_id, schema_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'], schema_id: schema_id, schema_type: schema_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::Region::China::Region.create! attributes
47
+ created_count += 1
48
+ end
49
+ else
50
+ attributes[:numeric_code] = numeric_code
51
+ ::Unidom::Region::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,17 @@
1
+ module Unidom
2
+ module Region
3
+ module China
4
+
5
+ class Engine < ::Rails::Engine
6
+
7
+ isolate_namespace ::Unidom::Region::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 Region
3
+ module China
4
+ VERSION = '0.1'.freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'unidom/region/china/engine'
2
+
3
+ module Unidom
4
+ module Region
5
+ module China
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unidom-region-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-01-22 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/region/china/application.js
40
+ - app/assets/stylesheets/unidom/region/china/application.css
41
+ - app/controllers/unidom/region/china/application_controller.rb
42
+ - app/helpers/unidom/region/china/application_helper.rb
43
+ - app/models/unidom/region/china/region.rb
44
+ - app/views/layouts/unidom/region/china/application.html.erb
45
+ - config/routes.rb
46
+ - db/migrate/20100101010000_create_unidom_china_regions.rb
47
+ - lib/tasks/data_tasks.rake
48
+ - lib/unidom/region/china.rb
49
+ - lib/unidom/region/china/engine.rb
50
+ - lib/unidom/region/china/version.rb
51
+ homepage: https://github.com/topbitdu/unidom-region-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: The China Region domain model engine includes China Region and its relative
75
+ models.
76
+ test_files: []