worlddb 1.6.6 → 1.7.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.
data/Rakefile CHANGED
@@ -19,7 +19,7 @@ Hoe.spec 'worlddb' do
19
19
  self.email = 'opensport@googlegroups.com'
20
20
 
21
21
  self.extra_deps = [
22
- ['textutils', '~> 0.5'], # e.g. >= 0.5 && <= 1.0
22
+ ['textutils', '~> 0.6'], # e.g. >= 0.6 && <= 1.0
23
23
  ['commander', '~> 4.1.3'],
24
24
  ['activerecord', '~> 3.2'] # NB: will include activesupport,etc.
25
25
  ### ['sqlite3', '~> 1.3'] # NB: install on your own; remove dependency
@@ -4,13 +4,13 @@ module WorldDb::Models
4
4
 
5
5
  class City < ActiveRecord::Base
6
6
 
7
- extend WorldDb::TagHelper # will add self.find_tags, self.find_tags_in_hash!, etc.
7
+ extend TextUtils::TagHelper # will add self.find_tags, self.find_tags_in_attribs!, etc.
8
8
 
9
9
  # NB: use extend - is_<type>? become class methods e.g. self.is_<type>? for use in
10
10
  # self.create_or_update_from_values
11
- extend TextUtils::ValueHelper # e.g. is_year?, is_region?, is_address?, is_taglist? etc.
11
+ extend TextUtils::ValueHelper # e.g. self.is_year?, self.is_region?, self.is_address?, self.is_taglist? etc.
12
+
12
13
 
13
-
14
14
  self.table_name = 'cities'
15
15
 
16
16
  belongs_to :country, :class_name => 'Country', :foreign_key => 'country_id'
@@ -70,90 +70,31 @@ class City < ActiveRecord::Base
70
70
  end
71
71
 
72
72
 
73
- def self.create_from_ary!( cities, more_values={} )
74
- cities.each do |values|
75
-
76
- ## key & title & country required
77
- attr = {
78
- key: values[0]
79
- }
80
-
81
- ## title (split of optional synonyms)
82
- # e.g. FC Bayern Muenchen|Bayern Muenchen|Bayern
83
- titles = values[1].split('|')
84
-
85
- attr[ :title ] = titles[0]
86
- ## add optional synonyms
87
- attr[ :synonyms ] = titles[1..-1].join('|') if titles.size > 1
88
-
89
- attr = attr.merge( more_values )
90
-
91
- value_numbers = []
92
-
93
- ## check for optional values
94
- values[2..-1].each do |value|
95
- if value.is_a? Country
96
- attr[ :country_id ] = value.id
97
- elsif value.is_a? Numeric
98
- value_numbers << value
99
- elsif value =~ /^[A-Z_]{3}$/ ## assume its three letter code (e.g. NYC,VIE,etc.)
100
- attr[ :code ] = value
101
- elsif value =~ /^region:/ ## region:
102
- value_region_key = value[7..-1] ## cut off region: prefix
103
- value_region = Region.find_by_key!( value_region_key )
104
- attr[ :region_id ] = value_region.id
105
- else
106
- # issue warning: unknown type for value
107
- end
108
- end
109
-
110
- if value_numbers.size > 0
111
- attr[ :pop ] = value_numbers[0]
112
- attr[ :area ] = value_numbers[1]
113
- end
73
+ def self.create_or_update_from_values( values, more_attribs={} )
74
+ ## key & title & country required
114
75
 
115
-
116
- City.create!( attr )
117
- end # each city
118
- end
76
+ attribs, more_values = find_key_n_title( values )
77
+ attribs = attribs.merge( more_attribs )
119
78
 
79
+ ## check for optional values
80
+ City.create_or_update_from_attribs( attribs, more_values )
81
+ end
120
82
 
121
- def self.create_or_update_from_titles( titles, more_attributes = {} ) # ary of titles e.g. ['Wien', 'Graz'] etc.
122
83
 
123
- # fix: add/configure logger for ActiveRecord!!!
124
- logger = LogKernel::Logger.root
84
+ def self.create_or_update_from_titles( titles, more_attribs = {} )
85
+ # ary of titles e.g. ['Wien', 'Graz'] etc.
125
86
 
126
87
  titles.each do |title|
127
-
128
- new_attributes = {}
129
- key = TextUtils.title_to_key( title ) # auto generate key from title
130
-
131
- # check if it exists
132
- # todo/fix: add country_id for lookup?
133
- city = City.find_by_key( key )
134
- if city.present?
135
- logger.debug "update city #{city.id}-#{city.key}:"
136
- else
137
- logger.debug "create city:"
138
- city = City.new
139
- new_attributes[ :key ] = key
140
- end
141
-
142
- new_attributes[ :title ] = title
143
-
144
- ## merge in passed in attribes (e.g. country_id etc.)
145
- new_attributes.merge!( more_attributes )
146
-
147
- logger.debug new_attributes.to_json
148
-
149
- city.update_attributes!( new_attributes )
150
-
151
- ### todo/fix: add captial ref to country/region
88
+ values = [title]
89
+ City.create_or_update_from_values( values, more_attribs )
152
90
  end # each city
153
91
  end # method create_or_update_from_titles
154
92
 
155
93
 
156
- def self.create_or_update_from_values( new_attributes, values, opts={} )
94
+
95
+ def self.create_or_update_from_attribs( new_attributes, values, opts={} )
96
+ # attribs -> key/value pairs e.g. hash
97
+ # values -> ary of string values/strings (key not yet known; might be starting of value e.g. city:wien)
157
98
 
158
99
  ## opts e.g. :skip_tags true|false
159
100
 
@@ -164,11 +105,14 @@ class City < ActiveRecord::Base
164
105
  value_tag_keys = []
165
106
 
166
107
  ### check for "default" tags - that is, if present new_attributes[:tags] remove from hash
167
- value_tag_keys += find_tags_in_hash!( new_attributes )
108
+ value_tag_keys += find_tags_in_attribs!( new_attributes )
168
109
 
169
110
  new_attributes[ :c ] = true # assume city type by default (use metro,district to change in fixture)
170
111
 
171
112
  ## check for optional values
113
+
114
+ ### fix: todo: use matcher from ValueHelper !!!!!
115
+
172
116
  values.each_with_index do |value,index|
173
117
  if value =~ /^region:/ ## region:
174
118
  value_region_key = value[7..-1] ## cut off region: prefix
@@ -263,6 +207,7 @@ class City < ActiveRecord::Base
263
207
  rec
264
208
  end # method create_or_update_from_values
265
209
 
210
+
266
211
  end # class Cities
267
212
 
268
213
  end # module WorldDb::Models
@@ -4,11 +4,11 @@ module WorldDb::Models
4
4
 
5
5
  class Country < ActiveRecord::Base
6
6
 
7
- extend WorldDb::TagHelper # will add self.find_tags, self.find_tags_in_hash!, etc.
7
+ extend TextUtils::TagHelper # will add self.find_tags, self.find_tags_in_attribs!, etc.
8
8
 
9
9
  # NB: use extend - is_<type>? become class methods e.g. self.is_<type>? for use in
10
10
  # self.create_or_update_from_values
11
- extend TextUtils::ValueHelper # e.g. is_year?, is_region?, is_address?, is_taglist? etc.
11
+ extend TextUtils::ValueHelper # e.g. self.is_year?, self.is_region?, self.is_address?, is_taglist? etc.
12
12
 
13
13
 
14
14
  self.table_name = 'countries'
@@ -65,46 +65,18 @@ class Country < ActiveRecord::Base
65
65
  end
66
66
 
67
67
 
68
- def self.create_from_ary!( countries )
69
- countries.each do |values|
70
-
71
- ## key & title required
72
- attr = {
73
- :key => values[0],
74
- :title => values[1],
75
- :code => values[2]
76
- }
77
-
78
- value_numbers = []
79
-
80
- ## check for optional values
81
- values[3..-1].each do |value|
82
- if value.is_a? Numeric
83
- value_numbers << value
84
- elsif value =~ /^motor:/
85
- value_motor = value[6..-1] ## cut off region: motor
86
- attr[ :motor ] = value_motor
87
- elsif value =~ /^tags:/
88
- value_tags = value[5..-1] ## cut off tags: prefix
89
- # do nothing now
90
- else
91
- # issue warning: unknown type for value
92
- end
93
- end
94
-
95
- if value_numbers.size > 0
96
- attr[ :area ] = value_numbers[0] # NB: area for countries goes first
97
- attr[ :pop ] = value_numbers[1]
98
- end
99
-
100
-
101
-
102
- Country.create!( attr )
103
- end # each country
68
+ def self.create_or_update_from_values( values, more_attribs={} )
69
+
70
+ ## key & title
71
+ ## NB: three-letter code (.e.g AUT) required - enforce in values? why? why not?
72
+ attribs, more_values = find_key_n_title( values )
73
+ attribs = attribs.merge( more_attribs )
74
+
75
+ Country.create_or_update_from_attribs( attribs, more_values )
104
76
  end
105
77
 
106
78
 
107
- def self.create_or_update_from_values( new_attributes, values, opts={} )
79
+ def self.create_or_update_from_attribs( new_attributes, values, opts={} )
108
80
 
109
81
  ## opts e.g. :skip_tags true|false
110
82
 
@@ -116,7 +88,7 @@ class Country < ActiveRecord::Base
116
88
  value_cities = []
117
89
 
118
90
  ### check for "default" tags - that is, if present new_attributes[:tags] remove from hash
119
- value_tag_keys += find_tags_in_hash!( new_attributes )
91
+ value_tag_keys += find_tags_in_attribs!( new_attributes )
120
92
 
121
93
 
122
94
  new_attributes[ :c ] = true # assume country type by default (use supra,depend to change)
@@ -4,7 +4,7 @@ module WorldDb::Models
4
4
 
5
5
  class Region < ActiveRecord::Base
6
6
 
7
- extend WorldDb::TagHelper # will add self.find_tags, self.find_tags_in_hash!, etc.
7
+ extend TextUtils::TagHelper # will add self.find_tags, self.find_tags_in_attribs!, etc.
8
8
 
9
9
  # NB: use extend - is_<type>? become class methods e.g. self.is_<type>? for use in
10
10
  # self.create_or_update_from_values
@@ -33,32 +33,19 @@ class Region < ActiveRecord::Base
33
33
  end
34
34
 
35
35
 
36
- def self.create_from_ary!( regions, more_values={} )
37
- regions.each do |values|
38
-
39
- ## key & title & country required
40
- attr = {
41
- key: values[0],
42
- title: values[1]
43
- }
44
36
 
45
- attr = attr.merge( more_values )
46
-
47
- ## check for optional values
48
- values[2..-1].each do |value|
49
- if value.is_a? Country
50
- attr[ :country_id ] = value.id
51
- else
52
- # issue warning: unknown type for value
53
- end
54
- end
55
-
56
- Region.create!( attr )
57
- end # each region
37
+ def self.create_or_update_from_values( values, more_attribs={} )
38
+
39
+ ## key & title & country required
40
+ attribs, more_values = find_key_n_title( values )
41
+ attribs = attribs.merge( more_attribs )
42
+
43
+ ## check for optional values
44
+ Region.create_or_update_from_attribs( attribs, more_values )
58
45
  end
59
46
 
60
47
 
61
- def self.create_or_update_from_values( new_attributes, values, opts={} )
48
+ def self.create_or_update_from_attribs( new_attributes, values, opts={} )
62
49
 
63
50
  ## opts e.g. :skip_tags true|false
64
51
 
@@ -70,7 +57,7 @@ class Region < ActiveRecord::Base
70
57
  value_cities = []
71
58
 
72
59
  ### check for "default" tags - that is, if present new_attributes[:tags] remove from hash
73
- value_tag_keys += find_tags_in_hash!( new_attributes )
60
+ value_tag_keys += find_tags_in_attribs!( new_attributes )
74
61
 
75
62
  ## check for optional values
76
63
  values.each_with_index do |value,index|
@@ -3,40 +3,6 @@
3
3
  module WorldDb
4
4
 
5
5
 
6
- ### fix: move to textutils
7
- ## PlusReaderWrapper find a better name than Plus?
8
- #
9
- # todo: also add a ValuesReaderPlus and use it
10
-
11
- class HashReaderPlus
12
- include LogUtils::Logging
13
-
14
- def initialize( name, include_path )
15
- @name = name
16
- @include_path = include_path
17
- end
18
-
19
- attr_reader :name
20
- attr_reader :include_path
21
-
22
- def each
23
- path = "#{include_path}/#{name}.yml"
24
- reader = HashReader.new( path )
25
-
26
- logger.info "parsing data '#{name}' (#{path})..."
27
-
28
- reader.each do |key, value|
29
- yield( key, value )
30
- end
31
-
32
- WorldDb::Models::Prop.create_from_fixture!( name, path )
33
- end
34
-
35
- end # class HashReaderPlus
36
-
37
-
38
-
39
-
40
6
  class Reader
41
7
 
42
8
  include LogUtils::Logging
@@ -133,10 +99,10 @@ class Reader
133
99
  # todo/fix: exit w/ error
134
100
  end
135
101
  end
136
-
137
102
 
138
- def load_countries( name, more_values={} )
139
- load_fixtures_for( Country, name, more_values )
103
+
104
+ def load_countries( name, more_attribs={} )
105
+ load_fixtures_for( Country, name, more_attribs )
140
106
  end
141
107
 
142
108
 
@@ -152,7 +118,7 @@ class Reader
152
118
  country = Country.find_by_key!( country_key )
153
119
  logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
154
120
 
155
- reader = HashReaderPlus.new( name, include_path )
121
+ reader = HashReaderV2.new( name, include_path )
156
122
 
157
123
  reader.each do |key, value|
158
124
  region = Region.find_by_country_id_and_key!( country.id, key )
@@ -171,7 +137,7 @@ class Reader
171
137
 
172
138
 
173
139
  def load_continent_refs( name )
174
- reader = HashReaderPlus.new( name, include_path )
140
+ reader = HashReaderV2.new( name, include_path )
175
141
 
176
142
  reader.each do |key, value|
177
143
  country = Country.find_by_key!( key )
@@ -182,12 +148,8 @@ class Reader
182
148
  end
183
149
 
184
150
 
185
- def load_continent_defs( name, more_values={} )
186
- path = "#{include_path}/#{name}.txt"
187
-
188
- logger.info "parsing data '#{name}' (#{path})..."
189
-
190
- reader = ValuesReader.new( path, more_values )
151
+ def load_continent_defs( name, more_attribs={} )
152
+ reader = ValuesReaderV2.new( name, include_path, more_attribs )
191
153
 
192
154
  reader.each_line do |attribs, values|
193
155
 
@@ -209,15 +171,12 @@ class Reader
209
171
  rec.update_attributes!( attribs )
210
172
 
211
173
  end # each lines
212
-
213
- Prop.create_from_fixture!( name, path )
214
-
215
174
  end # load_continent_defs
216
175
 
217
176
 
218
177
  def load_langs( name )
219
-
220
- reader = HashReaderPlus.new( name, include_path )
178
+
179
+ reader = HashReaderV2.new( name, include_path )
221
180
 
222
181
  reader.each do |key, value|
223
182
 
@@ -248,14 +207,14 @@ class Reader
248
207
  end # method load_langs
249
208
 
250
209
 
251
- def load_tags( name, more_values={} )
210
+ def load_tags( name, more_attribs={} )
252
211
 
253
- reader = HashReaderPlus.new( name, include_path )
212
+ reader = HashReaderV2.new( name, include_path )
254
213
 
255
214
  grade = 1
256
215
 
257
- if more_values[:grade].present?
258
- grade = more_values[:grade].to_i
216
+ if more_attribs[:grade].present?
217
+ grade = more_attribs[:grade].to_i
259
218
  end
260
219
 
261
220
  reader.each do |key, value|
@@ -301,7 +260,7 @@ class Reader
301
260
 
302
261
 
303
262
  def load_usages( name )
304
- reader = HashReaderPlus.new( name, include_path )
263
+ reader = HashReaderV2.new( name, include_path )
305
264
 
306
265
  reader.each do |key, value|
307
266
  logger.debug " adding langs >>#{value}<<to country >>#{key}<<"
@@ -324,7 +283,7 @@ class Reader
324
283
 
325
284
 
326
285
  def load_xxx( xxx, name )
327
- reader = HashReaderPlus.new( name, include_path )
286
+ reader = HashReaderV2.new( name, include_path )
328
287
 
329
288
  reader.each do |key, value|
330
289
  country = Country.find_by_key!( key )
@@ -334,20 +293,14 @@ class Reader
334
293
  end
335
294
 
336
295
  private
337
- def load_fixtures_for( clazz, name, more_values={} ) # load from file system
338
- path = "#{include_path}/#{name}.txt"
339
-
340
- logger.info "parsing data '#{name}' (#{path})..."
341
-
342
- reader = ValuesReader.new( path, more_values )
296
+ def load_fixtures_for( clazz, name, more_attribs={} )
297
+ reader = ValuesReaderV2.new( name, include_path, more_attribs )
343
298
 
344
- reader.each_line do |new_attributes, values|
299
+ reader.each_line do |attribs, values|
345
300
  opts = { skip_tags: skip_tags? }
346
- clazz.create_or_update_from_values( new_attributes, values, opts )
301
+ clazz.create_or_update_from_attribs( attribs, values, opts )
347
302
  end
348
-
349
- Prop.create_from_fixture!( name, path )
350
303
  end
351
-
304
+
352
305
  end # class Reader
353
306
  end # module WorldDb
@@ -17,38 +17,3 @@ class Time
17
17
 
18
18
  end # class Time
19
19
 
20
-
21
- ##### fix/todo: move to helper folder - use one file per module/helper
22
-
23
- module WorldDb
24
- module TagHelper
25
-
26
- def find_tags( value )
27
- # logger.debug " found tags: >>#{value}<<"
28
-
29
- tag_keys = value.split('|')
30
-
31
- ## unify; replace _w/ space; remove leading n trailing whitespace
32
- tag_keys = tag_keys.map do |key|
33
- key = key.gsub( '_', ' ' )
34
- key = key.strip
35
- key
36
- end
37
-
38
- tag_keys # return tag keys as ary
39
- end
40
-
41
- def find_tags_in_hash!( h )
42
- # NB: will remove :tags from hash
43
-
44
- if h[:tags].present?
45
- tag_keys = find_tags( h[:tags] )
46
- h.delete(:tags)
47
- tag_keys # return tag keys as ary
48
- else
49
- [] # nothing found; return empty ary
50
- end
51
- end
52
-
53
- end # module
54
- end # module WorldDb
@@ -1,6 +1,6 @@
1
1
 
2
2
  module WorldDb
3
- VERSION = '1.6.6' # sync version w/ sport.db - why? why not?
3
+ VERSION = '1.7.0' # sync version w/ sport.db - why? why not?
4
4
  end
5
5
 
6
6
  ###########################################
@@ -30,7 +30,7 @@ class TestValues < MiniTest::Unit::TestCase
30
30
  'un|fifa|uefa|eu|euro|schengen|central_europe|western_europe'
31
31
  ]
