Class: Fbup::LeagueConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/fbup/league_config.rb

Overview

check - rename to ExtraLeagueConfig or such - why? why not?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLeagueConfig

Returns a new instance of LeagueConfig.



46
47
48
# File 'lib/fbup/league_config.rb', line 46

def initialize
    @leagues = {}
end

Class Method Details

.builtinObject

(static) helpers



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fbup/league_config.rb', line 16

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

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

.find_by(code:, season:) ⇒ Object



8
9
10
11
# File 'lib/fbup/league_config.rb', line 8

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?



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fbup/league_config.rb', line 31

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

Instance Method Details

#_find_by_season(recs, season) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/fbup/league_config.rb', line 85

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



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fbup/league_config.rb', line 51

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

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

    @leagues[ key ] << {  'code'         => rec['code'],
                          '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

#find_by(code:, season:) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fbup/league_config.rb', line 66

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

  recs = @leagues[ key ] 

  if recs
    rec =  _find_by_season( recs, season )
  end

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

end