nayyar 0.1.0 → 0.2.0
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.
- checksums.yaml +5 -5
- data/.circleci/config.yml +38 -0
- data/.rubocop.yml +11 -0
- data/Gemfile +2 -0
- data/README.md +113 -15
- data/Rakefile +4 -2
- data/bin/console +4 -3
- data/bin/seed +11 -0
- data/lib/.DS_Store +0 -0
- data/lib/data/districts.yml +74 -0
- data/lib/data/extract.rb +39 -8
- data/lib/data/locations.csv +501 -1
- data/lib/data/states.yml +15 -0
- data/lib/data/townships.yml +430 -17
- data/lib/nayyar/district.rb +128 -120
- data/lib/nayyar/state.rb +99 -95
- data/lib/nayyar/township.rb +124 -117
- data/lib/nayyar/version.rb +3 -1
- data/lib/nayyar.rb +9 -5
- data/nayyar.gemspec +24 -20
- metadata +53 -20
data/lib/nayyar/district.rb
CHANGED
|
@@ -1,122 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Represents a District in Myanmar
|
|
1
4
|
class Nayyar::District
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
5
|
+
attr_reader :data
|
|
6
|
+
|
|
7
|
+
@districts = nil
|
|
8
|
+
@state_index = {}
|
|
9
|
+
|
|
10
|
+
ATTRIBUTES = %i[
|
|
11
|
+
pcode
|
|
12
|
+
name
|
|
13
|
+
my_name
|
|
14
|
+
].freeze
|
|
15
|
+
|
|
16
|
+
INDICES = %w[pcode].freeze
|
|
17
|
+
|
|
18
|
+
def initialize(data)
|
|
19
|
+
@data = data
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# define getters
|
|
23
|
+
ATTRIBUTES.each do |attr|
|
|
24
|
+
define_method attr do
|
|
25
|
+
@data[attr]
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# allow the values to be retrieved as an array
|
|
30
|
+
def [](key)
|
|
31
|
+
if ATTRIBUTES.include? key
|
|
32
|
+
@data[key]
|
|
33
|
+
elsif key.to_sym == :state
|
|
34
|
+
state
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def state
|
|
39
|
+
Nayyar::State.find_by_pcode(@data[:state])
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def townships
|
|
43
|
+
Nayyar::Township.of_district(self)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class << self
|
|
47
|
+
def all
|
|
48
|
+
districts
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def of_state(state)
|
|
52
|
+
state_pcode = state.pcode
|
|
53
|
+
districts = self.districts
|
|
54
|
+
@state_index[state_pcode].map do |index|
|
|
55
|
+
districts[index]
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def find_by(query)
|
|
60
|
+
key = get_key(query)
|
|
61
|
+
(index = send("#{key}_index".to_sym).index(query[key])) && districts[index]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def find_by!(query)
|
|
65
|
+
if (district = find_by(query))
|
|
66
|
+
district
|
|
67
|
+
else
|
|
68
|
+
key = get_key(query)
|
|
69
|
+
raise Nayyar::DistrictNotFound, "Cannot find State with given #{key}: #{query[key]}"
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
INDICES.each do |index|
|
|
74
|
+
define_method("find_by_#{index}") do |query|
|
|
75
|
+
find_by(index.to_sym => query)
|
|
76
|
+
end
|
|
77
|
+
define_method("find_by_#{index}!") do |query|
|
|
78
|
+
find_by!(index.to_sym => query)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
protected
|
|
83
|
+
|
|
84
|
+
def districts
|
|
85
|
+
unless @districts
|
|
86
|
+
indices = INDICES.inject({}) { |memo, index| memo.merge index => [] }
|
|
87
|
+
@districts = data.each_with_index.map do |district_data, i|
|
|
88
|
+
new(district_data).tap { |district| build_indices(indices, district, district_data[:state], i) }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
INDICES.each do |index|
|
|
92
|
+
class_variable_set("@@#{index}_index", indices[index]) # rubocop:disable Style/ClassVars
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
@districts
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def data
|
|
99
|
+
YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'districts.yml'))
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def build_indices(indices, district, state_pcode, reference_index)
|
|
103
|
+
INDICES.each do |index|
|
|
104
|
+
indices[index] << district.send(index)
|
|
105
|
+
end
|
|
106
|
+
@state_index[state_pcode] ||= []
|
|
107
|
+
@state_index[state_pcode] << reference_index
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
## define private methods for internal use of indexed array
|
|
111
|
+
INDICES.each do |index|
|
|
112
|
+
define_method("#{index}_index") do
|
|
113
|
+
districts
|
|
114
|
+
class_variable_get("@@#{index}_index")
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
## return the index for query given to find_by/find_by! method
|
|
119
|
+
def get_key(data)
|
|
120
|
+
keys = data.keys
|
|
121
|
+
if keys.length != 1 || INDICES.none? { |key| key.to_sym == keys.first.to_sym }
|
|
122
|
+
raise ArgumentError, "`find_by` accepts only one of #{INDICES.join(' or ')} as argument. none provided"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
keys.first
|
|
126
|
+
end
|
|
127
|
+
end
|
|
121
128
|
end
|
|
122
|
-
|
|
129
|
+
|
|
130
|
+
class Nayyar::DistrictNotFound < StandardError; end
|
data/lib/nayyar/state.rb
CHANGED
|
@@ -1,107 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Represents a State in Myanmar
|
|
1
4
|
class Nayyar::State
|
|
2
|
-
|
|
5
|
+
attr_reader :data
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
@states = nil
|
|
8
|
+
@pcode_index = []
|
|
9
|
+
@iso_index = []
|
|
10
|
+
@alpha3_index = []
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
ATTRIBUTES = %i[
|
|
13
|
+
pcode
|
|
14
|
+
iso
|
|
15
|
+
alpha3
|
|
16
|
+
name
|
|
17
|
+
my_name
|
|
18
|
+
].freeze
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
@data = data
|
|
18
|
-
end
|
|
20
|
+
INDICES = %w[pcode iso alpha3].freeze
|
|
19
21
|
|
|
22
|
+
def initialize(data)
|
|
23
|
+
@data = data
|
|
24
|
+
end
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
# define getters
|
|
27
|
+
ATTRIBUTES.each do |attr|
|
|
28
|
+
define_method attr do
|
|
29
|
+
@data[attr]
|
|
30
|
+
end
|
|
31
|
+
end
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
# allow the values to be retrieved as an array
|
|
34
|
+
def [](key)
|
|
35
|
+
@data[key]
|
|
36
|
+
end
|
|
32
37
|
|
|
33
38
|
def districts
|
|
34
|
-
|
|
39
|
+
Nayyar::District.of_state self
|
|
35
40
|
end
|
|
36
41
|
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
end
|
|
105
|
-
end
|
|
42
|
+
class << self
|
|
43
|
+
def all
|
|
44
|
+
states
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def find_by(query)
|
|
48
|
+
key = get_key(query)
|
|
49
|
+
(index = send("#{key}_index".to_sym).index(query[key])) && states[index]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def find_by!(query)
|
|
53
|
+
if (state = find_by(query))
|
|
54
|
+
state
|
|
55
|
+
else
|
|
56
|
+
key = get_key(query)
|
|
57
|
+
raise Nayyar::StateNotFound, "Cannot find State with given #{key}: #{query[key]}"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
INDICES.each do |index|
|
|
62
|
+
define_method("find_by_#{index}") do |query|
|
|
63
|
+
find_by(index.to_sym => query)
|
|
64
|
+
end
|
|
65
|
+
define_method("find_by_#{index}!") do |query|
|
|
66
|
+
find_by!(index.to_sym => query)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
protected
|
|
71
|
+
|
|
72
|
+
def states
|
|
73
|
+
unless @states
|
|
74
|
+
indices = INDICES.inject({}) { |memo, index| memo.merge index => [] }
|
|
75
|
+
@states = data.map do |state_data|
|
|
76
|
+
new(state_data).tap { |state| build_indices(indices, state) }
|
|
77
|
+
end
|
|
78
|
+
INDICES.each { |index| class_variable_set(:"@@#{index}_index", indices[index]) } # rubocop:disable Style/ClassVars
|
|
79
|
+
end
|
|
80
|
+
@states
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def data
|
|
84
|
+
YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'states.yml'))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def build_indices(indices, state)
|
|
88
|
+
INDICES.each { |index| indices[index] << state.send(index) }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
## define private methods for internal use of indexed array
|
|
92
|
+
INDICES.each do |index|
|
|
93
|
+
define_method("#{index}_index") do
|
|
94
|
+
states
|
|
95
|
+
class_variable_get(:"@@#{index}_index")
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
## return the index for query given to find_by/find_by! method
|
|
100
|
+
def get_key(data)
|
|
101
|
+
keys = data.keys
|
|
102
|
+
if keys.length != 1 || INDICES.none? { |key| key.to_sym == keys.first.to_sym }
|
|
103
|
+
raise ArgumentError, "`find_by` accepts only one of #{INDICES.join(' or ')} as argument. None provided"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
keys.first
|
|
107
|
+
end
|
|
108
|
+
end
|
|
106
109
|
end
|
|
107
|
-
|
|
110
|
+
|
|
111
|
+
class Nayyar::StateNotFound < StandardError; end
|