Class: WorldDb::Model::Country

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
TextUtils::TagHelper, TextUtils::ValueHelper
Defined in:
lib/worlddb/models/country.rb,
lib/worlddb/models/forward.rb,
lib/worlddb/models/country_comp.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

Class Method Details

.create_or_update_from_attribs(new_attributes, values, opts = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
# File 'lib/worlddb/models/country.rb', line 178

def self.create_or_update_from_attribs( new_attributes, values, opts={} )

  ## opts e.g. :skip_tags true|false

  ## fix: add/configure logger for ActiveRecord!!!
  logger = LogKernel::Logger.root

    value_numbers     = []
    value_tag_keys    = []
    value_cities      = []

    ### check for "default" tags - that is, if present new_attributes[:tags] remove from hash
    value_tag_keys += find_tags_in_attribs!( new_attributes )


    new_attributes[ :c ] = true   # assume country type by default (use supra,depend to change)

    ## check for optional values
    values.each_with_index do |value,index|
      if match_supra_flag( value ) do |_|  # supra(national)
           new_attributes[ :c ] = false   # turn off default c|country flag; make it s|supra only
           new_attributes[ :s ] = true
           ## auto-add tag supra
           value_tag_keys << 'supra'
         end
      elsif match_supra( value ) do |country| # supra:
              new_attributes[ :country_id ] = country.id
            end
      elsif match_country( value ) do |country| # country:
              new_attributes[ :country_id ] = country.id
              new_attributes[ :c ] = false # turn off default c|country flag; make it d|depend only
              new_attributes[ :d ] = true
              ## auto-add tag supra
              value_tag_keys << 'territory'  # rename tag to dependency? why? why not?
            end
      elsif match_km_squared( value ) do |num|  # allow numbers like 453 km²
              value_numbers << num
            end
      elsif match_number( value ) do |num|  # numeric (nb: can use any _ or spaces inside digits e.g. 1_000_000 or 1 000 000)
              value_numbers << num
            end
      elsif value =~ /#{COUNTRY_CODE_PATTERN}/  ## three letter code 
        new_attributes[ :code ] = value
      elsif (values.size==(index+1)) && is_taglist?( value )   # tags must be last entry
        logger.debug "   found tags: >>#{value}<<"
        value_tag_keys += find_tags( value )
      else
        
        ### assume it is the capital city - mark it for auto add
        value_cities << value
        next

        # issue warning: unknown type for value
        # logger.warn "unknown type for value >#{value}<"
      end
    end # each value

    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 )

    #################
    ## auto add capital cities

    City.create_or_update_from_titles( value_cities, country_id: rec.id )

    ##################
    ## add taggings 

    if value_tag_keys.size > 0
      
      if opts[:skip_tags].present?
        logger.debug "   skipping add taggings (flag skip_tag)"
      else
        value_tag_keys.uniq!  # remove duplicates
        logger.debug "   adding #{value_tag_keys.size} taggings: >>#{value_tag_keys.join('|')}<<..."

        ### fix/todo: check tag_ids and only update diff (add/remove ids)

        value_tag_keys.each do |key|
          tag = Tag.find_by_key( key )
          if tag.nil?  # create tag if it doesn't exit
            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



167
168
169
170
171
172
173
174
175
# File 'lib/worlddb/models/country.rb', line 167

def self.create_or_update_from_values( values, more_attribs={} )

    ## key & title
    ## NB: three-letter code (.e.g AUT) required - enforce in values? why? why not?
    attribs, more_values = find_key_n_title( values )
    attribs = attribs.merge( more_attribs )

    Country.create_or_update_from_attribs( attribs, more_values )
end

.search_by_name(q) ⇒ Object

todo/check: just use search (rename)? why? why not?



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
# File 'lib/worlddb/models/country.rb', line 123