32
32
 
33
- c = Country.create_or_update_from_values( new_attributes, values )
33
+ c = Country.create_or_update_from_attribs( new_attributes, values )
34
34
 
35
35
  c2 = Country.find_by_key!( new_attributes[:key] )
36
36
  assert( c.id == c2.id )
@@ -61,7 +61,7 @@ class TestValues < MiniTest::Unit::TestCase
61
61
  'eastern austria'
62
62
  ]
63
63
 
64
- r = Region.create_or_update_from_values( new_attributes, values )
64
+ r = Region.create_or_update_from_attribs( new_attributes, values )
65
65
 
66
66
  r2 = Region.find_by_key!( new_attributes[:key] )
67
67
  assert( r.id == r2.id )
@@ -96,7 +96,7 @@ class TestValues < MiniTest::Unit::TestCase
96
96
  'm:1_724_000'
97
97
  ]
98
98
 
99
- c = City.create_or_update_from_values( new_attributes, values )
99
+ c = City.create_or_update_from_attribs( new_attributes, values )
100
100
 
101
101
  c2 = City.find_by_key!( new_attributes[:key] )
102
102
  assert( c.id == c2.id )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worlddb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.6
4
+ version: 1.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-12 00:00:00.000000000 Z
12
+ date: 2013-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: textutils
16
- requirement: &87983620 !ruby/object:Gem::Requirement
16
+ requirement: &81436760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0.5'
21
+ version: '0.6'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *87983620
24
+ version_requirements: *81436760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: commander
27
- requirement: &87983400 !ruby/object:Gem::Requirement
27
+ requirement: &81436540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 4.1.3
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *87983400
35
+ version_requirements: *81436540
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activerecord
38
- requirement: &87983190 !ruby/object:Gem::Requirement
38
+ requirement: &81436330 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.2'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *87983190
46
+ version_requirements: *81436330
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rdoc
49
- requirement: &87982970 !ruby/object:Gem::Requirement
49
+ requirement: &81436110 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '3.10'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *87982970
57
+ version_requirements: *81436110
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: hoe
60
- requirement: &87982750 !ruby/object:Gem::Requirement
60
+ requirement: &81435890 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '3.3'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *87982750
68
+ version_requirements: *81435890
69
69
  description: worlddb - world.db command line tool
70
70
  email: opensport@googlegroups.com
71
71
  executables: