Class: SportDb::LeagueCodes

Inherits:
Object
  • Object
show all
Defined in:
lib/leagues/league_codes.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLeagueCodes



61
62
63
64
65
# File 'lib/leagues/league_codes.rb', line 61

def initialize
    ## keep to separate (hash) table for now - why? why not?

    @leagues = {}
    @codes   = {}
end

Class Method Details

.builtinObject

(static) helpers



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/leagues/league_codes.rb', line 25

def self.builtin
   ## get builtin league code index (build on demand)

   @leagues ||= begin
        leagues = SportDb::LeagueCodes.new
        ['leagues',
         'leagues_more',
        ].each do |name|
           recs = read_csv( "#{SportDb::Module::Leagues.root}/config/#{name}.csv" )
           leagues.add( recs )
        end

        ['codes_alt',
        ].each do |name|
           recs = read_csv( "#{SportDb::Module::Leagues.root}/config/#{name}.csv" )
           leagues.add_alt( recs )
        end
        leagues
   end
   @leagues
end

.find_by(code:, season:) ⇒ Object



17
18
19
20
# File 'lib/leagues/league_codes.rb', line 17

def self.find_by( code:, season: )
    ## return league code record/item or nil

    builtin.find_by( code: code, season: season )
end

.norm(code) ⇒ Object

use norm_(league)code - why? why not?



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/leagues/league_codes.rb', line 46

def self.norm( code )      ## use norm_(league)code - why? why not?

  ## norm league code

  ##   downcase

  ##   and remove all non-letters/digits e.g. at.1 => at1, at 1 => at1 etc.

  ##                                            ö.1 => ö1

  ##   note - allow unicode letters!!! 

  ##    note - assume downcase works for unicode too e.g. Ö=>ö

  ##           for now no need to use our own downcase - why? why not?


  code.downcase.gsub( /[^\p{Ll}0-9]/, '' )
end

.valid?(code) ⇒ Boolean

(public) api



12
13
14
15
# File 'lib/leagues/league_codes.rb', line 12

def self.valid?( code )   
    ## check if code is valid

    builtin.valid?( code )
end

Instance Method Details

#_find_alt_code(key, season) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/leagues/league_codes.rb', line 150

def _find_alt_code( key, season )
     ## check alt keys

     ref_key = nil
     recs = @codes[key]
     if recs
        rec = _find_by_season( recs, season )
        ## norm code

        ref_key = LeagueCodes.norm( rec['code'] )  if rec  
     end

     ref_key    ## return nil if no mapping found

end

#_find_by_season(recs, season) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/leagues/league_codes.rb', line 164

def _find_by_season( recs, season )
  recs.each do |rec|
      start_season = rec['start_season']
      end_season   = rec['end_season']
      return rec  if (start_season.nil? || start_season <= season) &&
                     (end_season.nil? || end_season >= season)
  end
  nil
end

#add(recs) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/leagues/league_codes.rb', line 68

def add( recs )
  recs.each do |rec|
    key = LeagueCodes.norm( rec['code'] )
    @leagues[ key ] ||= []

    ## note: auto-change seasons to season object or nil

    @leagues[ key ] << {  'code'         => rec['code'],
                          'name'         => rec['name'],
                          'basename'     => rec['basename'],
                          'start_season' => rec['start_season'].empty? ? nil : Season.parse( rec['start_season'] ),
                          'end_season'   => rec['end_season'].empty?   ? nil : Season.parse( rec['end_season'] ),
                       }
  end
end

#add_alt(recs) ⇒ Object

step two - add alt(ernative) codes



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/leagues/league_codes.rb', line 84

def add_alt( recs )
  recs.each do |rec|
    key = LeagueCodes.norm( rec['alt'] )
    @codes[ key ] ||= []

  ### double check code reference 

  ##    MUST be present for now!!

     ref_key = LeagueCodes.norm( rec['code'] )
     unless @leagues.has_key?( ref_key )
       raise ArgumentError, "league code >#{rec['code']}< for alt code >#{rec['alt']}< not found; sorry" 
     end  

    ## note: auto-change seasons to season object or nil

    @codes[ key ] << {  'code'         => rec['code'],
                        'alt'          => rec['alt'],
                        'start_season' => rec['start_season'].empty? ? nil : Season.parse( rec['start_season'] ),
                        'end_season'   => rec['end_season'].empty?   ? nil : Season.parse( rec['end_season'] ),
                     }
 end
end

#find_by(code:, season:) ⇒ Object

Raises:

  • (ArgumentError)


119
120
121
122
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
# File 'lib/leagues/league_codes.rb', line 119

def find_by( code:, season: )
  raise ArgumentError, "league code as string|symbol expected"  unless code.is_a?(String) || code.is_a?(Symbol)

  ## return league code record/item or nil

  ## check for alt code first

  season = Season( season )
  key    = LeagueCodes.norm( code )
  rec    = nil

  if !@leagues.has_key?( key )    ## try alt codes

     key = _find_alt_code( key, season )
  end

  if key
    recs = @leagues[ key ] 
    if recs
      rec =  _find_by_season( recs, season )
    end
  end


  if rec   ## (quick hack for now) auto-add timezone

      ## use canoncial (league) code

     rec['tz'] = find_zone!( league: rec['code'], season: season )
  end


  rec   ## return nil if no code record/item found

end

#valid?(code) ⇒ Boolean

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/leagues/league_codes.rb', line 106

def valid?( code )
  ## check if code is valid

  ##   1) canonical codes  (check first - why? why not?)

  ##   2) alt codes

  raise ArgumentError, "league code as string|symbol expected"  unless code.is_a?(String) || code.is_a?(Symbol)

  key = LeagueCodes.norm( code )
  found = @leagues.has_key?( key )
  found = @codes.has_key?( key )   if found == false
  found
end