worlddb 1.0.1 → 1.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.
@@ -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 # todo: add logger here?
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.load_countries_with_include_path( name, myopts.data_path )
126
+ reader.load_countries( name, myopts.data_path )
127
127
  elsif myopts.regions?
128
- reader.load_regions_with_include_path( myopts.country, name, myopts.data_path )
128
+ reader.load_regions( myopts.country, name, myopts.data_path )
129
129
  elsif myopts.cities?
130
- reader.load_cities_with_include_path( myopts.country, name, myopts.data_path )
130
+ reader.load_cities( myopts.country, name, myopts.data_path )
131
131
  else
132
- reader.load_with_include_path( name, myopts.data_path )
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
 
@@ -4,14 +4,10 @@ module WorldDB::Models
4
4
 
5
5
  class Prop < ActiveRecord::Base
6
6
 
7
- def self.create_from_worlddb_fixture!( name, path )
7
+ def self.create_from_fixture!( name, path )
8
8
  key = "db.#{fixture_name_to_prop_key(name)}.version"
9
9
 
10
- if path.starts_with?( WorldDB.data_path )
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
@@ -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( logger=nil )
11
- if logger.nil?
12
- @logger = LogUtils::Logger.new
13
- else
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 load_with_include_path( name, include_path )
22
+ def load( name, include_path )
22
23
 
23
24
  if name =~ /^lang/
24
- load_langs_with_include_path( name, include_path )
25
+ load_langs( name, include_path )
25
26
  elsif name =~ /\/lang/
26
- load_usages_with_include_path( name, include_path )
27
+ load_usages( name, include_path )
27
28
  elsif name =~ /\/fifa/
28
- load_xxx_with_include_path( 'fifa', name, include_path )
29
+ load_xxx( 'fifa', name, include_path )
29
30
  elsif name =~ /\/iso3/
30
- load_xxx_with_include_path( 'iso3', name, include_path )
31
+ load_xxx( 'iso3', name, include_path )
31
32
  elsif name =~ /\/internet/
32
- load_xxx_with_include_path( 'net', name, include_path )
33
+ load_xxx( 'net', name, include_path )
33
34
  elsif name =~ /\/motor/
34
- load_xxx_with_include_path( 'motor', name, include_path )
35
+ load_xxx( 'motor', name, include_path )
35
36
  elsif name =~ /^tag.*\.(\d)$/
36
- load_tags_with_include_path( name, include_path, :grade => $1.to_i )
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
- load_countries_with_include_path( name, include_path, :tags => $1 )
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
- load_cities_with_include_path( $1, name, include_path )
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
- load_regions_with_include_path( $1, name, include_path )
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 load_countries_with_include_path( name, include_path, more_values={} )
54
- load_fixtures_with_include_path_for( Country, name, include_path, more_values )
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 load_regions_with_include_path( country_key, name, include_path )
59
+ def load_regions( country_key, name, include_path )
59
60
  country = Country.find_by_key!( country_key )
60
- logger.info "Country #{country.key} >#{country.title} (#{country.code})<"
61
+ logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
61
62
 
62
- load_fixtures_with_include_path_for( Region, name, include_path, country_id: country.id )
63
+ load_fixtures_for( Region, name, include_path, country_id: country.id )
63
64
  end
64
65
 
65
66
 
66
- def load_cities_with_include_path( country_key, name, include_path )
67
+ def load_cities( country_key, name, include_path )
67
68
  country = Country.find_by_key!( country_key )
68
- logger.info "Country #{country.key} >#{country.title} (#{country.code})<"
69
+ logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
69
70
 
70
- load_fixtures_with_include_path_for( City, name, include_path, country_id: country.id )
71
+ load_fixtures_for( City, name, include_path, country_id: country.id )
71
72
  end
72
73
 
73
74
 
74
- def load_langs_with_include_path( name, include_path )
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 "*** parsing data '#{name}' (#{path})..."
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
- logger.debug "adding lang >>#{key}<< >>#{value}<<..."
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
- lang_key = key.strip
86
- lang_title = value.strip
101
+ lang_key = key.strip
102
+ lang_title = value.strip
87
103
 
88
- lang_attribs = {}
104
+ lang_attribs = {}
89
105
 
