worlddb 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/worlddb/cli/main.rb +5 -5
- data/lib/worlddb/models/prop.rb +2 -6
- data/lib/worlddb/reader.rb +114 -97
- data/lib/worlddb/version.rb +1 -1
- data/lib/worlddb.rb +2 -2
- metadata +4 -4
data/lib/worlddb/cli/main.rb
CHANGED
@@ -118,18 +118,18 @@ command :load do |c|
|
|
118
118
|
WorldDB.delete! if options.delete.present?
|
119
119
|
|
120
120
|
# read plain text country/region/city fixtures
|
121
|
-
reader = WorldDB::Reader.new
|
121
|
+
reader = WorldDB::Reader.new
|
122
122
|
args.each do |arg|
|
123
123
|
name = arg # File.basename( arg, '.*' )
|
124
124
|
|
125
125
|
if myopts.countries?
|
126
|
-
reader.
|
126
|
+
reader.load_countries( name, myopts.data_path )
|
127
127
|
elsif myopts.regions?
|
128
|
-
reader.
|
128
|
+
reader.load_regions( myopts.country, name, myopts.data_path )
|
129
129
|
elsif myopts.cities?
|
130
|
-
reader.
|
130
|
+
reader.load_cities( myopts.country, name, myopts.data_path )
|
131
131
|
else
|
132
|
-
reader.
|
132
|
+
reader.load( name, myopts.data_path )
|
133
133
|
## todo: issue a warning here; no fixture type specified; assume country?
|
134
134
|
end
|
135
135
|
|
data/lib/worlddb/models/prop.rb
CHANGED
@@ -4,14 +4,10 @@ module WorldDB::Models
|
|
4
4
|
|
5
5
|
class Prop < ActiveRecord::Base
|
6
6
|
|
7
|
-
def self.
|
7
|
+
def self.create_from_fixture!( name, path )
|
8
8
|
key = "db.#{fixture_name_to_prop_key(name)}.version"
|
9
9
|
|
10
|
-
|
11
|
-
value = "world.yml.#{WorldDB::VERSION}" # assume builtin
|
12
|
-
else
|
13
|
-
value = "file.txt.#{File.mtime(path).strftime('%Y.%m.%d')}"
|
14
|
-
end
|
10
|
+
value = "txt.#{File.mtime(path).strftime('%Y.%m.%d')}"
|
15
11
|
|
16
12
|
Prop.create!( key: key, value: value )
|
17
13
|
end
|
data/lib/worlddb/reader.rb
CHANGED
@@ -6,43 +6,44 @@ class Reader
|
|
6
6
|
# e.g. lets you use City instead of Models::City
|
7
7
|
include WorldDB::Models
|
8
8
|
|
9
|
+
### todo/fix: make include_path
|
10
|
+
## required first arg in ctor!!!
|
11
|
+
## cleanup load_ and remove include_path
|
9
12
|
|
10
|
-
def initialize(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@logger = logger
|
15
|
-
end
|
13
|
+
def initialize( opts={} )
|
14
|
+
@logger = LogUtils::Logger.new
|
15
|
+
|
16
|
+
# todo/fix/cleanup: allow initialize( opts = {} ) for logger: logger
|
16
17
|
end
|
17
18
|
|
18
19
|
attr_reader :logger
|
19
20
|
|
20
21
|
|
21
|
-
def
|
22
|
+
def load( name, include_path )
|
22
23
|
|
23
24
|
if name =~ /^lang/
|
24
|
-
|
25
|
+
load_langs( name, include_path )
|
25
26
|
elsif name =~ /\/lang/
|
26
|
-
|
27
|
+
load_usages( name, include_path )
|
27
28
|
elsif name =~ /\/fifa/
|
28
|
-
|
29
|
+
load_xxx( 'fifa', name, include_path )
|
29
30
|
elsif name =~ /\/iso3/
|
30
|
-
|
31
|
+
load_xxx( 'iso3', name, include_path )
|
31
32
|
elsif name =~ /\/internet/
|
32
|
-
|
33
|
+
load_xxx( 'net', name, include_path )
|
33
34
|
elsif name =~ /\/motor/
|
34
|
-
|
35
|
+
load_xxx( 'motor', name, include_path )
|
35
36
|
elsif name =~ /^tag.*\.(\d)$/
|
36
|
-
|
37
|
+
load_tags( name, include_path, :grade => $1.to_i )
|
37
38
|
elsif name =~ /^([a-z]{3,})\/countries/ # e.g. africa/countries or america/countries
|
38
39
|
## auto-add continent (from folder structure) as tag
|
39
|
-
|
40
|
+
load_countries( name, include_path, :tags => $1 )
|
40
41
|
elsif name =~ /\/([a-z]{2})\/cities/
|
41
42
|
## auto-add required country code (from folder structure)
|
42
|
-
|
43
|
+
load_cities( $1, name, include_path )
|
43
44
|
elsif name =~ /\/([a-z]{2})\/regions/
|
44
45
|
## auto-add required country code (from folder structure)
|
45
|
-
|
46
|
+
load_regions( $1, name, include_path )
|
46
47
|
else
|
47
48
|
logger.error "unknown world.db fixture type >#{name}<"
|
48
49
|
# todo/fix: exit w/ error
|
@@ -50,121 +51,137 @@ class Reader
|
|
50
51
|
end
|
51
52
|
|
52
53
|
|
53
|
-
def
|
54
|
-
|
54
|
+
def load_countries( name, include_path, more_values={} )
|
55
|
+
load_fixtures_for( Country, name, include_path, more_values )
|
55
56
|
end
|
56
57
|
|
57
58
|
|
58
|
-
def
|
59
|
+
def load_regions( country_key, name, include_path )
|
59
60
|
country = Country.find_by_key!( country_key )
|
60
|
-
logger.
|
61
|
+
logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
|
61
62
|
|
62
|
-
|
63
|
+
load_fixtures_for( Region, name, include_path, country_id: country.id )
|
63
64
|
end
|
64
65
|
|
65
66
|
|
66
|
-
def
|
67
|
+
def load_cities( country_key, name, include_path )
|
67
68
|
country = Country.find_by_key!( country_key )
|
68
|
-
logger.
|
69
|
+
logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
|
69
70
|
|
70
|
-
|
71
|
+
load_fixtures_for( City, name, include_path, country_id: country.id )
|
71
72
|
end
|
72
73
|
|
73
74
|
|
74
|
-
|
75
|
+
|
76
|
+
def with_path_for( name, include_path )
|
77
|
+
## todo: find a better name?
|
78
|
+
# e.g. find_path_for or open_fixture_for ??
|
79
|
+
|
75
80
|
path = "#{include_path}/#{name}.yml"
|
76
81
|
|
77
|
-
logger.info "
|
82
|
+
logger.info "parsing data '#{name}' (#{path})..."
|
83
|
+
|
84
|
+
yield( path )
|
85
|
+
|
86
|
+
Prop.create_from_fixture!( name, path )
|
87
|
+
end
|
78
88
|
|
79
|
-
reader = HashReader.new( logger, path )
|
80
89
|
|
81
|
-
reader.each do |key, value|
|
82
90
|
|
83
|
-
|
91
|
+
def load_langs( name, include_path )
|
92
|
+
|
93
|
+
with_path_for( name, include_path) do |path|
|
94
|
+
|
95
|
+
reader = HashReader.new( logger, path )
|
96
|
+
|
97
|
+
reader.each do |key, value|
|
98
|
+
|
99
|
+
logger.debug "adding lang >>#{key}<< >>#{value}<<..."
|
84
100
|
|
85
|
-
|
86
|
-
|
101
|
+
lang_key = key.strip
|
102
|
+
lang_title = value.strip
|
87
103
|
|
88
|
-
|
104
|
+
lang_attribs = {}
|
89
105
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
106
|
+
## check if it exists
|
107
|
+
lang = Lang.find_by_key( lang_key )
|
108
|
+
if lang.present?
|
109
|
+
logger.debug "update lang #{lang.id}-#{lang.key}:"
|
110
|
+
else
|
111
|
+
logger.debug "create lang:"
|
112
|
+
lang = Lang.new
|
113
|
+
lang_attribs[ :key ] = lang_key
|
114
|
+
end
|
99
115
|
|
100
|
-
|
116
|
+
lang_attribs[ :title ] = lang_title
|
101
117
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
Prop.create_from_worlddb_fixture!( name, path )
|
108
|
-
end
|
118
|
+
logger.debug lang_attribs.to_json
|
119
|
+
|
120
|
+
lang.update_attributes!( lang_attribs )
|
121
|
+
end # each key,value
|
122
|
+
end # with_path_for
|
109
123
|
|
124
|
+
end # method load_langs
|
110
125
|
|
111
|
-
def load_tags_with_include_path( name, include_path, more_values={} )
|
112
|
-
path = "#{include_path}/#{name}.yml"
|
113
126
|
|
114
|
-
|
127
|
+
def load_tags( name, include_path, more_values={} )
|
128
|
+
|
129
|
+
with_path_for( name, include_path) do |path|
|
115
130
|
|
116
|
-
|
131
|
+
### fix/todo/cleanup: HashReader -> make logger a keyword option only; do NOT use here
|
132
|
+
reader = HashReader.new( logger, path )
|
117
133
|
|
118
|
-
|
134
|
+
grade = 1
|
119
135
|
|
120
|
-
|
121
|
-
|
122
|
-
|
136
|
+
if more_values[:grade].present?
|
137
|
+
grade = more_values[:grade].to_i
|
138
|
+
end
|
123
139
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
140
|
+
reader.each do |key, value|
|
141
|
+
### split value by comma (e.g. northern america,southern america, etc.)
|
142
|
+
logger.debug "adding grade #{grade} tags >>#{key}<< >>#{value}<<..."
|
143
|
+
tag_pairs = value.split(',')
|
144
|
+
tag_pairs.each do |pair|
|
129
145
|
## split key|title
|
130
|
-
|
146
|
+
values = pair.split('|')
|
131
147
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
148
|
+
key = values[0]
|
149
|
+
### remove (optional comment) from key (e.g. carribean (islands))
|
150
|
+
key = key.gsub( /\(.+\)/, '' )
|
151
|
+
## remove leading n trailing space
|
152
|
+
key = key.strip
|
137
153
|
|
138
|
-
|
139
|
-
|
154
|
+
title = values[1] || '' # nb: title might be empty/missing
|
155
|
+
title = title.strip
|
140
156
|
|
141
|
-
|
157
|
+
tag_attribs = {}
|
142
158
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
159
|
+
## check if it exists
|
160
|
+
## todo/fix: add country_id for lookup?
|
161
|
+
tag = Tag.find_by_key( key )
|
162
|
+
if tag.present?
|
163
|
+
logger.debug "update tag #{tag.id}-#{tag.key}:"
|
164
|
+
else
|
165
|
+
logger.debug "create tag:"
|
166
|
+
tag = Tag.new
|
167
|
+
tag_attribs[ :key ] = key
|
168
|
+
end
|
153
169
|
|
154
|
-
|
155
|
-
|
170
|
+
tag_attribs[ :title ] = title
|
171
|
+
tag_attribs[ :grade ] = grade
|
156
172
|
|
157
|
-
|
173
|
+
logger.debug tag_attribs.to_json
|
158
174
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
175
|
+
tag.update_attributes!( tag_attribs )
|
176
|
+
end
|
177
|
+
end # each key,value
|
178
|
+
|
179
|
+
end # with_path_for
|
180
|
+
|
181
|
+
end # method load_tags
|
165
182
|
|
166
183
|
|
167
|
-
def
|
184
|
+
def load_usages( name, include_path )
|
168
185
|
path = "#{include_path}/#{name}.yml"
|
169
186
|
|
170
187
|
puts "*** parsing data '#{name}' (#{path})..."
|
@@ -189,11 +206,11 @@ class Reader
|
|
189
206
|
end
|
190
207
|
end
|
191
208
|
|
192
|
-
Prop.
|
209
|
+
Prop.create_from_fixture!( name, path )
|
193
210
|
end
|
194
211
|
|
195
212
|
|
196
|
-
def
|
213
|
+
def load_xxx( xxx, name, include_path )
|
197
214
|
path = "#{include_path}/#{name}.yml"
|
198
215
|
|
199
216
|
puts "*** parsing data '#{name}' (#{path})..."
|
@@ -206,12 +223,12 @@ class Reader
|
|
206
223
|
country.save!
|
207
224
|
end
|
208
225
|
|
209
|
-
Prop.
|
226
|
+
Prop.create_from_fixture!( name, path )
|
210
227
|
end
|
211
228
|
|
212
229
|
|
213
230
|
private
|
214
|
-
def
|
231
|
+
def load_fixtures_for( clazz, name, include_path, more_values={} ) # load from file system
|
215
232
|
path = "#{include_path}/#{name}.txt"
|
216
233
|
|
217
234
|
puts "*** parsing data '#{name}' (#{path})..."
|
@@ -220,7 +237,7 @@ private
|
|
220
237
|
|
221
238
|
load_fixtures_worker_for( clazz, reader )
|
222
239
|
|
223
|
-
Prop.
|
240
|
+
Prop.create_from_fixture!( name, path )
|
224
241
|
end
|
225
242
|
|
226
243
|
|
data/lib/worlddb/version.rb
CHANGED
data/lib/worlddb.rb
CHANGED
@@ -65,7 +65,7 @@ module WorldDB
|
|
65
65
|
def self.read( ary, include_path )
|
66
66
|
reader = Reader.new
|
67
67
|
ary.each do |name|
|
68
|
-
reader.
|
68
|
+
reader.load( name, include_path )
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -81,7 +81,7 @@ module WorldDB
|
|
81
81
|
ary = Fixtures.all - fixture_excludes
|
82
82
|
|
83
83
|
ary.each do |name|
|
84
|
-
reader.
|
84
|
+
reader.load( name, include_path )
|
85
85
|
end # each name
|
86
86
|
end # method read_all
|
87
87
|
|
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: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
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: 2013-02-
|
18
|
+
date: 2013-02-21 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: textutils
|