Class: SportDb::Import::CountryIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/config/countries.rb

Instance Method Summary collapse

Constructor Details

#initialize(recs) ⇒ CountryIndex

Returns a new instance of CountryIndex.



14
15
16
17
18
19
# File 'lib/sportdb/config/countries.rb', line 14

def initialize( recs )
  @countries         = []
  @countries_by_code = {}

  add( recs )
end

Instance Method Details

#[](key) ⇒ Object

method initialize



50
51
52
53
# File 'lib/sportdb/config/countries.rb', line 50

def []( key )
  key = key.to_s.downcase   ## allow symbols (and always downcase e.g. AUT to aut etc.)
  @countries_by_code[ key ]
end

#add(recs) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sportdb/config/countries.rb', line 21

def add( recs )
  ###########################################
  ## auto-fill countries
  ## pp recs
  recs.each do |rec|
    ## rec e.g. { key:'af', fifa:'AFG', name:'Afghanistan'}

    @countries << rec

    ## add codes lookups - key, fifa, ...
    if @countries_by_code[ rec.key ]
      puts "** !! ERROR !! country code (key) >#{rec.key}< already exits!!"
      exit 1
    else
      @countries_by_code[ rec.key ] = rec
    end

    ## add fifa code (only) if different from key
    if rec.key != rec.fifa.downcase
      if @countries_by_code[ rec.fifa.downcase ]
        puts "** !! ERROR !! country code (fifa) >#{rec.fifa.downcase}< already exits!!"
        exit 1
      else
        @countries_by_code[ rec.fifa.downcase ] = rec
      end
    end
  end
end