worlddb 0.3.0 → 0.3.1
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/db/america/br/cities.txt +9968 -7
- data/db/america/countries.txt +1 -1
- data/lib/worlddb.rb +71 -0
- data/lib/worlddb/cli/opts.rb +11 -1
- data/lib/worlddb/cli/runner.rb +12 -12
- data/lib/worlddb/models/city.rb +7 -2
- data/lib/worlddb/models/country.rb +5 -1
- data/lib/worlddb/models/region.rb +1 -0
- data/lib/worlddb/reader.rb +18 -5
- data/lib/worlddb/schema.rb +4 -3
- data/lib/worlddb/version.rb +1 -1
- metadata +4 -4
data/db/america/countries.txt
CHANGED
@@ -22,7 +22,7 @@ sv, El Salvador, SLV
|
|
22
22
|
|
23
23
|
ar, Argentina, ARG, 2780400, 40518425, south america|es
|
24
24
|
bo, Bolivia, BOL, 1098581, 10907778, south america|es
|
25
|
-
br, Brazil, BRA, 8514215, 192380000, south america|pt
|
25
|
+
br, Brazil, BRA, 8514215, 192380000, south america|pt-br
|
26
26
|
cl, Chile, CHI, 755696, 16763470, south america|es
|
27
27
|
co, Colombia, COL, 1138748, 46413791, south america|es
|
28
28
|
ec, Ecuador, ECU, 258238, 15007343, south america|es
|
data/lib/worlddb.rb
CHANGED
@@ -49,6 +49,9 @@ module WorldDB
|
|
49
49
|
Runner.new.run(ARGV)
|
50
50
|
end
|
51
51
|
|
52
|
+
def self.create
|
53
|
+
CreateDB.up
|
54
|
+
end
|
52
55
|
|
53
56
|
# load built-in (that is, bundled within the gem) named seeds
|
54
57
|
# - pass in an array of seed names e.g. [ 'countries', 'at/cities', 'de/cities' ] etc.
|
@@ -60,6 +63,74 @@ module WorldDB
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
66
|
+
def self.fixtures # all builtin fixtures; helper for covenience
|
67
|
+
|
68
|
+
['africa/countries',
|
69
|
+
'america/countries',
|
70
|
+
'america/br/regions',
|
71
|
+
'america/br/cities',
|
72
|
+
'america/ca/regions',
|
73
|
+
'america/ca/cities',
|
74
|
+
'america/mx/cities',
|
75
|
+
'america/us/regions',
|
76
|
+
'america/us/cities',
|
77
|
+
'america/ve/regions',
|
78
|
+
'america/ve/cities',
|
79
|
+
'asia/countries',
|
80
|
+
'asia/jp/cities',
|
81
|
+
'europe/countries',
|
82
|
+
'europe/at/regions',
|
83
|
+
'europe/at/cities',
|
84
|
+
'europe/be/cities',
|
85
|
+
'europe/by/cities',
|
86
|
+
'europe/ch/cities',
|
87
|
+
'europe/cy/cities',
|
88
|
+
'europe/de/regions',
|
89
|
+
'europe/de/cities',
|
90
|
+
'europe/dk/cities',
|
91
|
+
'europe/en/cities',
|
92
|
+
'europe/es/cities',
|
93
|
+
'europe/fr/cities',
|
94
|
+
'europe/gr/cities',
|
95
|
+
'europe/hr/cities',
|
96
|
+
'europe/it/cities',
|
97
|
+
'europe/nl/cities',
|
98
|
+
'europe/pt/cities',
|
99
|
+
'europe/ro/cities',
|
100
|
+
'europe/ru/cities',
|
101
|
+
'europe/sc/cities',
|
102
|
+
'europe/tr/cities',
|
103
|
+
'europe/ua/cities',
|
104
|
+
'oceania/countries',
|
105
|
+
'oceania/au/cities'
|
106
|
+
]
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def self.read( ary )
|
111
|
+
reader = Reader.new
|
112
|
+
ary.each do |name|
|
113
|
+
reader.load_builtin( name )
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.read_all # load all builtins (using plain text reader); helper for convenience
|
118
|
+
reader = Reader.new
|
119
|
+
|
120
|
+
# too big for heroku free db plan (10,000 record limit)
|
121
|
+
# - sorry, can't load by default
|
122
|
+
fixture_excludes = [
|
123
|
+
'america/br/cities',
|
124
|
+
'america/ve/cities'
|
125
|
+
]
|
126
|
+
|
127
|
+
ary = fixtures - fixture_excludes
|
128
|
+
|
129
|
+
ary.each do |name|
|
130
|
+
reader.load_builtin( name )
|
131
|
+
end # each name
|
132
|
+
end # method load_all
|
133
|
+
|
63
134
|
|
64
135
|
class Deleter
|
65
136
|
## todo: move into its own file???
|
data/lib/worlddb/cli/opts.rb
CHANGED
@@ -47,7 +47,17 @@ class Opts
|
|
47
47
|
return false if @create.nil? # default create flag is false
|
48
48
|
@create == true
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
|
+
|
52
|
+
def setup=(boolean)
|
53
|
+
@setup = boolean
|
54
|
+
end
|
55
|
+
|
56
|
+
def setup?
|
57
|
+
return false if @setup.nil? # default create flag is false
|
58
|
+
@setup == true
|
59
|
+
end
|
60
|
+
|
51
61
|
|
52
62
|
def delete=(boolean)
|
53
63
|
@delete = boolean
|
data/lib/worlddb/cli/runner.rb
CHANGED
@@ -22,6 +22,7 @@ class Runner
|
|
22
22
|
|
23
23
|
## NB: reserve -c for use with -c/--config
|
24
24
|
cmd.on( '--create', 'Create DB schema' ) { opts.create = true }
|
25
|
+
cmd.on( '--setup', "Create DB schema 'n' load builtin world data" ) { opts.setup = true }
|
25
26
|
|
26
27
|
### todo: in future allow multiple search path??
|
27
28
|
cmd.on( '-i', '--include PATH', "Data path (default is #{opts.data_path})" ) { |path| opts.data_path = path }
|
@@ -91,19 +92,18 @@ EOS
|
|
91
92
|
|
92
93
|
ActiveRecord::Base.establish_connection( db_config )
|
93
94
|
|
94
|
-
if opts.
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
if opts.delete?
|
99
|
-
WorldDB.delete!
|
100
|
-
end
|
101
|
-
|
102
|
-
|
103
|
-
if opts.countries? || opts.regions? || opts.cities?
|
104
|
-
Reader.new( logger ).run( opts, args ) # load/read plain text country/region/city fixtures
|
95
|
+
if opts.setup?
|
96
|
+
WorldDB.create
|
97
|
+
WorldDB.read_all
|
105
98
|
else
|
106
|
-
|
99
|
+
WorldDB.create if opts.create?
|
100
|
+
WorldDB.delete! if opts.delete?
|
101
|
+
|
102
|
+
if opts.countries? || opts.regions? || opts.cities?
|
103
|
+
Reader.new( logger ).run( opts, args ) # load/read plain text country/region/city fixtures
|
104
|
+
else
|
105
|
+
Loader.new( logger ).run( opts, args ) # load ruby fixtures
|
106
|
+
end
|
107
107
|
end
|
108
108
|
|
109
109
|
dump_stats
|
data/lib/worlddb/models/city.rb
CHANGED
@@ -10,8 +10,11 @@ class City < ActiveRecord::Base
|
|
10
10
|
|
11
11
|
has_many :taggings, :as => :taggable
|
12
12
|
has_many :tags, :through => :taggings
|
13
|
-
|
14
|
-
|
13
|
+
|
14
|
+
validates :key, :format => { :with => /^[a-z]{3,}$/, :message => 'expected three or more lowercase letters a-z' }
|
15
|
+
validates :code, :format => { :with => /^[A-Z_]{3}$/, :message => 'expected three uppercase letters A-Z (and _)' }, :allow_nil => true
|
16
|
+
|
17
|
+
|
15
18
|
def self.create_from_ary!( cities, more_values={} )
|
16
19
|
cities.each do |values|
|
17
20
|
|
@@ -38,6 +41,8 @@ class City < ActiveRecord::Base
|
|
38
41
|
attr[ :country_id ] = value.id
|
39
42
|
elsif value.is_a? Numeric
|
40
43
|
value_numbers << value
|
44
|
+
elsif value =~ /^[A-Z_]{3}$/ ## assume its three letter code (e.g. NYC,VIE,etc.)
|
45
|
+
attr[ :code ] = value
|
41
46
|
elsif value =~ /^region:/ ## region:
|
42
47
|
value_region_key = value[7..-1] ## cut off region: prefix
|
43
48
|
value_region = Region.find_by_key!( value_region_key )
|
@@ -11,6 +11,10 @@ class Country < ActiveRecord::Base
|
|
11
11
|
has_many :taggings, :as => :taggable
|
12
12
|
has_many :tags, :through => :taggings
|
13
13
|
|
14
|
+
validates :key, :format => { :with => /^[a-z]{2}$/, :message => 'expected two lowercase letters a-z' }
|
15
|
+
validates :code, :format => { :with => /^[A-Z_]{3}$/, :message => 'expected three uppercase letters A-Z (and _)' }
|
16
|
+
|
17
|
+
|
14
18
|
def self.create_from_ary!( countries )
|
15
19
|
countries.each do |values|
|
16
20
|
|
@@ -18,7 +22,7 @@ class Country < ActiveRecord::Base
|
|
18
22
|
attr = {
|
19
23
|
:key => values[0],
|
20
24
|
:title => values[1],
|
21
|
-
:
|
25
|
+
:code => values[2]
|
22
26
|
}
|
23
27
|
|
24
28
|
value_numbers = []
|
@@ -9,6 +9,7 @@ class Region < ActiveRecord::Base
|
|
9
9
|
has_many :taggings, :as => :taggable
|
10
10
|
has_many :tags, :through => :taggings
|
11
11
|
|
12
|
+
validates :key, :format => { :with => /^[a-z]{2,}$/, :message => 'expected two or more lowercase letters a-z' }
|
12
13
|
|
13
14
|
def self.create_from_ary!( regions, more_values={} )
|
14
15
|
regions.each do |values|
|
data/lib/worlddb/reader.rb
CHANGED
@@ -45,14 +45,14 @@ class Reader
|
|
45
45
|
|
46
46
|
def load_regions_with_include_path( country_key, name, include_path )
|
47
47
|
country = Country.find_by_key!( country_key )
|
48
|
-
puts "Country #{country.key} >#{country.title} (#{country.
|
48
|
+
puts "Country #{country.key} >#{country.title} (#{country.code})<"
|
49
49
|
|
50
50
|
load_fixtures_with_include_path_for( Region, name, include_path, country_id: country.id )
|
51
51
|
end
|
52
52
|
|
53
53
|
def load_cities_with_include_path( country_key, name, include_path )
|
54
54
|
country = Country.find_by_key!( country_key )
|
55
|
-
puts "Country #{country.key} >#{country.title} (#{country.
|
55
|
+
puts "Country #{country.key} >#{country.title} (#{country.code})<"
|
56
56
|
|
57
57
|
load_fixtures_with_include_path_for( City, name, include_path, country_id: country.id )
|
58
58
|
end
|
@@ -60,20 +60,33 @@ class Reader
|
|
60
60
|
##################################
|
61
61
|
# load from gem (built-in)
|
62
62
|
|
63
|
+
def load_builtin( name ) ## convenience helper (requires proper named files w/ convention)
|
64
|
+
if name =~ /\/countries/
|
65
|
+
load_countries_builtin( name )
|
66
|
+
elsif name =~ /\/([a-z]{2})\/cities/
|
67
|
+
load_cities_builtin( $1, name )
|
68
|
+
elsif name =~ /\/([a-z]{2})\/regions/
|
69
|
+
load_regions_builtin( $1, name )
|
70
|
+
else
|
71
|
+
puts "*** error: unknown world.db fixture type >#{name}<"
|
72
|
+
# todo/fix: exit w/ error
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
63
76
|
def load_countries_builtin( name )
|
64
77
|
load_fixtures_builtin_for( Country, name )
|
65
78
|
end
|
66
79
|
|
67
80
|
def load_regions_builtin( country_key, name )
|
68
81
|
country = Country.find_by_key!( country_key )
|
69
|
-
puts "Country #{country.key} >#{country.title} (#{country.
|
82
|
+
puts "Country #{country.key} >#{country.title} (#{country.code})<"
|
70
83
|
|
71
84
|
load_fixtures_builtin_for( Region, name, country_id: country.id )
|
72
85
|
end
|
73
86
|
|
74
87
|
def load_cities_builtin( country_key, name )
|
75
88
|
country = Country.find_by_key!( country_key )
|
76
|
-
puts "Country #{country.key} >#{country.title} (#{country.
|
89
|
+
puts "Country #{country.key} >#{country.title} (#{country.code})<"
|
77
90
|
|
78
91
|
load_fixtures_builtin_for( City, name, country_id: country.id )
|
79
92
|
end
|
@@ -166,7 +179,7 @@ private
|
|
166
179
|
value_region = Region.find_by_key!( value_region_key )
|
167
180
|
attribs[ :region_id ] = value_region.id
|
168
181
|
elsif value =~ /^[A-Z]{3}$/ ## assume three-letter code
|
169
|
-
attribs[ :
|
182
|
+
attribs[ :code ] = value
|
170
183
|
elsif value =~ /^\d+$/ ## numeric
|
171
184
|
value_numbers << value.to_i
|
172
185
|
elsif (values.size==(index+3)) && value =~ /^[a-z0-9\| ]+$/ # tags must be last entry
|
data/lib/worlddb/schema.rb
CHANGED
@@ -11,9 +11,9 @@ def self.up
|
|
11
11
|
ActiveRecord::Schema.define do
|
12
12
|
|
13
13
|
create_table :countries do |t|
|
14
|
-
t.string :title,
|
15
|
-
t.string :key,
|
16
|
-
t.string :
|
14
|
+
t.string :title, :null => false
|
15
|
+
t.string :key, :null => false
|
16
|
+
t.string :code, :null => false # short three letter code (FIFA country code e.g. ITA)
|
17
17
|
t.string :synonyms # comma separated list of synonyms
|
18
18
|
t.string :motor # optional auto motor (vehicle) licene plate
|
19
19
|
t.integer :pop # optional population count
|
@@ -34,6 +34,7 @@ end
|
|
34
34
|
create_table :cities do |t|
|
35
35
|
t.string :title, :null => false
|
36
36
|
t.string :key, :null => false
|
37
|
+
t.string :code # short three letter code (ITAT/airport code e.g. NYC or VIE)
|
37
38
|
t.string :synonyms # comma separated list of synonyms
|
38
39
|
t.references :country, :null => false
|
39
40
|
t.references :region # optional for now
|
data/lib/worlddb/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worlddb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gerald Bauer
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-11-
|
18
|
+
date: 2012-11-16 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: activerecord
|