Class: SportDb::Sync::Country

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/sync/country.rb

Class Method Summary collapse

Class Method Details

.country(q) ⇒ Object

todo/fix:

add ALTERNATE / ALTERNATIVE COUNTRY KEYS!!!!
 e.g. d => de, a => at, en => eng, etc.
 plus  add all fifa codes too   aut => at, etc.  - why? why not?


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sportdb/sync/country.rb', line 10

def self.country( q )
   ## note: use built-in countries for mapping country keys/codes
   country = Import.catalog.countries.find( q )
   if country
    ## todo/check:  keep key mapping warning - useful? why? why not?
     if country.key != q.to_s
      puts "** note: mapping (normalizing) country key >#{q}< to >#{country.key}<"
     end
   else
     puts "** !!! ERROR !!! unknown / invalid country for key >#{q}<; sorry - add to COUNTRIES table"
     exit 1
   end
   country
end

.find(country) ⇒ Object

finders



41
42
43
# File 'lib/sportdb/sync/country.rb', line 41

def self.find( country )
  WorldDb::Model::Country.find_by( key: country.key )
end

.find!(country) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/sportdb/sync/country.rb', line 45

def self.find!( country )
   rec = find( country )
   if rec.nil?
       puts "** !!! ERROR !!! - country for key >#{country.key}< not found; sorry - add to COUNTRIES table"
       exit 1
   end
   rec
end

.find_or_create(country) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sportdb/sync/country.rb', line 54

def self.find_or_create( country )
  rec = find( country )
  if rec.nil?
    attribs = {
      key:  country.key,
      name: country.name,
      code: country.code,  ## fix:  uses fifa code now (should be iso-alpha3 if available)
      ## fifa: country.fifa,
      area: 1,
      pop:  1
    }
    rec = WorldDb::Model::Country.create!( attribs )
  end
  rec
end

.search!(q) ⇒ Object

searchers



28
29
30
31
# File 'lib/sportdb/sync/country.rb', line 28

def self.search!( q )
  country = country( q )
  find!( country )
end

.search_or_create!(q) ⇒ Object



33
34
35
36
# File 'lib/sportdb/sync/country.rb', line 33

def self.search_or_create!( q )
  country = country( q )
  find_or_create( country )
end