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