nayyar 0.1.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.
data/lib/nayyar.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "nayyar/version"
2
+ require "nayyar/state"
3
+ require "nayyar/district"
4
+ require "nayyar/township"
5
+
6
+ module Nayyar
7
+ end
@@ -0,0 +1,122 @@
1
+ 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
121
+ end
122
+ class Nayyar::DistrictNotFound < StandardError; end;
@@ -0,0 +1,107 @@
1
+ class Nayyar::State
2
+ attr_reader :data
3
+
4
+ @@states = nil
5
+ @@pcode_index = []
6
+ @@iso_index = []
7
+ @@alpha3_index = []
8
+
9
+ attributes = [
10
+ :pcode,
11
+ :iso,
12
+ :alpha3,
13
+ :name
14
+ ]
15
+
16
+ def initialize(data)
17
+ @data = data
18
+ end
19
+
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
+ @data[key]
31
+ end
32
+
33
+ def districts
34
+ Nayyar::District.of_state self
35
+ end
36
+
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
106
+ end
107
+ class Nayyar::StateNotFound < StandardError; end;
@@ -0,0 +1,119 @@
1
+ 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
118
+ end
119
+ class Nayyar::TownshipNotFound < StandardError; end;
@@ -0,0 +1,3 @@
1
+ module Nayyar
2
+ VERSION = "0.1.0"
3
+ end
data/nayyar.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nayyar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nayyar"
8
+ spec.version = Nayyar::VERSION
9
+ spec.authors = ["mmhan"]
10
+ spec.email = ["mmhan2u@gmail.com"]
11
+
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
+
15
+ 15 States are indexed by MIMU's pcode, ISO3166-2:MM and alpha3 codes used in plate numbers by transportation authority.
16
+ 74 Districts and 413 Townships are indexed by MIMU's pcode.
17
+
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
+
22
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
23
+ # 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
29
+
30
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.9"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency 'rspec', '>= 3'
38
+ spec.add_development_dependency 'pry', '>= 0.10'
39
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nayyar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mmhan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ 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.
71
+
72
+ 15 States are indexed by MIMU's pcode, ISO3166-2:MM and alpha3 codes used in plate numbers by transportation authority.
73
+ 74 Districts and 413 Townships are indexed by MIMU's pcode.
74
+
75
+ The current version is `0.1.0` and it uses [Semantic Versioning](http://semver.org/)
76
+ email:
77
+ - mmhan2u@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".rspec"
84
+ - ".travis.yml"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - lib/data/.gitkeep
92
+ - lib/data/districts.yml
93
+ - lib/data/extract.rb
94
+ - lib/data/locations.csv
95
+ - lib/data/states.yml
96
+ - lib/data/townships.yml
97
+ - lib/nayyar.rb
98
+ - lib/nayyar/district.rb
99
+ - lib/nayyar/state.rb
100
+ - lib/nayyar/township.rb
101
+ - lib/nayyar/version.rb
102
+ - nayyar.gemspec
103
+ homepage: https://github.com/mmhan/nayyar
104
+ licenses:
105
+ - MIT
106
+ metadata:
107
+ allowed_push_host: https://rubygems.org
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Nayyar gives you access to State/Regions, Districts and Townships of Myanmar.
128
+ test_files: []