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,119 +1,126 @@
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
+ 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 == :district
34
+ district
35
+ end
36
+ end
37
+
38
+ def district
39
+ Nayyar::District.find_by_pcode(@data[:district])
40
+ end
41
+
42
+ class << self
43
+ def all
44
+ townships
45
+ end
46
+
47
+ def of_district(district)
48
+ state_pcode = district.pcode
49
+ townships = self.townships
50
+ @district_index[state_pcode].map do |index|
51
+ townships[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])) && townships[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::TownshipNotFound, "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
+
80
+ def townships
81
+ unless @townships
82
+ indices = INDICES.inject({}) { |memo, index| memo.merge index => [] }
83
+ @townships = data.each_with_index.map do |township_row, i|
84
+ new(township_row).tap { |township| build_indices(indices, township, township_row[:district], i) }
85
+ end
86
+
87
+ INDICES.each do |index|
88
+ class_variable_set(:"@@#{index}_index", indices[index]) # rubocop:disable Style/ClassVars
89
+ end
90
+ end
91
+ @townships
92
+ end
93
+
94
+ def data
95
+ YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'townships.yml'))
96
+ end
97
+
98
+ def build_indices(indices, township, district_pcode, reference_index)
99
+ INDICES.each do |index|
100
+ indices[index] << township.send(index)
101
+ end
102
+ @district_index[district_pcode] ||= []
103
+ @district_index[district_pcode] << reference_index
104
+ end
105
+
106
+ ## define private methods for internal use of indexed array
107
+ INDICES.each do |index|
108
+ define_method("#{index}_index") do
109
+ townships
110
+ class_variable_get("@@#{index}_index")
111
+ end
112
+ end
113
+
114
+ ## return the index for query given to find_by/find_by! method
115
+ def get_key(data)
116
+ keys = data.keys
117
+ if keys.length != 1 || INDICES.none? { |key| key.to_sym == keys.first.to_sym }
118
+ raise ArgumentError, "`find_by` accepts only one of #{INDICES.join(' or ')} as argument. none provided"
119
+ end
120
+
121
+ keys.first
122
+ end
123
+ end
118
124
  end
119
- class Nayyar::TownshipNotFound < StandardError; end;
125
+
126
+ class Nayyar::TownshipNotFound < StandardError; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Nayyar
2
- VERSION = "0.1.0"
4
+ VERSION = '0.2.0'
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.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmhan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-20 00:00:00.000000000 Z
11
+ date: 2022-08-08 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,41 +67,60 @@ 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
87
118
  - README.md
88
119
  - Rakefile
89
120
  - bin/console
121
+ - bin/seed
90
122
  - bin/setup
123
+ - lib/.DS_Store
91
124
  - lib/data/.gitkeep
92
125
  - lib/data/districts.yml
93
126
  - lib/data/extract.rb
@@ -105,6 +138,7 @@ licenses:
105
138
  - MIT
106
139
  metadata:
107
140
  allowed_push_host: https://rubygems.org
141
+ rubygems_mfa_required: 'true'
108
142
  post_install_message:
109
143
  rdoc_options: []
110
144
  require_paths:
@@ -113,15 +147,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
147
  requirements:
114
148
  - - ">="
115
149
  - !ruby/object:Gem::Version
116
- version: '0'
150
+ version: 2.7.0
117
151
  required_rubygems_version: !ruby/object:Gem::Requirement
118
152
  requirements:
119
153
  - - ">="
120
154
  - !ruby/object:Gem::Version
121
155
  version: '0'
122
156
  requirements: []
123
- rubyforge_project:
124
- rubygems_version: 2.2.3
157
+ rubygems_version: 3.2.22
125
158
  signing_key:
126
159
  specification_version: 4
127
160
  summary: Nayyar gives you access to State/Regions, Districts and Townships of Myanmar.