90
- ## check if it exists
91
- lang = Lang.find_by_key( lang_key )
92
- if lang.present?
93
- puts "*** update lang #{lang.id}-#{lang.key}:"
94
- else
95
- puts "*** create lang:"
96
- lang = Lang.new
97
- lang_attribs[ :key ] = lang_key
98
- end
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
- lang_attribs[ :title ] = lang_title
116
+ lang_attribs[ :title ] = lang_title
101
117
 
102
- puts lang_attribs.to_json
103
-
104
- lang.update_attributes!( lang_attribs )
105
- end # each key,value
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
- puts "*** parsing data '#{name}' (#{path})..."
127
+ def load_tags( name, include_path, more_values={} )
128
+
129
+ with_path_for( name, include_path) do |path|
115
130
 
116
- reader = HashReader.new( logger, path )
131
+ ### fix/todo/cleanup: HashReader -> make logger a keyword option only; do NOT use here
132
+ reader = HashReader.new( logger, path )
117
133
 
118
- grade = 1
134
+ grade = 1
119
135
 
120
- if more_values[:grade].present?
121
- grade = more_values[:grade].to_i
122
- end
136
+ if more_values[:grade].present?
137
+ grade = more_values[:grade].to_i
138
+ end
123
139
 
124
- reader.each do |key, value|
125
- ### split value by comma (e.g. northern america,southern america, etc.)
126
- puts "adding grade #{grade} tags >>#{key}<< >>#{value}<<..."
127
- tag_pairs = value.split(',')
128
- tag_pairs.each do |pair|
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
- values = pair.split('|')
146
+ values = pair.split('|')
131
147
 
132
- key = values[0]
133
- ### remove (optional comment) from key (e.g. carribean (islands))
134
- key = key.gsub( /\(.+\)/, '' )
135
- ## remove leading n trailing space
136
- key = key.strip
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
- title = values[1] || '' # nb: title might be empty/missing
139
- title = title.strip
154
+ title = values[1] || '' # nb: title might be empty/missing
155
+ title = title.strip
140
156
 
141
- tag_attribs = {}
157
+ tag_attribs = {}
142
158
 
143
- ## check if it exists
144
- ## todo/fix: add country_id for lookup?
145
- tag = Tag.find_by_key( key )
146
- if tag.present?
147
- puts "*** update tag #{tag.id}-#{tag.key}:"
148
- else
149
- puts "*** create tag:"
150
- tag = Tag.new
151
- tag_attribs[ :key ] = key
152
- end
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
- tag_attribs[ :title ] = title
155
- tag_attribs[ :grade ] = grade
170
+ tag_attribs[ :title ] = title
171
+ tag_attribs[ :grade ] = grade
156
172
 
157
- puts tag_attribs.to_json
173
+ logger.debug tag_attribs.to_json
158
174
 
159
- tag.update_attributes!( tag_attribs )
160
- end
161
- end # each key,value
162
-
163
- Prop.create_from_worlddb_fixture!( name, path )
164
- end # method load_tags_with_include_path
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 load_usages_with_include_path( name, include_path )
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.create_from_worlddb_fixture!( name, path )
209
+ Prop.create_from_fixture!( name, path )
193
210
  end
194
211
 
195
212
 
196
- def load_xxx_with_include_path( xxx, name, include_path )
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.create_from_worlddb_fixture!( name, path )
226
+ Prop.create_from_fixture!( name, path )
210
227
  end
211
228
 
212
229
 
213
230
  private
214
- def load_fixtures_with_include_path_for( clazz, name, include_path, more_values={} ) # load from file system
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.create_from_worlddb_fixture!( name, path )
240
+ Prop.create_from_fixture!( name, path )
224
241
  end
225
242
 
226
243
 
@@ -1,4 +1,4 @@
1
1
 
2
2
  module WorldDB
3
- VERSION = '1.0.1' # sync version w/ sport.db - why? why not?
3
+ VERSION = '1.1.0' # sync version w/ sport.db - why? why not?
4
4
  end
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.load_with_include_path( name, include_path )
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.load_with_include_path( name, include_path )
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
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-20 00:00:00 Z
18
+ date: 2013-02-21 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: textutils