Class: Fifa::CountryIndex

Inherits:
Object
  • Object
show all
Includes:
SportDb::NameHelper
Defined in:
lib/fifa/org_index.rb,
lib/fifa/country_index.rb

Overview

built-in countries for (quick starter) auto-add

Defined Under Namespace

Classes: OrgIndex

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recs = nil) ⇒ CountryIndex

Returns a new instance of CountryIndex.



17
18
19
20
21
22
23
24
# File 'lib/fifa/country_index.rb', line 17

def initialize( recs=nil )
  @countries  = []
  @by_code    = {}  ## countries by codes (fifa, internet, etc)
  @by_name    = {}  ##  normalized name
  @orgs       = OrgIndex.new

  add( recs )   if recs
end

Class Method Details

.read(*paths) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/fifa/country_index.rb', line 8

def self.read( *paths )
  recs = []
  paths.each do |path|
    recs += CountryReader.read( path )
  end
  new( recs )
end

Instance Method Details

#_add(recs) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fifa/country_index.rb', line 71

def _add( recs )
  recs.each do |rec|
    ## step 1 - add code lookups - key, fifa, ...
    rec.codes.each do |code|
      if @by_code.has_key?( code )
        puts "** !!! ERROR - country code already in use >#{code}<; sorry - no duplicates allowed!!"
        pp rec
        pp @by_code[ code ]
        exit 1
      else
        @by_code[ code ] = rec
      end
    end

    ## step 2 - add name lookups
    names = rec.names
    ## remove year and norm
    names = names.map { |name| normalize( unaccent( strip_year( name ))) }

    ## note - allow duplicates only if same country
    ##   e.g Panama, Panamá
    names = names.uniq

    names.each do |name|
      if @by_name.has_key?( name )
        puts "** !!! ERROR - country (norm) name already in use >#{name}<; sorry - no duplicates allowed!!"
        pp rec
        pp @by_name[ name ]
        exit 1
      else
        @by_name[ name ] = rec
      end
    end
  end
end

#add(recs) ⇒ Object



63
64
65
66
67
68
# File 'lib/fifa/country_index.rb', line 63

def add( recs )
  @countries += recs

  _add( recs)              ## step one add to our own index
  @orgs.add( recs )   ## step two add to orgs (helper) index (for members)
end

#countriesObject



26
# File 'lib/fifa/country_index.rb', line 26

def countries() @countries; end

#each(&blk) ⇒ Object



28
# File 'lib/fifa/country_index.rb', line 28

def each( &blk ) @countries.each { |country| blk.call( country ) }; end

#find(q) ⇒ Object



41
42
43
44
# File 'lib/fifa/country_index.rb', line 41

def find( q )
  ## todo/fix - add find by name (find_by_name_and_code) - why? why not?
  find_by_code( q )
end

#find_by_code(code) ⇒ Object Also known as: []



46
47
48
49
# File 'lib/fifa/country_index.rb', line 46

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

#find_by_name(name) ⇒ Object



52
53
54
55
# File 'lib/fifa/country_index.rb', line 52

def find_by_name( name )
   key = normalize( unaccent( name.to_s ))
   @by_name[ key ]
end

#members(key = :fifa) ⇒ Object

default to fifa members



32
33
34
# File 'lib/fifa/country_index.rb', line 32

def members( key=:fifa )   ## default to fifa members
  @orgs.members( key )
end

#orgsObject

for testing/debugging return org keys

return OrgIndex instead - why? why not?


38
# File 'lib/fifa/country_index.rb', line 38

def orgs() @orgs.keys; end

#sizeObject



27
# File 'lib/fifa/country_index.rb', line 27

def size()  @countries.size; end