def self.search_by_name( q )    ## todo/check: just use search (rename)? why? why not?

  ## fix: add/configure logger for ActiveRecord!!!
  ## logger = LogKernel::Logger.root

  name = q.strip

  ## 1) first try 1:1 (exact) match
  cty = Country.find_by_name( name )   # NOTE: assume AR escapes quotes in name ??
  if cty.nil?
    ## 2) retry: a) remove all (..) enclosed
    ##           b) remove all extra spaces (e.g. Cocos (Keeling) Islands => Cocos__Islands => Cocos_Islands)
    name = name.gsub( /\([^)]+\)/, '' ).strip
    name = name.gsub( /[ \t]{2,}/, ' ' )
    cty = Country.find_by_name( name )

    ### NOTE: escape ' for sql like clause
    ##   for now use '' for escapes, that is, double quotes
    ##  check - working for postgresql n sqlite?? 
    name_esc = name.gsub( /'/, "''" )

    ## 3) retry: use SQL like match
    ##    % is used to match *zero* or more occurrences of any characters
    ##  todo: check if it matches zero too
    if cty.nil?
      cty = Country.where( "name LIKE '%#{name_esc}%'" ).first
    end

    ## 4) retry: use SQL like match for alternative names match
    if cty.nil?
      cty = Country.where( "alt_names LIKE '%#{name_esc}%'" ).first
    end

    ## 5) retry: use SQL like match for historic names match (e.g. Burma for Myanmar etc.)
    ##  todo/check: make it optional (pass in opts hash to configure) - why? why not???
    if cty.nil?
      cty = Country.where( "hist_names LIKE '%#{name_esc}%'" ).first
    end
  end

  cty  # return cty (country); nil if not found
end

Instance Method Details

#all_names(opts = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/worlddb/models/country.rb', line 103

def all_names( opts={} )
  ### fix:
  ## allow to passing in sep or separator e.g. | or other

  return name if alt_names.blank?
  
  buf = ''
  buf << name
  buf << ' | '
  buf << alt_names.split('|').join(' | ')
  buf
end

#is_country?Boolean

Returns:

  • (Boolean)


98
# File 'lib/worlddb/models/country.rb', line 98

def is_country?()    c? == true;  end

#is_dependency?Boolean

Returns:

  • (Boolean)


99
# File 'lib/worlddb/models/country.rb', line 99

def is_dependency?() d? == true;  end

#is_misc?Boolean

Returns:

  • (Boolean)


100
# File 'lib/worlddb/models/country.rb', line 100

def is_misc?()       m? == true;  end

#is_supra?Boolean

NB: use is_ for flags to avoid conflict w/ assocs

Returns:

  • (Boolean)


97
# File 'lib/worlddb/models/country.rb', line 97

def is_supra?()      s? == true;  end

#iso2Object



18
# File 'lib/worlddb/models/country_comp.rb', line 18

def iso2()       alpha2;              end

#iso2=(value) ⇒ Object



19
# File 'lib/worlddb/models/country_comp.rb', line 19

def iso2=(value) self.alpha2 = value; end

#iso3Object



21
# File 'lib/worlddb/models/country_comp.rb', line 21

def iso3()       alpha3;              end

#iso3=(value) ⇒ Object



22
# File 'lib/worlddb/models/country_comp.rb', line 22

def iso3=(value) self.alpha3 = value; end

#on_createObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/worlddb/models/country.rb', line 58

def on_create
  place_rec = Place.create!( name: name, kind: place_kind )
  self.place_id = place_rec.id

  if slug.blank?
    ## todo: change and to n  (if en/english) ?? - why? why not?
    ##  remove subtitles/subnames e.g. () -- why? why not?

    ## remove translations []  e.g. México [Mexico] -> México etc.
    self.slug = TextUtils.slugify( name.gsub( /\[[^\]]+\]/, '' ) )
  end
end

#on_updateObject



71
72
73
74
75
76
77
# File 'lib/worlddb/models/country.rb', line 71

def on_update
  ## fix/todo: check - if name or kind changed - only update if changed ?? why? why not??
  place.update_attributes!( name: name, kind: place_kind )

  ## check if name changed -- possible?
  ## update slug too??
end

#parentObject

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_kindObject

use place_kind_of_code ??



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/worlddb/models/country.rb', line 79

def place_kind   # use place_kind_of_code ??
  if is_supra?
    'SUPR'
  elsif is_dependency?
    'TERR'
  elsif is_misc?  ## misc(ellaneous) country or dependent territory
    # todo: use different marker?
    # territory w/ shared or disputes claims e.g Antartica/Western Sahara/Paracel Islands pg Spratly Islands/etc. 
    'MISC'
  else
    'CNTY'
  end
end

#synonymsObject



25
# File 'lib/worlddb/models/country_comp.rb', line 25

def synonyms()       alt_names;      end

#synonyms=(value) ⇒ Object



26
# File 'lib/worlddb/models/country_comp.rb', line 26

def synonyms=(value) self.alt_names = value; end

#titleObject



13
# File 'lib/worlddb/models/country_comp.rb', line 13

def title()       name;              end

#title=(value) ⇒ Object



14
# File 'lib/worlddb/models/country_comp.rb', line 14

def title=(value) self.name = value; end

#title_w_synonyms(opts = {}) ⇒ Object

depreciated: use all_names instead



28
# File 'lib/worlddb/models/country_comp.rb', line 28

def title_w_synonyms( opts={} )  all_names( opts );  end

#to_path(opts = {}) ⇒ Object



117
118
119
120
# File 'lib/worlddb/models/country.rb', line 117

def to_path( opts={} )
  # e.g. europe/at-austria
  "#{continent.slug}/#{key}-#{slug}"
end