Class: CatalogDb::Metal::EventInfo

Inherits:
Record show all
Defined in:
lib/sportdb/catalogs/event_info.rb

Class Method Summary collapse

Methods inherited from Record

_city, _league, _to_city, _to_country, _to_league, database, database=, database?

Methods inherited from BaseRecord

_country, _to_bool, columns, columns=, count, execute, tablename, tablename=

Class Method Details

._build_event_info(row) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sportdb/catalogs/event_info.rb', line 16

def self._build_event_info( row )
   ## note: cache structs by key (do NOT rebuild duplicates; reuse)
   @cache ||= Hash.new
   ## note cache key MUST be unique!
   ##   use league PLUS season   (row[0] season only will NOT work)
   key = "#{row[0]}_#{row[1]}"
   @cache[ key ] ||= Sports::EventInfo.new(
                           league: _to_league( row[0] ),
                           season: Season.parse(row[1]),
                           teams: row[2],
                           matches: row[3],
                           goals: row[4],
                           start_date: row[5] ? Date.strptime( row[5], '%Y-%m-%d' ) : nil,
                           end_date: row[6]   ? Date.strptime( row[6], '%Y-%m-%d' ) : nil,
                         )
end

.find_by(league:, season:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sportdb/catalogs/event_info.rb', line 47

def self.find_by( league:, season: )
  league_key = _league( league ).key
  season_key = season.is_a?( String ) ? Season.parse(season).key
                                      : season.key

         rows =  execute( <<-SQL )
    SELECT #{self.columns.join(', ')}
    FROM event_infos
    WHERE event_infos.league_key = '#{league_key}' AND
          event_infos.season     = '#{season_key}'
SQL

  if rows.empty?
       nil
  elsif rows.size > 1  ## not possible in theory
   puts "** !!! ERROR - expected zero or one rows; got (#{rows.size}):"
   pp rows
   exit 1
  else
     _build_event_info( rows[0] )
  end
end

.seasons(league) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sportdb/catalogs/event_info.rb', line 34

def self.seasons( league )
  league_key = _league( league ).key

         rows =  execute( <<-SQL )
    SELECT #{self.columns.join(', ')}
    FROM event_infos
    WHERE event_infos.league_key = '#{league_key}'
SQL

     rows.map {|row| _build_event_info( row ) }
end