GB2260 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ class GB2260
2
+ LATEST_YEAR = '2014'.freeze
3
+ PROVINCE_SUFFIX = '0000'.freeze
4
+ PREFECTURE_SUFFIX = '00'.freeze
5
+ end
@@ -0,0 +1,25 @@
1
+ class GB2260
2
+ class Data
3
+ class << self
4
+ def data
5
+ gem_dir = File.join(File.dirname(__FILE__), '../../')
6
+ Dir.chdir(gem_dir) do
7
+ @data ||= Hash[Dir.glob("data/*.txt").map do |fn|
8
+ [
9
+ fn.gsub(/data\/GB2260-/, '').gsub(/\.txt$/, ''),
10
+ Hash[
11
+ File.readlines(File.expand_path(File.join(gem_dir, fn))).map do |l|
12
+ l.strip.split("\t")
13
+ end]
14
+ ]
15
+ end]
16
+ end
17
+ end
18
+
19
+ def search(code, year=nil)
20
+ year ||= LATEST_YEAR
21
+ data[year.to_s][code.to_s]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ class GB2260
2
+ class Division
3
+ attr_reader :code, :name, :year
4
+
5
+ def self.get(code, year=nil)
6
+ new(code, Data.search(code, year))
7
+ end
8
+
9
+ def initialize(code, name, year=nil)
10
+ @code = code.to_s
11
+ @name = name.to_s
12
+ @year = year
13
+ end
14
+
15
+ def ==(other)
16
+ code == other.code && year == other.year
17
+ end
18
+
19
+ def to_s
20
+ "<GB2260-#{@year||LATEST_YEAR} #{[province, prefecture, county].compact.map(&:name).join('/')}>"
21
+ end
22
+
23
+ def province
24
+ Division.get(@code[0,2] + PROVINCE_SUFFIX, @year)
25
+ end
26
+
27
+ def is_province?
28
+ province == self
29
+ end
30
+
31
+ def prefecture
32
+ Division.get(@code[0,4] + PREFECTURE_SUFFIX, @year) unless is_province?
33
+ end
34
+
35
+ def is_prefecture?
36
+ prefecture == self
37
+ end
38
+
39
+ def county
40
+ self unless is_province? or is_prefecture?
41
+ end
42
+
43
+ def is_county?
44
+ !is_province? and !is_prefecture?
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ class GB2260
2
+ VERSION = "0.1.1"
3
+ end
data/lib/GB2260.rb ADDED
@@ -0,0 +1,35 @@
1
+ require "GB2260/version"
2
+ require "GB2260/constants"
3
+ require "GB2260/data"
4
+ require "GB2260/division"
5
+
6
+ class GB2260
7
+ def initialize(year=nil)
8
+ @year = year || LATEST_YEAR
9
+ end
10
+
11
+ def get(code)
12
+ Division.get(code, @year)
13
+ end
14
+
15
+ def provinces
16
+ Data.data[@year].keys
17
+ .select { |c| c.end_with? PROVINCE_SUFFIX }
18
+ .map { |c| Division.get(c, @year) }
19
+ end
20
+
21
+ def prefectures(province_code)
22
+ Data.data[@year].keys
23
+ .select { |c| c.start_with? province_code.to_s[0,2] }
24
+ .select { |c| c.end_with? PREFECTURE_SUFFIX }
25
+ .reject { |c| c.end_with? PROVINCE_SUFFIX }
26
+ .map { |c| Division.get(c, @year) }
27
+ end
28
+
29
+ def counties(prefecture_code)
30
+ Data.data[@year].keys
31
+ .select { |c| c.start_with? prefecture_code.to_s[0,4] }
32
+ .reject { |c| c.end_with? PREFECTURE_SUFFIX }
33
+ .map { |c| Division.get(c, @year) }
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: GB2260
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - WolfLee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - liyuan0228@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".gitmodules"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CHANGELOG.md
67
+ - CODE_OF_CONDUCT.md
68
+ - GB2260.gemspec
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - data/GB2260-2002.txt
76
+ - data/GB2260-2003.txt
77
+ - data/GB2260-200306.txt
78
+ - data/GB2260-2004.txt
79
+ - data/GB2260-200403.txt
80
+ - data/GB2260-200409.txt
81
+ - data/GB2260-2005.txt
82
+ - data/GB2260-200506.txt
83
+ - data/GB2260-2006.txt
84
+ - data/GB2260-2007.txt
85
+ - data/GB2260-2008.txt
86
+ - data/GB2260-2009.txt
87
+ - data/GB2260-2010.txt
88
+ - data/GB2260-2011.txt
89
+ - data/GB2260-2012.txt
90
+ - data/GB2260-2013.txt
91
+ - data/GB2260-2014.txt
92
+ - lib/GB2260.rb
93
+ - lib/GB2260/constants.rb
94
+ - lib/GB2260/data.rb
95
+ - lib/GB2260/division.rb
96
+ - lib/GB2260/version.rb
97
+ homepage: https://github.com/wolflee/GB2260.rb
98
+ licenses:
99
+ - MIT
100
+ metadata:
101
+ allowed_push_host: https://rubygems.org
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.5.1
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: The Ruby implementation for looking up the Chinese administrative divisions.
122
+ test_files: []