worlddb-models 2.3.3 → 2.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +2 -2
- data/lib/worlddb/models.rb +1 -1
- data/lib/worlddb/models/name.rb +75 -0
- data/lib/worlddb/models/place.rb +2 -0
- data/lib/worlddb/readers/state_tree.rb +20 -48
- data/lib/worlddb/version.rb +1 -1
- data/test/data/at-austria/3--w-wien/{counties.txt → districts.txt} +0 -0
- data/test/data/at-austria/setups/adm.txt +3 -1
- data/test/data/at-austria/setups/tree.txt +4 -1
- data/test/{test_state_tree_reader_name_finder.rb → test_name_finders.rb} +21 -15
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03a22edaff5cdbe119a10429130a8465dcc5a59c
|
4
|
+
data.tar.gz: c3c71f45eebfbbd56c1a9675838803674050252e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a5b509f87918c6032a1185b5c0d7f349c2abf013e5c6125f1556d455108349dec022b76f05ae92667c822e7bf71409a74c3f120f935a19fa15bf58eef9727c6
|
7
|
+
data.tar.gz: 180eb5093b77c214acdccb0aa9d1ee4c5f699f75cfc0c5d6b4f51c576ec697a14a052135744bc16a44d447b7f3be3009b1abc7abc2bca9e3fd9528e421ec6864
|
data/Manifest.txt
CHANGED
@@ -43,7 +43,7 @@ test/adm/test_read_adm.rb
|
|
43
43
|
test/adm/test_read_tree.rb
|
44
44
|
test/data/at-austria/1--b-burgenland/counties.txt
|
45
45
|
test/data/at-austria/2--n-niederoesterreich/counties.txt
|
46
|
-
test/data/at-austria/3--w-wien/
|
46
|
+
test/data/at-austria/3--w-wien/districts.txt
|
47
47
|
test/data/at-austria/orte.txt
|
48
48
|
test/data/at-austria/setups/adm.txt
|
49
49
|
test/data/at-austria/setups/tree.txt
|
@@ -67,6 +67,7 @@ test/test_model_state.rb
|
|
67
67
|
test/test_model_states_at.rb
|
68
68
|
test/test_model_states_de.rb
|
69
69
|
test/test_models.rb
|
70
|
+
test/test_name_finders.rb
|
70
71
|
test/test_name_parser.rb
|
71
72
|
test/test_parse_city.rb
|
72
73
|
test/test_parse_country.rb
|
@@ -74,4 +75,3 @@ test/test_parse_state.rb
|
|
74
75
|
test/test_report_country.rb
|
75
76
|
test/test_state_tree_reader_at.rb
|
76
77
|
test/test_state_tree_reader_de.rb
|
77
|
-
test/test_state_tree_reader_name_finder.rb
|
data/lib/worlddb/models.rb
CHANGED
data/lib/worlddb/models/name.rb
CHANGED
@@ -9,6 +9,81 @@ class Name < ActiveRecord::Base
|
|
9
9
|
belongs_to :place, class_name: 'Place', foreign_key: 'place_id'
|
10
10
|
|
11
11
|
|
12
|
+
def place_object # returns "typed" place object e.g. state, part, county, muni, city, etc.
|
13
|
+
|
14
|
+
case place_kind
|
15
|
+
when 'STAT' ## state
|
16
|
+
place.state
|
17
|
+
when 'PART' ## part
|
18
|
+
place.part
|
19
|
+
when 'COUN' ## county
|
20
|
+
place.county
|
21
|
+
when 'MUNI' ## muni
|
22
|
+
place.muni
|
23
|
+
when 'CITY' ## city
|
24
|
+
place.city
|
25
|
+
else
|
26
|
+
puts "*** error [Name#place_object] - unknown place_kind #{place_kind}"
|
27
|
+
fail "[Name#place_object] - unknown place_kind #{place_kind}"
|
28
|
+
end # end case
|
29
|
+
end
|
30
|
+
|
31
|
+
#########
|
32
|
+
# finders
|
33
|
+
#
|
34
|
+
# -- search scoped by country
|
35
|
+
|
36
|
+
def self.find_states( name, country_id ) # note: requires country_id (for search scope)
|
37
|
+
Name.joins(
|
38
|
+
:place => :state
|
39
|
+
).where(
|
40
|
+
:name => name,
|
41
|
+
:place_kind => 'STAT',
|
42
|
+
:'states.country_id' => country_id )
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.find_cities( name, country_id ) # note: requires country_id (for search scope) -- add version w/ state_id scope - why?? why not??
|
46
|
+
Name.joins(
|
47
|
+
:place => :city
|
48
|
+
).where(
|
49
|
+
:name => name,
|
50
|
+
:place_kind => 'CITY',
|
51
|
+
:'cities.country_id' => country_id )
|
52
|
+
end
|
53
|
+
|
54
|
+
## -- search scoped by state
|
55
|
+
|
56
|
+
def self.find_parts( name, state_id ) # note requires state_id (for search scope)
|
57
|
+
Name.joins(
|
58
|
+
:place => :part
|
59
|
+
).where(
|
60
|
+
:name => name,
|
61
|
+
:place_kind => 'PART',
|
62
|
+
:'parts.state_id' => state_id )
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.find_counties( name, state_id ) # note requires state_id (for search scope)
|
66
|
+
Name.joins(
|
67
|
+
:place => :county
|
68
|
+
).where(
|
69
|
+
:name => name,
|
70
|
+
:place_kind => 'COUN',
|
71
|
+
:'counties.state_id' => state_id )
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.find_munis( name, state_id ) # note requires state_id (for search scope)
|
75
|
+
Name.joins(
|
76
|
+
:place => :muni
|
77
|
+
).where(
|
78
|
+
:name => name,
|
79
|
+
:place_kind => 'MUNI',
|
80
|
+
:'munis.state_id' => state_id )
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
########
|
85
|
+
# create
|
86
|
+
|
12
87
|
def self.parse( *args )
|
13
88
|
## remove (extract) attribs hash (if last arg is a hash n present)
|
14
89
|
more_attribs = args.last.is_a?(Hash) ? args.pop : {} ## extract_options!
|
data/lib/worlddb/models/place.rb
CHANGED
@@ -8,8 +8,10 @@ class Place < ActiveRecord::Base
|
|
8
8
|
## todo: depending on type
|
9
9
|
## has_one continent, country, state, city etc.
|
10
10
|
has_one :state, class_name: 'State', foreign_key: 'place_id'
|
11
|
+
has_one :part, class_name: 'Part', foreign_key: 'place_id'
|
11
12
|
has_one :county, class_name: 'County', foreign_key: 'place_id'
|
12
13
|
has_one :muni, class_name: 'Muni', foreign_key: 'place_id'
|
14
|
+
has_one :city, class_name: 'City', foreign_key: 'place_id'
|
13
15
|
## add more types..
|
14
16
|
|
15
17
|
end # class Place
|
@@ -36,68 +36,38 @@ class StateTreeReader < ReaderBaseWithMoreAttribs
|
|
36
36
|
puts " #{names.join( ' › ' )}:"
|
37
37
|
puts " key: >#{node.key}<, level: >#{node.level}<, value: >#{node.value}<"
|
38
38
|
|
39
|
-
## use name for lookup - use where() to find - might match more than 1 record!!
|
40
|
-
## todo/fix: check for multiple or no matches!!!
|
41
|
-
## todo/fix: check how to add state.id scope (and country.id scope) etc.
|
42
|
-
## add state.id to name - why? why not?
|
43
|
-
## move finders to name model for reuse (and testing) - why? why not ??
|
44
39
|
if node.level == state_level # 1
|
45
|
-
|
46
|
-
names = Name.joins(
|
47
|
-
:place => :state
|
48
|
-
).find_by(
|
49
|
-
:name => node.value,
|
50
|
-
:place_kind => 'STAT',
|
51
|
-
:'states.country_id' => country.id )
|
52
|
-
rec = if names.nil?
|
53
|
-
nil
|
54
|
-
else
|
55
|
-
names.place.state # get first record; fix: use where instead of find_by etc.
|
56
|
-
end
|
40
|
+
recs = Name.find_states( node.value, country.id )
|
57
41
|
elsif node.level == part_level # 2
|
58
42
|
state = stack[0]
|
59
|
-
|
43
|
+
recs = Name.find_parts( node.value, state.id )
|
60
44
|
elsif node.level == county_level # 2 or 3
|
61
45
|
state = stack[0]
|
62
|
-
|
63
|
-
names = Name.joins(
|
64
|
-
:place => :county
|
65
|
-
).find_by(
|
66
|
-
:name => node.value,
|
67
|
-
:place_kind => 'COUN',
|
68
|
-
:'counties.state_id' => state.id )
|
69
|
-
rec = if names.nil?
|
70
|
-
nil
|
71
|
-
else
|
72
|
-
names.place.county # get first record
|
73
|
-
end
|
46
|
+
recs = Name.find_counties( node.value, state.id )
|
74
47
|
elsif node.level == muni_level # 3 or 4
|
75
48
|
state = stack[0]
|
76
|
-
|
77
|
-
names = Name.joins(
|
78
|
-
:place => :muni
|
79
|
-
).find_by(
|
80
|
-
:name => node.value,
|
81
|
-
:place_kind => 'MUNI',
|
82
|
-
:'munis.state_id' => state.id )
|
83
|
-
rec = if names.nil?
|
84
|
-
nil
|
85
|
-
else
|
86
|
-
names.place.muni # get first record
|
87
|
-
end
|
49
|
+
recs = Name.find_munis( node.value, state.id )
|
88
50
|
elsif node.level == city_level # 4 or 5
|
89
51
|
## note: city requires country scope for lookup
|
90
52
|
## todo/fix: how to deal with cities with the same name
|
91
53
|
## in the same country (and same state and same county etc.) ??? - add some examples here
|
92
|
-
|
54
|
+
recs = Name.find_cities( node.value, country.id )
|
93
55
|
else
|
94
56
|
puts "*** (fatal) error: unknown level for tree node: #{node.inspect}"
|
95
|
-
|
57
|
+
fail "[StateTreeReader] unknown level for tree node: #{node.inspect}"
|
96
58
|
end
|
97
59
|
|
98
60
|
|
99
|
-
if
|
100
|
-
|
61
|
+
if recs.size > 0
|
62
|
+
if recs.size == 1
|
63
|
+
puts "ok - record match found: #{recs.inspect}"
|
64
|
+
rec = recs[0].place_object # e.g. state,part,county,muni,city,etc.
|
65
|
+
else
|
66
|
+
puts "** ok - #{recs.size} record(s) match found: #{recs.inspect}"
|
67
|
+
## fix/todo: note - uses always first entry for now;
|
68
|
+
## make lookup/matching more intelligent/usable!!
|
69
|
+
rec = recs[0].place_object # e.g. state,part,county,muni,city,etc.
|
70
|
+
end
|
101
71
|
else
|
102
72
|
## note: for now only auto-adds munis n cities
|
103
73
|
if node.level == muni_level # 3 or 4
|
@@ -129,7 +99,7 @@ class StateTreeReader < ReaderBaseWithMoreAttribs
|
|
129
99
|
country_id: country.id )
|
130
100
|
else
|
131
101
|
puts "*** (fatal) error: record not found for tree node: #{node.inspect}"
|
132
|
-
|
102
|
+
fail "[StateTreeReader] record not found for tree node: #{node.inspect}"
|
133
103
|
end
|
134
104
|
end
|
135
105
|
|
@@ -137,7 +107,9 @@ class StateTreeReader < ReaderBaseWithMoreAttribs
|
|
137
107
|
|
138
108
|
if level_diff > 0
|
139
109
|
logger.debug "[StateTreeReader] up +#{level_diff}"
|
140
|
-
|
110
|
+
if level_diff > 1
|
111
|
+
fail "[StateTreeReader] level diff MUST be +1 is +#{level_diff}"
|
112
|
+
end
|
141
113
|
elsif level_diff < 0
|
142
114
|
logger.debug "[StateTreeReader] down #{level_diff}"
|
143
115
|
level_diff.abs.times { stack.pop }
|
data/lib/worlddb/version.rb
CHANGED
File without changes
|
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
###
|
4
4
|
# to run use
|
5
|
-
# ruby -I ./lib -I ./test test/
|
5
|
+
# ruby -I ./lib -I ./test test/test_name_finders.rb
|
6
6
|
|
7
7
|
|
8
8
|
require 'helper'
|
9
9
|
|
10
|
-
class
|
10
|
+
class TestNameFinders < MiniTest::Test
|
11
11
|
|
12
12
|
def setup
|
13
13
|
# delete all countries, states, cities in in-memory only db
|
@@ -24,14 +24,25 @@ class TestStateTreeReaderNameFinder < MiniTest::Test
|
|
24
24
|
reader = WorldDb::Reader.new( "#{WorldDb.root}/test/data/at-austria" )
|
25
25
|
reader.load_setup( 'setups/adm' )
|
26
26
|
|
27
|
+
recs = Name.find_states( 'Wien', at.id )
|
28
|
+
pp recs
|
29
|
+
|
30
|
+
recs = Name.find_cities( 'Krems', at.id )
|
31
|
+
pp recs
|
32
|
+
|
33
|
+
|
34
|
+
####
|
35
|
+
# -- finders scoped by state
|
36
|
+
|
27
37
|
n = State.find_by!( key: 'n' )
|
28
38
|
|
39
|
+
recs = Name.find_parts( 'Waldviertel', n.id )
|
40
|
+
pp recs
|
29
41
|
|
30
42
|
## find counties w/ state_id using names
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
:'counties.state_id' => n.id )
|
43
|
+
recs = Name.find_counties( 'Horn', n.id )
|
44
|
+
pp recs
|
45
|
+
|
35
46
|
## results in:
|
36
47
|
## SELECT "names".* FROM "names"
|
37
48
|
## INNER JOIN "places" ON "places"."id" = "names"."place_id"
|
@@ -41,14 +52,11 @@ class TestStateTreeReaderNameFinder < MiniTest::Test
|
|
41
52
|
## AND "counties"."state_id" = 2
|
42
53
|
## [["name", "Horn"], ["place_kind", "COUN"]]
|
43
54
|
|
44
|
-
pp names
|
45
55
|
|
46
56
|
|
47
|
-
## find
|
48
|
-
|
49
|
-
|
50
|
-
:place_kind => 'MUNI',
|
51
|
-
:'munis.state_id' => n.id )
|
57
|
+
## find munis w/ state_id using names
|
58
|
+
recs = Name.find_munis( 'Horn', n.id )
|
59
|
+
pp recs
|
52
60
|
|
53
61
|
## results in:
|
54
62
|
## SELECT "names".* FROM "names"
|
@@ -59,9 +67,7 @@ class TestStateTreeReaderNameFinder < MiniTest::Test
|
|
59
67
|
## AND "munis"."state_id" = 2
|
60
68
|
## [["name", "Horn"], ["place_kind", "MUNI"]]
|
61
69
|
|
62
|
-
pp names
|
63
|
-
|
64
70
|
end
|
65
71
|
|
66
|
-
end # class
|
72
|
+
end # class TestNameFinders
|
67
73
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worlddb-models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: props
|
@@ -219,7 +219,7 @@ files:
|
|
219
219
|
- test/adm/test_read_tree.rb
|
220
220
|
- test/data/at-austria/1--b-burgenland/counties.txt
|
221
221
|
- test/data/at-austria/2--n-niederoesterreich/counties.txt
|
222
|
-
- test/data/at-austria/3--w-wien/
|
222
|
+
- test/data/at-austria/3--w-wien/districts.txt
|
223
223
|
- test/data/at-austria/orte.txt
|
224
224
|
- test/data/at-austria/setups/adm.txt
|
225
225
|
- test/data/at-austria/setups/tree.txt
|
@@ -243,6 +243,7 @@ files:
|
|
243
243
|
- test/test_model_states_at.rb
|
244
244
|
- test/test_model_states_de.rb
|
245
245
|
- test/test_models.rb
|
246
|
+
- test/test_name_finders.rb
|
246
247
|
- test/test_name_parser.rb
|
247
248
|
- test/test_parse_city.rb
|
248
249
|
- test/test_parse_country.rb
|
@@ -250,7 +251,6 @@ files:
|
|
250
251
|
- test/test_report_country.rb
|
251
252
|
- test/test_state_tree_reader_at.rb
|
252
253
|
- test/test_state_tree_reader_de.rb
|
253
|
-
- test/test_state_tree_reader_name_finder.rb
|
254
254
|
homepage: https://github.com/worlddb/world.db.models
|
255
255
|
licenses:
|
256
256
|
- Public Domain
|
@@ -289,8 +289,8 @@ test_files:
|
|
289
289
|
- test/test_model_state.rb
|
290
290
|
- test/test_model_city.rb
|
291
291
|
- test/test_fixture_matchers.rb
|
292
|
-
- test/test_state_tree_reader_name_finder.rb
|
293
292
|
- test/test_model_country.rb
|
293
|
+
- test/test_name_finders.rb
|
294
294
|
- test/test_parse_city.rb
|
295
295
|
- test/test_report_country.rb
|
296
296
|
- test/test_model_compat.rb
|