nayyar 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2b73ccd19ba2f9ea0124304341c983edb9363ec0
4
- data.tar.gz: e9c7e00c72547ed8904b1db7d03433e2185e0418
2
+ SHA256:
3
+ metadata.gz: 8877dd83a8a2ba5c4efc6ce025c6afc79e072d2361008d02e2cc4eebad49c027
4
+ data.tar.gz: dd7f9602d9cb86f1447017101f014b5c652d47b71e7394363260a476b3d33ccd
5
5
  SHA512:
6
- metadata.gz: 475405f6bed972be38687570dc18cfff97354c499de93f33c096010a447aa3cd40478ec5c07ca05ebcdf1a3a1b4eee90f86ba8f1d67e60062829d347d33f30a4
7
- data.tar.gz: 10476a3b59b57050c366257fdc422fc3819eab5fc1add2305dc9c9b5bd8e9012457bd443dce26aa3919b3093c38dd016aacc0b60c445baa68ba845d9c0d04f54
6
+ metadata.gz: 2ac406af5aab9869fcaeb709c8a08f25105b62c8ba1528b0ef836b15ba4c923d629e2431a970b761ae552ecd32f5ae41e69dfbad557e340eaf6e02257017c8ee
7
+ data.tar.gz: df07064162e9a98507b12a075b5ae17cd690453587bd20c2bd08107fcf7730483de97e52a7b79cd189ae36a911b9187c7ab31e700377e6b76b1442f25b0c07a3
@@ -0,0 +1,35 @@
1
+ # Use the latest 2.1 version of CircleCI pipeline process engine.
2
+ # See: https://circleci.com/docs/2.0/configuration-reference
3
+ version: 2.1
4
+
5
+ # Orbs are reusable packages of CircleCI configuration that you may share across projects, enabling you to create encapsulated, parameterized commands, jobs, and executors that can be used across multiple projects.
6
+ # See: https://circleci.com/docs/2.0/orb-intro/
7
+ orbs:
8
+ ruby: circleci/ruby@1.4.0
9
+
10
+ # Define a job to be invoked later in a workflow.
11
+ # See: https://circleci.com/docs/2.0/configuration-reference/#jobs
12
+ jobs:
13
+ rspec:
14
+ docker:
15
+ - image: cimg/ruby:3.1.0
16
+ executor: ruby/default
17
+ steps:
18
+ - checkout
19
+ - run:
20
+ name: Which bundler?
21
+ command: bundle -v
22
+ - run:
23
+ name: bundle install
24
+ command: bundle install
25
+ - run:
26
+ name: rspec
27
+ command: rspec
28
+
29
+ # Invoke jobs via workflows
30
+ # See: https://circleci.com/docs/2.0/configuration-reference/#workflows
31
+ workflows:
32
+ rspec: # This is the name of the workflow, feel free to change it to better match your workflow.
33
+ # Inside the workflow, you define the jobs you want to run.
34
+ jobs:
35
+ - rspec
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ AllCops:
2
+ NewCops: enable
3
+
4
+ Style/ClassAndModuleChildren:
5
+ EnforcedStyle: compact
6
+
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - 'nayyar.gemspec'
10
+ - 'spec/*.rb'
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in nayyar.gemspec
data/README.md CHANGED
@@ -1,13 +1,12 @@
1
1
  # Nayyar / နေရာ
