-
Notifications
You must be signed in to change notification settings - Fork 6
/
division.rb
67 lines (52 loc) · 1.35 KB
/
division.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class GB2260
class Division
using DeprecationWarner
attr_reader :code, :name, :revision
deprecate :year, :revision
def self.get(code, revision=nil)
name = Data.search(code, revision)
name && new(code, name, revision)
end
def self.batch(codes, revision=nil)
codes.map { |code| get(code, revision) }.compact
end
def initialize(code, name, revision=nil)
@code = code.to_s
@name = name.to_s
@revision = (revision || DEFAULT_REVISION).to_s
end
def ==(other)
eql? other
end
def eql?(other)
code == other.code && revision == other.revision
end
def description(separator = '/')
[province, prefecture, county].compact.map(&:name).join(separator)
end
def to_s
"<GB2260-#{revision.split(NAMESPACE_SEPARATOR)[-1]} #{code} #{description}>"
end
def hash
[@code, @revision].hash
end
def province
Division.get(@code[0,2] + PROVINCE_SUFFIX, @revision)
end
def is_province?
province == self
end
def prefecture
Division.get(@code[0,4] + PREFECTURE_SUFFIX, @revision) unless is_province?
end
def is_prefecture?
prefecture == self
end
def county
self unless is_province? or is_prefecture?
end
def is_county?
!is_province? and !is_prefecture?
end
end
end