Class: WorldDb::Model::Country
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- WorldDb::Model::Country
show all
- Extended by:
- TextUtils::TagHelper, TextUtils::ValueHelper
- Defined in:
- lib/worlddb/models/country.rb,
lib/worlddb/models/forward.rb
Overview
Country / Supra (e.g. European Union) / Territory (e.g. Puerto Rico) or Dependency (e.g. Dependent territory)
Class Method Summary
collapse
Instance Method Summary
collapse
is_state?, match_city, match_country, match_metro, match_metro_flag, match_metro_pop, match_state_for_country, match_supra, match_supra_flag
Class Method Details
.create_or_update_from_attribs(new_attributes, values, opts = {}) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
# File 'lib/worlddb/models/country.rb', line 202
def self.create_or_update_from_attribs( new_attributes, values, opts={} )
logger = LogKernel::Logger.root
value_numbers = []
value_tag_keys = []
value_cities = []
value_tag_keys += find_tags_in_attribs!( new_attributes )
new_attributes[ :c ] = true
values.each_with_index do |value,index|
if match_supra_flag( value ) do |_| new_attributes[ :c ] = false new_attributes[ :s ] = true
value_tag_keys << 'supra'
end
elsif match_supra( value ) do |country| new_attributes[ :country_id ] = country.id
end
elsif match_country( value ) do |country| new_attributes[ :country_id ] = country.id
new_attributes[ :c ] = false new_attributes[ :d ] = true
value_tag_keys << 'territory' end
elsif match_km_squared( value ) do |num| value_numbers << num
end
elsif match_number( value ) do |num| value_numbers << num
end
elsif value =~ /#{COUNTRY_CODE_PATTERN}/ new_attributes[ :code ] = value
elsif (values.size==(index+1)) && is_taglist?( value ) logger.debug " found tags: >>#{value}<<"
value_tag_keys += find_tags( value )
else
value_cities << value
next
end
end
if value_numbers.size > 0
new_attributes[ :area ] = value_numbers[0]
new_attributes[ :pop ] = value_numbers[1]
end
=begin
# auto-add tags
area = value_numbers[0]
pop = value_numbers[1]
# categorize into brackets
if area >= 1_000_000
value_tag_keys << 'area_1_000_000_n_up'
elsif area >= 100_000
value_tag_keys << 'area_100_000_to_1_000_000'
elsif area >= 1000
value_tag_keys << 'area_1_000_to_100_000'
else
value_tag_keys << 'area_1_000_n_less' # microstate
end
# include all
value_tag_keys << 'area_100_000_n_up' if area >= 100_000
value_tag_keys << 'area_1_000_n_up' if area >= 1_000
# categorize into brackets
if pop >= 100_000_000
value_tag_keys << 'pop_100m_n_up'
elsif pop >= 10_000_000
value_tag_keys << 'pop_10m_to_100m'
elsif pop >= 1_000_000
value_tag_keys << 'pop_1m_to_10m'
else
value_tag_keys << 'pop_1m_n_less'
end
# include all
value_tag_keys << 'pop_10m_n_up' if pop >= 10_000_000
value_tag_keys << 'pop_1m_n_up' if pop >= 1_000_000
=end
rec = Country.find_by_key( new_attributes[ :key ] )
if rec.present?
logger.debug "update Country #{rec.id}-#{rec.key}:"
else
logger.debug "create Country:"
rec = Country.new
end
logger.debug new_attributes.to_json
rec.update_attributes!( new_attributes )
City.parse( value_cities, country_id: rec.id )
if value_tag_keys.size > 0
if opts[:skip_tags].present?
logger.debug " skipping add taggings (flag skip_tag)"
else
value_tag_keys.uniq! logger.debug " adding #{value_tag_keys.size} taggings: >>#{value_tag_keys.join('|')}<<..."
value_tag_keys.each do |key|
tag = Tag.find_by_key( key )
if tag.nil? logger.debug " creating tag >#{key}<"
tag = Tag.create!( key: key )
end
rec.tags << tag
end
end
end
rec
end
|
.create_or_update_from_values(values, more_attribs = {}) ⇒ Object
191
192
193
194
195
196
197
198
199
|
# File 'lib/worlddb/models/country.rb', line 191
def self.create_or_update_from_values( values, more_attribs={} )
attribs, more_values = find_key_n_title( values )
attribs = attribs.merge( more_attribs )
Country.create_or_update_from_attribs( attribs, more_values )
end
|
.parse(*args) ⇒ Object
183
184
185
186
187
188
189
|
# File 'lib/worlddb/models/country.rb', line 183
def self.parse( *args )
more_attribs = args.last.is_a?(Hash) ? args.pop : {} values = args
self.create_or_update_from_values( values, more_attribs )
end
|
.search_by_name(q) ⇒ Object
todo/check: just use search (rename)? why? why not?
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/worlddb/models/country.rb', line 138
def self.search_by_name( q )
name = q.strip
cty = Country.find_by_name( name ) if cty.nil?
name = name.gsub( /\([^)]+\)/, '' ).strip
name = name.gsub( /[ \t]{2,}/, ' ' )
cty = Country.find_by_name( name )
name_esc = name.gsub( /'/, "''" )
if cty.nil?
cty = Country.where( "name LIKE '%#{name_esc}%'" ).first
end
if cty.nil?
cty = Country.where( "alt_names LIKE '%#{name_esc}%'" ).first
end
if cty.nil?
cty = Country.where( "hist_names LIKE '%#{name_esc}%'" ).first
end
end
cty end
|
Instance Method Details
#all_names(opts = {}) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/worlddb/models/country.rb', line 118
def all_names( opts={} )
return name if alt_names.blank?
buf = ''
buf << name
buf << ' | '
buf << alt_names.split('|').join(' | ')
buf
end
|
#as_row(opts = {}) ⇒ Object
#is_country? ⇒ Boolean
113
|
# File 'lib/worlddb/models/country.rb', line 113
def is_country?() c? == true; end
|
#is_dependency? ⇒ Boolean
114
|
# File 'lib/worlddb/models/country.rb', line 114
def is_dependency?() d? == true; end
|
#is_misc? ⇒ Boolean
115
|
# File 'lib/worlddb/models/country.rb', line 115
def is_misc?() m? == true; end
|
#is_supra? ⇒ Boolean
NB: use is_ for flags to avoid conflict w/ assocs
112
|
# File 'lib/worlddb/models/country.rb', line 112
def is_supra?() s? == true; end
|
#on_create ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/worlddb/models/country.rb', line 73
def on_create
place_rec = Place.create!( name: name, kind: place_kind )
self.place_id = place_rec.id
if slug.blank?
self.slug = TextUtils.slugify( name.gsub( /\[[^\]]+\]/, '' ) )
end
end
|
#on_update ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/worlddb/models/country.rb', line 86
def on_update
place.update_attributes!( name: name, kind: place_kind )
end
|
#parent ⇒ Object
self referencing hierachy within countries e.g. EU > GB > EN
30
|
# File 'lib/worlddb/models/country.rb', line 30
belongs_to :parent, class_name: 'Country', foreign_key: 'country_id'
|
#place_kind ⇒ Object
use place_kind_of_code ??
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/worlddb/models/country.rb', line 94
def place_kind if is_supra?
'SUPR'
elsif is_dependency?
'TERR'
elsif is_misc? 'MISC'
else
'CNTY'
end
end
|
#synonyms ⇒ Object
45
|
# File 'lib/worlddb/models/country.rb', line 45
def synonyms() alt_names; end
|
#synonyms=(value) ⇒ Object
46
|
# File 'lib/worlddb/models/country.rb', line 46
def synonyms=(value) self.alt_names = value; end
|
#title ⇒ Object
42
|
# File 'lib/worlddb/models/country.rb', line 42
def title() name; end
|
#title=(value) ⇒ Object
43
|
# File 'lib/worlddb/models/country.rb', line 43
def title=(value) self.name = value; end
|
#to_path(opts = {}) ⇒ Object
132
133
134
135
|
# File 'lib/worlddb/models/country.rb', line 132
def to_path( opts={} )
"#{continent.slug}/#{key}-#{slug}"
end
|