2
- [![Build Status](https://travis-ci.org/mmhan/nayyar.png)](https://travis-ci.org/mmhan/nayyar.png)
3
- [![Gem Version](https://badge.fury.io/rb/nayyar.svg)](http://badge.fury.io/rb/nayyar)
2
+ [![CircleCI](https://circleci.com/gh/mmhan/nayyar/tree/master.svg?style=svg)](https://circleci.com/gh/mmhan/nayyar/tree/master)
4
3
 
5
4
  Nayyar is created with the intent of providing basic access to State/Regions, Districts or Townships of Myanmar, based on standards of Myanmar's country-wide census of 2014.
6
5
 
7
6
  15 States are indexed by MIMU's pcode, ISO3166-2:MM and alpha3 codes used in plate numbers by transportation authority.
8
7
  74 Districts and 413 Townships are indexed by MIMU's pcode.
9
8
 
10
- The current version is `0.1.0` and it uses [Semantic Versioning](http://semver.org/)
9
+ The current version is [![Gem Version](https://badge.fury.io/rb/nayyar.svg)](http://badge.fury.io/rb/nayyar) and it uses [Semantic Versioning](http://semver.org/)
11
10
 
12
11
  ## Installation
13
12
 
@@ -134,29 +133,20 @@ Nayyar::Township.find_by(pcode:"MMR013017")
134
133
  Find the district that a township belongs to using
135
134
 
136
135
  ```ruby
137
- Nayyar::Township.find_by(pcode:"MMR013017").township
136
+ Nayyar::Township.find_by(pcode:"MMR013017").district
138
137
 
139
138
  # => <Nayyar::District:0x007fb8a5ad14d8 @data={:pcode=>"MMR013D002", :name=>"Yangon (East)", :state=>"MMR013"}>
140
139
  ```
141
140
 
142
141
  Use any of the `find_by` or `find_by_**index_name**` with a bang `!` to trigger `Nayyar::TownshipNotFound` error.
143
- <!--
142
+
144
143
  ## Development
145
144
 
146
145
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
147
146
 
148
147
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
149
- -->
150
-
151
- ## Goals
152
- 1. Make it work
153
- - Create just enough API to allow creations of dropdowns/multiple-select options in HTML ✓
154
- 2. Make it right
155
- - Remove duplicate/similar codes across the three main classes using metaprogramming
156
- 3. Make it fast
157
- - Optimize memory footprint by refactoring the way data is stored/read/used
158
148
 
159
- If you feel that I have missed out your use-case or if you wanna add other goals, objectives [submit an issue](https://github.com/mmhan/nayyar/issues/new) so that I can plan it in.
149
+ If you feel that I have missed out your use-case or find an issue with this gem [please submit an issue](https://github.com/mmhan/nayyar/issues/new).
160
150
 
161
151
  ## Contributing
162
152
 
@@ -164,4 +154,100 @@ If you feel that I have missed out your use-case or if you wanna add other goals
164
154
  2. Create your feature branch (`git checkout -b feature/my-new-feature`)
165
155
  3. Commit your changes (`git commit -am 'Add some feature'`)
166
156
  4. Push to the branch (`git push origin feature/my-new-feature`)
167
- 5. Create a new Pull Request
157
+ 5. Create a new Pull Request
158
+
159
+
160
+ ## Documentation
161
+
162
+ Since, this is a pretty small library, a documentation hasn't been created yet. Nonetheless, here's the rspec output with documentation format.
163
+
164
+ ```
165
+ Nayyar
166
+ has a version number
167
+
168
+ Nayyar::State
169
+ class methods
170
+ .all
171
+ should be a kind of Array
172
+ contains hashes of state data
173
+ .find_by
174
+ returns state when it finds it
175
+ will return nil if state is not found
176
+ will raise ArgumentError if no argument is provided
177
+ will raise ArgumentError if wrong argument is provided
178
+ .find_by!
179
+ should be a kind of Nayyar::State
180
+ will raise error if the state isn't found
181
+ .find_by_**indices**
182
+ returns state when it finds it
183
+ returns nil when it can't be found
184
+ .find_by_**indices**!
185
+ returns state when it finds it
186
+ raise error when it can't be found
187
+ #initialize
188
+ allows to create a state by providing hash data
189
+ attributes
190
+ allows its attributes to be accessed by methods
191
+ allows its attributes to be accessed as keys
192
+ #districts
193
+ return a list of districts under given state
194
+
195
+ Nayyar::District
196
+ class methods
197
+ .all
198
+ should return all districts
199
+ .of_state
200
+ return a list of districts under given state
201
+ .find_by
202
+ should be a kind of Nayyar::District
203
+ returns nil if it's not found
204
+ .find_by!
205
+ should be a kind of Nayyar::District
206
+ raise error if it's not found
207
+ .find_by_**indices**
208
+ returns state when it finds it
209
+ returns nil when it can't be found
210
+ .find_by_**indices**!
211
+ returns state when it finds it
212
+ raise error when it can't be found
213
+ #initialize
214
+ allows to create a district by providing hash data
215
+ attributes
216
+ allows its attributes to be accessed by methods
217
+ allows its attributes to be accessed as keys
218
+ #state
219
+ should be a kind of Nayyar::State
220
+ expect it to be a correct state
221
+ .townships
222
+ return a list of districts under given district
223
+
224
+ Nayyar::Township
225
+ class methods
226
+ .all
227
+ should return all township
228
+ .of_district
229
+ return a list of districts under given district
230
+ .find_by
231
+ should be a kind of Nayyar::Township
232
+ returns nil if it's not found
233
+ .find_by!
234
+ should be a kind of Nayyar::Township
235
+ raise error if it's not found
236
+ .find_by_**indices**
237
+ returns state when it finds it
238
+ returns nil when it can't be found
239
+ .find_by_**indices**!
240
+ returns state when it finds it
241
+ raise error when it can't be found
242
+ #initialize
243
+ allows to create a township by providing hash data
244
+ attributes
245
+ allows its attributes to be accessed by methods
246
+ allows its attributes to be accessed as keys
247
+ #district
248
+ should be a kind of Nayyar::District
249
+ expect it to be a correct state
250
+
251
+ Finished in 0.1254 seconds (files took 0.1826 seconds to load)
252
+ 48 examples, 0 failures
253
+ ```
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "nayyar"
4
+ require 'bundler/setup'
5
+ require 'nayyar'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "nayyar"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start
data/lib/data/extract.rb CHANGED
@@ -1,3 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extract data from locations.csv which will be the source of data for the gem
4
+ # and create states.yml, districts.yml and townships.yml which will be read at runtime
5
+
1
6
  require 'csv'
2
7
  require 'pry'
3
8
  require 'psych'
@@ -6,14 +11,14 @@ states = []
6
11
  districts = []
7
12
  townships = []
8
13
 
9
- CSV.foreach("locations.csv") do |iso, s_pcode, alpha3, state, d_pcode, district, t_pcode, township|
10
- states << {iso: iso, pcode: s_pcode, alpha3: alpha3, name: state}
11
- districts << {pcode: d_pcode, name: district, state: s_pcode} unless d_pcode.nil?
12
- townships << {pcode: t_pcode, name: township, district: d_pcode} unless t_pcode.nil?
14
+ CSV.foreach('locations.csv') do |iso, s_pcode, alpha3, state, d_pcode, district, t_pcode, township| # rubocop:disable Metrics/ParameterLists
15
+ states << { iso: iso, pcode: s_pcode, alpha3: alpha3, name: state }
16
+ districts << { pcode: d_pcode, name: district, state: s_pcode } unless d_pcode.nil?
17
+ townships << { pcode: t_pcode, name: township, district: d_pcode } unless t_pcode.nil?
13
18
  end
14
19
  states.uniq! { |state| state[:pcode] }
15
- districts.uniq! { |districts| districts[:pcode] }
20
+ districts.uniq! { |district| district[:pcode] }
16
21
 
17
- File.open("states.yml", "w") { |f| f.write states.to_yaml }
18
- File.open("districts.yml", "w") { |f| f.write districts.to_yaml }
19
- File.open("townships.yml", "w") { |f| f.write townships.to_yaml }
22
+ File.write('states.yml', states.to_yaml)
23
+ File.write('districts.yml', districts.to_yaml)
24
+ File.write('townships.yml', townships.to_yaml)
@@ -1,122 +1,129 @@
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
+ ].freeze
14
+
15
+ INDICES = %w[pcode].freeze
16
+
17
+ def initialize(data)
18
+ @data = data
19
+ end
20
+
21
+ # define getters
22
+ ATTRIBUTES.each do |attr|
23
+ define_method attr do
24
+ @data[attr]
25
+ end
26
+ end
27
+
28
+ # allow the values to be retrieved as an array
29
+ def [](key)
30
+ if ATTRIBUTES.include? key
31
+ @data[key]
32
+ elsif key.to_sym == :state
33
+ state
34
+ end
35
+ end
36
+
37
+ def state
38
+ Nayyar::State.find_by_pcode(@data[:state])
39
+ end
40
+
41
+ def townships
42
+ Nayyar::Township.of_district(self)
43
+ end
44
+
45
+ class << self
46
+ def all
47
+ districts
48
+ end
49
+
50
+ def of_state(state)
51
+ state_pcode = state.pcode
52
+ districts = self.districts
53
+ @state_index[state_pcode].map do |index|
54
+ districts[index]
55
+ end
56
+ end
57
+
58
+ def find_by(query)
59
+ key = get_key(query)
60
+ (index = send("#{key}_index".to_sym).index(query[key])) && districts[index]
61
+ end
62
+
63
+ def find_by!(query)
64
+ if (district = find_by(query))
65
+ district
66
+ else
67
+ key = get_key(query)
68
+ raise Nayyar::DistrictNotFound, "Cannot find State with given #{key}: #{query[key]}"
69
+ end
70
+ end
71
+
72
+ INDICES.each do |index|
73
+ define_method("find_by_#{index}") do |query|
74
+ find_by(index.to_sym => query)
75
+ end
76
+ define_method("find_by_#{index}!") do |query|
77
+ find_by!(index.to_sym => query)
78
+ end
79
+ end
80
+
81
+ protected
82
+
83
+ def districts
84
+ unless @districts
85
+ indices = INDICES.inject({}) { |memo, index| memo.merge index => [] }
86
+ @districts = data.each_with_index.map do |district_data, i|
87
+ new(district_data).tap { |district| build_indices(indices, district, district_data[:state], i) }
88
+ end
89
+
90
+ INDICES.each do |index|
91
+ class_variable_set("@@#{index}_index", indices[index]) # rubocop:disable Style/ClassVars
92
+ end
93
+ end
94
+ @districts
95
+ end
96
+
97
+ def data
98
+ YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'districts.yml'))
99
+ end
100
+
101
+ def build_indices(indices, district, state_pcode, reference_index)
102
+ INDICES.each do |index|
103
+ indices[index] << district.send(index)
104
+ end
105
+ @state_index[state_pcode] ||= []
106
+ @state_index[state_pcode] << reference_index
107
+ end
108
+
109
+ ## define private methods for internal use of indexed array
110
+ INDICES.each do |index|
111
+ define_method("#{index}_index") do
112
+ districts
113
+ class_variable_get("@@#{index}_index")
114
+ end
115
+ end
116
+
117
+ ## return the index for query given to find_by/find_by! method
118
+ def get_key(data)
119
+ keys = data.keys
120
+ if keys.length != 1 || INDICES.none? { |key| key.to_sym == keys.first.to_sym }
121
+ raise ArgumentError, "`find_by` accepts only one of #{INDICES.join(' or ')} as argument. none provided"
122
+ end
123
+
124
+ keys.first
125
+ end
126
+ end
121
127
  end
122
- class Nayyar::DistrictNotFound < StandardError; end;
128
+
129
+ class Nayyar::DistrictNotFound < StandardError; end
data/lib/nayyar/state.rb CHANGED
@@ -1,107 +1,110 @@
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
+ ].freeze
15
18
 
16
- def initialize(data)
17
- @data = data
18
- end
19
+ INDICES = %w[pcode iso alpha3].freeze
19
20
 
21
+ def initialize(data)
22
+ @data = data
23
+ end
20
24
 
21
- # define getters
22
- attributes.each do |attr|
23
- define_method attr do
24
- @data[attr]
25
- end
26
- end
25
+ # define getters
26
+ ATTRIBUTES.each do |attr|
27
+ define_method attr do
28
+ @data[attr]
29
+ end
30
+ end
27
31
 
28
- # allow the values to be retrieved as an array
29
- def [](key)
30
- @data[key]
31
- end
32
+ # allow the values to be retrieved as an array
33
+ def [](key)
34
+ @data[key]
35
+ end
32
36
 
33
37
  def districts
34
- Nayyar::District.of_state self
38
+ Nayyar::District.of_state self
35
39
  end
36
40
 
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
41
+ class << self
42
+ def all
43
+ states
44
+ end
45
+
46
+ def find_by(query)
47
+ key = get_key(query)
48
+ (index = send("#{key}_index".to_sym).index(query[key])) && states[index]
49
+ end
50
+
51
+ def find_by!(query)
52
+ if (state = find_by(query))
53
+ state
54
+ else
55
+ key = get_key(query)
56
+ raise Nayyar::StateNotFound, "Cannot find State with given #{key}: #{query[key]}"
57
+ end
58
+ end
59
+
60
+ INDICES.each do |index|
61
+ define_method("find_by_#{index}") do |query|
62
+ find_by(index.to_sym => query)
63
+ end
64
+ define_method("find_by_#{index}!") do |query|
65
+ find_by!(index.to_sym => query)
66
+ end
67
+ end
68
+
69
+ protected
70
+
71
+ def states
72
+ unless @states
73
+ indices = INDICES.inject({}) { |memo, index| memo.merge index => [] }
74
+ @states = data.map do |state_data|
75
+ new(state_data).tap { |state| build_indices(indices, state) }
76
+ end
77
+ INDICES.each { |index| class_variable_set(:"@@#{index}_index", indices[index]) } # rubocop:disable Style/ClassVars
78
+ end
79
+ @states
80
+ end
81
+
82
+ def data
83
+ YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'states.yml'))
84
+ end
85
+
86
+ def build_indices(indices, state)
87
+ INDICES.each { |index| indices[index] << state.send(index) }
88
+ end
89
+
90
+ ## define private methods for internal use of indexed array
91
+ INDICES.each do |index|
92
+ define_method("#{index}_index") do
93
+ states
94
+ class_variable_get(:"@@#{index}_index")
95
+ end
96
+ end
97
+
98
+ ## return the index for query given to find_by/find_by! method
99
+ def get_key(data)
100
+ keys = data.keys
101
+ if keys.length != 1 || INDICES.none? { |key| key.to_sym == keys.first.to_sym }
102
+ raise ArgumentError, "`find_by` accepts only one of #{INDICES.join(' or ')} as argument. None provided"
103
+ end
104
+
105
+ keys.first
106
+ end
107
+ end
106
108
  end
107
- class Nayyar::StateNotFound < StandardError; end;
109
+
110
+ class Nayyar::StateNotFound < StandardError; end
@@ -1,119 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Represents a Township in Myanmar
1
4
  class Nayyar::Township
2
- attr_reader :data
3
-
4
- @@townships = nil
5
- @@district_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 :district == key.to_sym
28
- district
29
- end
30
- end
31
-
32
- def district
33
- Nayyar::District.find_by_pcode(@data[:district])
34
- end
35
-
36
- class << self
37
- INDICES = %w(pcode)
38
-
39
- def all
40
- townships
41
- end
42
-
43
- def of_district(district)
44
- state_pcode = district.pcode
45
- townships = self.townships
46
- @@district_index[state_pcode].map do |index|
47
- townships[index]
48
- end
49
- end
50
-
51
- def find_by(query)
52
- key = get_key(query)
53
- (index = send("#{key}_index".to_sym).index(query[key])) && townships[index]
54
- end
55
-
56
- def find_by!(query)
57
- if district = find_by(query)
58
- district
59
- else
60
- key = get_key(query)
61
- raise Nayyar::TownshipNotFound.new("Cannot find State with given #{key}: #{query[key]}")
62
- end
63
- end
64
-
65
- INDICES.each do |index|
66
- define_method("find_by_#{index}") do |query|
67
- find_by(index.to_sym => query)
68
- end
69
- define_method("find_by_#{index}!") do |query|
70
- find_by!(index.to_sym => query)
71
- end
72
- end
73
-
74
- protected
75
- def townships
76
- unless @@townships
77
- require "yaml"
78
- data = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'townships.yml'))
79
- indices = INDICES.inject({}) { |memo, index| memo.merge index => [] }
80
- i = 0
81
- @@townships= data.map do |township_row|
82
- district_pcode = township_row[:district]
83
- district = new(township_row)
84
- INDICES.each do |index|
85
- indices[index] << district.send(index)
86
- end
87
- @@district_index[district_pcode] ||= []
88
- @@district_index[district_pcode] << i
89
- i += 1
90
- district
91
- end
92
-
93
-
94
- INDICES.each do |index|
95
- class_variable_set("@@#{index}_index", indices[index])
96
- end
97
- end
98
- @@townships
99
- end
100
-
101
- ## define private methods for internal use of indexed array
102
- INDICES.each do |index|
103
- define_method("#{index}_index") do
104
- townships
105
- class_variable_get("@@#{index}_index")
106
- end
107
- end
108
-
109
- ## return the index for query given to find_by/find_by! method
110
- def get_key(data)
111
- keys = data.keys
112
- if keys.length != 1 || INDICES.none? { |key| key.to_sym == keys.first.to_sym }
113
- raise ArgumentError.new("`find_by` accepts only one of #{INDICES.join(" or ")} as argument. none provided")
114
- end
115
- keys.first
116
- end
117
- end
5
+ attr_reader :data
6
+
7
+ @townships = nil
8
+ @district_index = {}
9
+
10
+ ATTRIBUTES = %i[
11
+ pcode
12
+ name
13
+ ].freeze
14
+
15
+ INDICES = %w[pcode].freeze
16
+
17
+ def initialize(data)
18
+ @data = data
19
+ end
20
+
21
+ # define getters
22
+ ATTRIBUTES.each do |attr|
23
+ define_method attr do
24
+ @data[attr]
25
+ end
26
+ end
27
+
28
+ # allow the values to be retrieved as an array
29
+ def [](key)
30
+ if ATTRIBUTES.include? key
31
+ @data[key]
32
+ elsif key.to_sym == :district
33
+ district
34
+ end
35
+ end
36
+
37
+ def district
38
+ Nayyar::District.find_by_pcode(@data[:district])
39
+ end
40
+
41
+ class << self
42
+ def all
43
+ townships
44
+ end
45
+
46
+ def of_district(district)
47
+ state_pcode = district.pcode
48
+ townships = self.townships
49
+ @district_index[state_pcode].map do |index|
50
+ townships[index]
51
+ end
52
+ end
53
+
54
+ def find_by(query)
55
+ key = get_key(query)
56
+ (index = send("#{key}_index".to_sym).index(query[key])) && townships[index]
57
+ end
58
+
59
+ def find_by!(query)
60
+ if (district = find_by(query))
61
+ district
62
+ else
63
+ key = get_key(query)
64
+ raise Nayyar::TownshipNotFound, "Cannot find State with given #{key}: #{query[key]}"
65
+ end
66
+ end
67
+
68
+ INDICES.each do |index|
69
+ define_method("find_by_#{index}") do |query|
70
+ find_by(index.to_sym => query)
71
+ end
72
+ define_method("find_by_#{index}!") do |query|
73
+ find_by!(index.to_sym => query)
74
+ end
75
+ end
76
+
77
+ protected
78
+
79
+ def townships
80
+ unless @townships
81
+ indices = INDICES.inject({}) { |memo, index| memo.merge index => [] }
82
+ @townships = data.each_with_index.map do |township_row, i|
83
+ new(township_row).tap { |township| build_indices(indices, township, township_row[:district], i) }
84
+ end
85
+
86
+ INDICES.each do |index|
87
+ class_variable_set(:"@@#{index}_index", indices[index]) # rubocop:disable Style/ClassVars
88
+ end
89
+ end
90
+ @townships
91
+ end
92
+
93
+ def data
94
+ YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'townships.yml'))
95
+ end
96
+
97
+ def build_indices(indices, township, district_pcode, reference_index)
98
+ INDICES.each do |index|
99
+ indices[index] << township.send(index)
100
+ end
101
+ @district_index[district_pcode] ||= []
102
+ @district_index[district_pcode] << reference_index
103
+ end
104
+
105
+ ## define private methods for internal use of indexed array
106
+ INDICES.each do |index|
107
+ define_method("#{index}_index") do
108
+ townships
109
+ class_variable_get("@@#{index}_index")
110
+ end
111
+ end
112
+
113
+ ## return the index for query given to find_by/find_by! method
114
+ def get_key(data)
115
+ keys = data.keys
116
+ if keys.length != 1 || INDICES.none? { |key| key.to_sym == keys.first.to_sym }
117
+ raise ArgumentError, "`find_by` accepts only one of #{INDICES.join(' or ')} as argument. none provided"
118
+ end
119
+
120
+ keys.first
121
+ end
122
+ end
118
123
  end
119
- class Nayyar::TownshipNotFound < StandardError; end;
124
+
125
+ class Nayyar::TownshipNotFound < StandardError; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Nayyar
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.2'
3
5
  end
data/lib/nayyar.rb CHANGED
@@ -1,7 +1,11 @@
1
- require "nayyar/version"
2
- require "nayyar/state"
3
- require "nayyar/district"
4
- require "nayyar/township"
1
+ # frozen_string_literal: true
5
2
 
3
+ require 'yaml'
4
+ require 'nayyar/version'
5
+ require 'nayyar/state'
6
+ require 'nayyar/district'
7
+ require 'nayyar/township'
8
+
9
+ # Top-level module for Nayyar.
6
10
  module Nayyar
7
- end
11
+ end
data/nayyar.gemspec CHANGED
@@ -1,39 +1,43 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'nayyar/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "nayyar"
8
+ spec.name = 'nayyar'
8
9
  spec.version = Nayyar::VERSION
9
- spec.authors = ["mmhan"]
10
- spec.email = ["mmhan2u@gmail.com"]
10
+ spec.authors = ['mmhan']
11
+ spec.email = ['mmhan2u@gmail.com']
12
+ spec.required_ruby_version = '>= 2.7.0'
11
13
 
12
- spec.summary = %q{Nayyar gives you access to State/Regions, Districts and Townships of Myanmar.}
13
- spec.description = %q{Nayyar is created with the intent of providing basic access to State/Regions, Districts or Townships of Myanmar, based on standards of Myanmar's country-wide census of 2014.
14
+ spec.summary = 'Nayyar gives you access to State/Regions, Districts and Townships of Myanmar.'
15
+ spec.description = "Nayyar is created with the intent of providing basic access to State/Regions, Districts or
16
+ Townships of Myanmar, based on standards of Myanmar's country-wide census of 2014.
14
17
 
15
18
  15 States are indexed by MIMU's pcode, ISO3166-2:MM and alpha3 codes used in plate numbers by transportation authority.
16
19
  74 Districts and 413 Townships are indexed by MIMU's pcode.
17
20
 
18
- The current version is `0.1.0` and it uses [Semantic Versioning](http://semver.org/)}
19
- spec.homepage = "https://github.com/mmhan/nayyar"
20
- spec.license = "MIT"
21
+ Uses [Semantic Versioning](http://semver.org/)"
22
+ spec.homepage = 'https://github.com/mmhan/nayyar'
23
+ spec.license = 'MIT'
21
24
 
22
25
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
23
26
  # delete this section to allow pushing this gem to any host.
24
- if spec.respond_to?(:metadata)
25
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
26
- else
27
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
28
- end
27
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
28
+
29
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
30
+ spec.metadata['rubygems_mfa_required'] = 'true'
29
31
 
30
32
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
- spec.bindir = "exe"
33
+ spec.bindir = 'exe'
32
34
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
35
+ spec.require_paths = ['lib']
34
36
 
35
- spec.add_development_dependency "bundler", "~> 1.9"
36
- spec.add_development_dependency "rake", "~> 10.0"
37
- spec.add_development_dependency 'rspec', '>= 3'
37
+ spec.add_development_dependency 'bundler', '>= 2.3.10'
38
38
  spec.add_development_dependency 'pry', '>= 0.10'
39
+ spec.add_development_dependency 'rake', '>= 12.3.3'
40
+ spec.add_development_dependency 'rspec', '>= 3'
41
+ spec.add_development_dependency 'rubocop', '~> 1.27.0'
42
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.9.0'
39
43
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nayyar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmhan
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-20 00:00:00.000000000 Z
11
+ date: 2022-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.9'
19
+ version: 2.3.10
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.9'
26
+ version: 2.3.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '10.0'
47
+ version: 12.3.3
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '10.0'
54
+ version: 12.3.3
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -53,34 +67,51 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '3'
55
69
  - !ruby/object:Gem::Dependency
56
- name: pry
70
+ name: rubocop
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ">="
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '0.10'
75
+ version: 1.27.0
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ">="
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: '0.10'
82
+ version: 1.27.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.9.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.9.0
69
97
  description: |-
70
- Nayyar is created with the intent of providing basic access to State/Regions, Districts or Townships of Myanmar, based on standards of Myanmar's country-wide census of 2014.
98
+ Nayyar is created with the intent of providing basic access to State/Regions, Districts or
99
+ Townships of Myanmar, based on standards of Myanmar's country-wide census of 2014.
71
100
 
72
101
  15 States are indexed by MIMU's pcode, ISO3166-2:MM and alpha3 codes used in plate numbers by transportation authority.
73
102
  74 Districts and 413 Townships are indexed by MIMU's pcode.
74
103
 
75
- The current version is `0.1.0` and it uses [Semantic Versioning](http://semver.org/)
104
+ Uses [Semantic Versioning](http://semver.org/)
76
105
  email:
77
106
  - mmhan2u@gmail.com
78
107
  executables: []
79
108
  extensions: []
80
109
  extra_rdoc_files: []
81
110
  files:
111
+ - ".circleci/config.yml"
82
112
  - ".gitignore"
83
113
  - ".rspec"
114
+ - ".rubocop.yml"
84
115
  - ".travis.yml"
85
116
  - Gemfile
86
117
  - LICENSE.txt
@@ -105,7 +136,8 @@ licenses:
105
136
  - MIT
106
137
  metadata:
107
138
  allowed_push_host: https://rubygems.org
108
- post_install_message:
139
+ rubygems_mfa_required: 'true'
140
+ post_install_message:
109
141
  rdoc_options: []
110
142
  require_paths:
111
143
  - lib
@@ -113,16 +145,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
145
  requirements:
114
146
  - - ">="
115
147
  - !ruby/object:Gem::Version
116
- version: '0'
148
+ version: 2.7.0
117
149
  required_rubygems_version: !ruby/object:Gem::Requirement
118
150
  requirements:
119
151
  - - ">="
120
152
  - !ruby/object:Gem::Version
121
153
  version: '0'
122
154
  requirements: []
123
- rubyforge_project:
124
- rubygems_version: 2.2.3
125
- signing_key:
155
+ rubygems_version: 3.3.11
156
+ signing_key:
126
157
  specification_version: 4
127
158
  summary: Nayyar gives you access to State/Regions, Districts and Townships of Myanmar.
128
159
  test_files: []