worlddb-models 2.3.0 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +1 -0
- data/lib/worlddb/models/city_base.rb +8 -0
- data/lib/worlddb/models/name.rb +87 -0
- data/lib/worlddb/models/state.rb +5 -13
- data/lib/worlddb/models/state_base.rb +9 -1
- data/lib/worlddb/schema.rb +7 -5
- data/lib/worlddb/version.rb +1 -1
- data/test/test_model_states_at.rb +3 -3
- data/test/test_model_states_de.rb +4 -4
- data/test/test_name_parser.rb +38 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e65089aa526d90b22ff73acc40835c46ef784c93
|
4
|
+
data.tar.gz: 07883ca048c28555a3d27d1ba9a25a9b1a5de377
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9c7c04d0c3d9b7d6240865abfbc523ba464ed30d90c8433afe11826ba9e7fe770ef18176b6785f922e9ca4c51c56d6a55e253813c149ef49c0ac24a62f9b523
|
7
|
+
data.tar.gz: 9e615c49999dc69f574b9f50c8c14d1f489e5dd279e99e772f6629fee56b50fa7dab2445ae2b351a1efb833454694544d4c7762746b1b82774913323c691fa37
|
data/Manifest.txt
CHANGED
@@ -32,11 +32,19 @@ class CityBase < ActiveRecord::Base
|
|
32
32
|
def on_create
|
33
33
|
place_rec = Place.create!( name: name, kind: place_kind )
|
34
34
|
self.place_id = place_rec.id
|
35
|
+
|
36
|
+
## create name recs -- todo: add default lang etc. - move to: after_create callback!!
|
37
|
+
Name.parse( name, alt_names,
|
38
|
+
place_id: place_rec.id,
|
39
|
+
place_kind: place_rec.kind )
|
35
40
|
end
|
36
41
|
|
37
42
|
def on_update
|
38
43
|
## fix/todo: check - if name or kind changed - only update if changed ?? why? why not??
|
39
44
|
place.update_attributes!( name: name, kind: place_kind )
|
45
|
+
|
46
|
+
## todo/fix:
|
47
|
+
## update names too ??
|
40
48
|
end
|
41
49
|
|
42
50
|
validates :key, format: { with: /#{CITY_KEY_PATTERN}/, message: CITY_KEY_PATTERN_MESSAGE }
|
data/lib/worlddb/models/name.rb
CHANGED
@@ -1,11 +1,98 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
|
4
|
+
##
|
5
|
+
# todo: move to textutils!!!
|
6
|
+
|
7
|
+
class NameParser
|
8
|
+
|
9
|
+
include LogUtils::Logging
|
10
|
+
|
11
|
+
def parse( chunks )
|
12
|
+
## todo/fix: (re)use nameparser - for now "simple" inline version
|
13
|
+
## fix!!! - note: for now lang gets ignored
|
14
|
+
## fix: add hanlde
|
15
|
+
## Leuven[nl]|Louvain[fr] Löwen[de]
|
16
|
+
## Antwerpen[nl]|Anvers[fr] [Antwerp]
|
17
|
+
## Brussel[nl]•Bruxelles[fr] -> official bi-lingual name
|
18
|
+
## etc.
|
19
|
+
|
20
|
+
## values - split into names (name n lang pairs)
|
21
|
+
## note: assumes (default) lang from more_attribs unless otherwise marked e.g. [] assume en etc.
|
22
|
+
|
23
|
+
## split chunks into values
|
24
|
+
values = []
|
25
|
+
chunks.each do |chunk|
|
26
|
+
next if chunk.nil? || chunk.blank? ## skip nil or empty/blank chunks
|
27
|
+
|
28
|
+
parts = chunk.split( '|' ) # 1) split |
|
29
|
+
|
30
|
+
parts.each do |part|
|
31
|
+
s = StringScanner.new( part )
|
32
|
+
s.skip( /[ \t]+/) # skip whitespaces
|
33
|
+
|
34
|
+
while s.eos? == false
|
35
|
+
if s.check( /\[/ )
|
36
|
+
## scan everything until the end of bracket (e.g.])
|
37
|
+
## fix!!! - note: for now lang gets ignored
|
38
|
+
value = s.scan( /\[[^\]]+\]/)
|
39
|
+
value = value[1...-1] # strip enclosing [] e.g. [Bavaria] => Bavaria
|
40
|
+
else
|
41
|
+
## scan everything until the begin of bracket (e.g.[)
|
42
|
+
value = s.scan( /[^\[]+/)
|
43
|
+
value = value.strip
|
44
|
+
end
|
45
|
+
values << value
|
46
|
+
|
47
|
+
s.skip( /[ \t]+/) # skip whitespaces
|
48
|
+
logger.debug( "[NameParser] eos?: #{s.eos?}, rest: >#{s.rest}<" )
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
logger.debug( "[NameParser] values=#{values.inspect}")
|
54
|
+
|
55
|
+
names = []
|
56
|
+
values.each do |value|
|
57
|
+
name = value
|
58
|
+
## todo: split by bullet ? (official multilang name) e.g. Brussel • Bruxelles
|
59
|
+
## todo: process variants w/ () e.g. Krems (a. d. Donau) etc. ??
|
60
|
+
names << name
|
61
|
+
end
|
62
|
+
|
63
|
+
logger.debug( "[NameParser] names=#{names.inspect}")
|
64
|
+
|
65
|
+
names
|
66
|
+
end # method parse
|
67
|
+
end # class NameParser
|
68
|
+
|
69
|
+
|
70
|
+
|
3
71
|
module WorldDb
|
4
72
|
module Model
|
5
73
|
|
6
74
|
class Name < ActiveRecord::Base
|
7
75
|
|
8
76
|
|
77
|
+
def self.parse( *args )
|
78
|
+
## remove (extract) attribs hash (if last arg is a hash n present)
|
79
|
+
more_attribs = args.last.is_a?(Hash) ? args.pop : {} ## extract_options!
|
80
|
+
chunks = args
|
81
|
+
|
82
|
+
names = NameParser.new.parse( chunks )
|
83
|
+
recs = []
|
84
|
+
|
85
|
+
names.each do |name|
|
86
|
+
attribs = more_attribs.merge( name: name ) ## overwrite (default) attribs (lang, etc.) plus add name
|
87
|
+
puts "[Name.parse] adding Name record:"
|
88
|
+
puts " #{attribs.inspect}"
|
89
|
+
rec = Name.create!( attribs )
|
90
|
+
recs << rec
|
91
|
+
end
|
92
|
+
|
93
|
+
recs # note: returns an array of name records
|
94
|
+
end # method parse
|
95
|
+
|
9
96
|
end # class Name
|
10
97
|
|
11
98
|
|
data/lib/worlddb/models/state.rb
CHANGED
@@ -13,10 +13,8 @@ class State < StateBase
|
|
13
13
|
has_many :munis, class_name: 'Muni', foreign_key: 'state_id' # ADM3 / ADM4
|
14
14
|
has_many :cities, class_name: 'City', foreign_key: 'state_id'
|
15
15
|
|
16
|
-
def place_kind
|
17
|
-
|
18
|
-
end
|
19
|
-
|
16
|
+
def place_kind() 'STAT'; end
|
17
|
+
|
20
18
|
def state_id # return top-level (e.g. adm1) state_id
|
21
19
|
id
|
22
20
|
end
|
@@ -31,9 +29,7 @@ class Part < StateBase ## optional ADM2 e.g. Regierungsbezirke in Bayern, etc
|
|
31
29
|
has_many :counties, class_name: 'County', foreign_key: 'part_id'
|
32
30
|
has_many :cities, class_name: 'City', foreign_key: 'part_id'
|
33
31
|
|
34
|
-
def place_kind
|
35
|
-
'ADM2' # note: for now assumes always level 2
|
36
|
-
end
|
32
|
+
def place_kind() 'PART'; end
|
37
33
|
|
38
34
|
def country_id # return country_id via top-level (e.g. adm1) state; -- used for auto-creating cities
|
39
35
|
state.country_id
|
@@ -50,9 +46,7 @@ class County < StateBase ## note: might be ADM2 or ADM3
|
|
50
46
|
has_many :munis, class_name: 'Muni', foreign_key: 'county_id'
|
51
47
|
has_many :cities, class_name: 'City', foreign_key: 'county_id'
|
52
48
|
|
53
|
-
def place_kind
|
54
|
-
"ADM#{level}" # note: might be ADM2 or ADM3
|
55
|
-
end
|
49
|
+
def place_kind() 'COUN'; end
|
56
50
|
|
57
51
|
def country_id # return country_id via top-level (e.g. adm1) state
|
58
52
|
state.country_id
|
@@ -68,9 +62,7 @@ class Muni < StateBase ## note: might be ADM3 or ADM4
|
|
68
62
|
|
69
63
|
has_many :cities, class_name: 'City', foreign_key: 'muni_id'
|
70
64
|
|
71
|
-
def place_kind
|
72
|
-
"ADM#{level}" # note: might be ADM3 or ADM4
|
73
|
-
end
|
65
|
+
def place_kind() 'MUNI'; end
|
74
66
|
|
75
67
|
def country_id # return country_id via top-level (e.g. adm1) state
|
76
68
|
state.country_id
|
@@ -39,12 +39,20 @@ class StateBase < ActiveRecord::Base
|
|
39
39
|
|
40
40
|
def on_create
|
41
41
|
place_rec = Place.create!( name: name, kind: place_kind )
|
42
|
-
self.place_id = place_rec.id
|
42
|
+
self.place_id = place_rec.id
|
43
|
+
|
44
|
+
## create name recs -- todo: add default lang etc. - move to: after_create callback!!
|
45
|
+
Name.parse( name, alt_names,
|
46
|
+
place_id: place_rec.id,
|
47
|
+
place_kind: place_rec.kind )
|
43
48
|
end
|
44
49
|
|
45
50
|
def on_update
|
46
51
|
## fix/todo: check - if name or kind changed - only update if changed ?? why? why not??
|
47
52
|
place.update_attributes!( name: name, kind: place_kind )
|
53
|
+
|
54
|
+
## todo/fix:
|
55
|
+
## update names too ??
|
48
56
|
end
|
49
57
|
|
50
58
|
|
data/lib/worlddb/schema.rb
CHANGED
@@ -18,10 +18,10 @@ create_table :places do |t|
|
|
18
18
|
# CNTY - country
|
19
19
|
# TERR - terr
|
20
20
|
# state:
|
21
|
-
# ADM1 - e.g. state/province
|
22
|
-
#
|
23
|
-
# ADM3 - e.g.
|
24
|
-
# ADM4
|
21
|
+
# STAT -- was: ADM1 - e.g. state/province/etc.
|
22
|
+
# PART e.g. regierungsbezirk ADM2
|
23
|
+
# COUN -- was ADM2/ADM3 - e.g. county/bezirk/kreis/etc.
|
24
|
+
# MUNI - was ADM3/ADM4 - e.g. municipality/gemeinde/etc.
|
25
25
|
# city:
|
26
26
|
# MTRO - metro
|
27
27
|
# CITY - city/town/village/hamlet //(de)ort/stadt/markt
|
@@ -50,7 +50,9 @@ end
|
|
50
50
|
### alternative names for places
|
51
51
|
create_table :names do |t|
|
52
52
|
t.string :name, null: false
|
53
|
-
t.references :place,
|
53
|
+
t.references :place, null: false
|
54
|
+
t.string :place_kind, null: false # add (cached) place_kind for easier search queries (w/o join)
|
55
|
+
|
54
56
|
### todo/fix: add lang_id to reference lang from lang table!!! (for now make it a duplicate; e.g. keep stirng lang for now)
|
55
57
|
t.string :lang, null: false, default: 'en' # default to english for now (or use unknown/undeterminded/unspecified???)
|
56
58
|
|
data/lib/worlddb/version.rb
CHANGED
@@ -36,7 +36,7 @@ class TestModelStatesAt < MiniTest::Test
|
|
36
36
|
assert_equal 1, n.area
|
37
37
|
assert_equal at.id, n.country_id
|
38
38
|
assert_equal 1, n.level
|
39
|
-
assert_equal '
|
39
|
+
assert_equal 'STAT', n.place_kind
|
40
40
|
|
41
41
|
### test place
|
42
42
|
assert_equal 'Niederösterreich', n.place.name
|
@@ -64,7 +64,7 @@ class TestModelStatesAt < MiniTest::Test
|
|
64
64
|
assert_equal 1, tu.area
|
65
65
|
assert_equal n.id, tu.state_id
|
66
66
|
assert_equal 2, tu.level
|
67
|
-
assert_equal '
|
67
|
+
assert_equal 'COUN', tu.place_kind
|
68
68
|
|
69
69
|
### test place
|
70
70
|
assert_equal 'Tulln', tu.place.name
|
@@ -94,7 +94,7 @@ class TestModelStatesAt < MiniTest::Test
|
|
94
94
|
assert_equal n.id, gr.state_id
|
95
95
|
assert_equal tu.id, gr.county_id
|
96
96
|
assert_equal 3, gr.level
|
97
|
-
assert_equal '
|
97
|
+
assert_equal 'MUNI', gr.place_kind
|
98
98
|
|
99
99
|
### test place
|
100
100
|
assert_equal 'Grafenwörth', gr.place.name
|
@@ -36,7 +36,7 @@ class TestModelStatesDe < MiniTest::Test
|
|
36
36
|
assert_equal 1, by.area
|
37
37
|
assert_equal de.id, by.country_id
|
38
38
|
assert_equal 1, by.level
|
39
|
-
assert_equal '
|
39
|
+
assert_equal 'STAT', by.place_kind
|
40
40
|
|
41
41
|
### test place
|
42
42
|
assert_equal 'Bayern', by.place.name
|
@@ -64,7 +64,7 @@ class TestModelStatesDe < MiniTest::Test
|
|
64
64
|
assert_equal 1, ob.area
|
65
65
|
assert_equal by.id, ob.state_id
|
66
66
|
assert_equal 2, ob.level
|
67
|
-
assert_equal '
|
67
|
+
assert_equal 'PART', ob.place_kind
|
68
68
|
|
69
69
|
### test place
|
70
70
|
assert_equal 'Oberbayern', ob.place.name
|
@@ -95,7 +95,7 @@ class TestModelStatesDe < MiniTest::Test
|
|
95
95
|
assert_equal by.id, fs.state_id
|
96
96
|
assert_equal ob.id, fs.part_id
|
97
97
|
assert_equal 3, fs.level
|
98
|
-
assert_equal '
|
98
|
+
assert_equal 'COUN', fs.place_kind
|
99
99
|
|
100
100
|
### test place
|
101
101
|
assert_equal 'Freising', fs.place.name
|
@@ -129,7 +129,7 @@ class TestModelStatesDe < MiniTest::Test
|
|
129
129
|
assert_equal by.id, au.state_id
|
130
130
|
assert_equal fs.id, au.county_id
|
131
131
|
assert_equal 4, au.level
|
132
|
-
assert_equal '
|
132
|
+
assert_equal 'MUNI', au.place_kind
|
133
133
|
|
134
134
|
### test place
|
135
135
|
assert_equal 'Au i.d. Hallertau', au.place.name
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_name_parser.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
class TestNameParser < MiniTest::Test
|
11
|
+
|
12
|
+
def test_bayern
|
13
|
+
names = NameParser.new.parse( [ 'Bayern [Bavaria]' ] )
|
14
|
+
|
15
|
+
assert_equal 2, names.size
|
16
|
+
assert_equal 'Bayern', names[0]
|
17
|
+
assert_equal 'Bavaria', names[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_brussel
|
21
|
+
names = NameParser.new.parse( [ 'Brussel•Bruxelles [Brussels]' ] )
|
22
|
+
|
23
|
+
## fix: (auto-)split into three variants e.g.Brussel•Bruxelles, Brussel, Bruxelles
|
24
|
+
assert_equal 2, names.size
|
25
|
+
assert_equal 'Brussel•Bruxelles', names[0]
|
26
|
+
assert_equal 'Brussels', names[1]
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_antwerp
|
30
|
+
names = NameParser.new.parse( [ 'Antwerpen|Anvers [Antwerp]' ] )
|
31
|
+
|
32
|
+
assert_equal 3, names.size
|
33
|
+
assert_equal 'Antwerpen', names[0]
|
34
|
+
assert_equal 'Anvers', names[1]
|
35
|
+
assert_equal 'Antwerp', names[2]
|
36
|
+
end
|
37
|
+
|
38
|
+
end # class TestNameParser
|
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.1
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: props
|
@@ -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_parser.rb
|
246
247
|
- test/test_parse_city.rb
|
247
248
|
- test/test_parse_country.rb
|
248
249
|
- test/test_parse_state.rb
|
@@ -276,6 +277,7 @@ signing_key:
|
|
276
277
|
specification_version: 4
|
277
278
|
summary: worlddb - world.db schema 'n' models for easy (re)use
|
278
279
|
test_files:
|
280
|
+
- test/test_name_parser.rb
|
279
281
|
- test/test_models.rb
|
280
282
|
- test/adm/test_read_tree.rb
|
281
283
|
- test/adm/test_fixture_matcher_tree.rb
|