Class: SportDb::ConfReader

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/sportdb/readers/conf_reader.rb

Overview

todo/check: rename to EventsReaderV2 (use plural?) why? why not?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ ConfReader

Returns a new instance of ConfReader.



20
21
22
# File 'lib/sportdb/readers/conf_reader.rb', line 20

def initialize( txt )
  @txt = txt
end

Class Method Details

.parse(txt, season: nil) ⇒ Object



13
14
15
# File 'lib/sportdb/readers/conf_reader.rb', line 13

def self.parse( txt, season: nil )
  new( txt ).parse( season: season )
end

.read(path, season: nil) ⇒ Object

use - rename to read_file or from_file etc. - why? why not?



8
9
10
11
# File 'lib/sportdb/readers/conf_reader.rb', line 8

def self.read( path, season: nil )   ## use - rename to read_file or from_file etc. - why? why not?
  txt = File.open( path, 'r:utf-8' ) {|f| f.read }
  parse( txt, season: season )
end

Instance Method Details

#catalogObject

method read



97
# File 'lib/sportdb/readers/conf_reader.rb', line 97

def catalog() Import.catalog; end

#parse(season: nil) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
# File 'lib/sportdb/readers/conf_reader.rb', line 24

def parse( season: nil )
  secs = LeagueOutlineReader.parse( @txt, season: season )
  pp secs

  ## pass 1 - check & map; replace inline (string with record)
  secs.each do |sec|    # sec(tion)s

    conf = ConfParser.parse( sec[:lines] )

    league = sec[:league]
    teams = []    ## convert lines to teams

    if league.clubs?
      if league.intl?
        conf.each do |name, rec|
          country_key = rec[:country]
          teams << catalog.clubs.find_by!( name: name,
                                          country: country_key )
        end
      else
        conf.each do |name, _|
          ## note: rank and standing gets ignored (not used) for now
          teams << catalog.clubs.find_by!( name: name,
                                          country: league.country )
        end
      end
    else   ### assume national teams
      conf.each do |name, _|
        ## note: rank and standing gets ignored (not used) for now
        teams << catalog.national_teams.find!( name )
      end
    end


    sec[:teams] = teams

    sec.delete( :lines )   ## remove lines entry
  end


  ## pass 2 - import (insert/update) into db
  secs.each do |sec|   # sec(tion)s
     ## todo/fix: always return Season struct record in LeagueReader - why? why not?
     event_rec  = Sync::Event.find_or_create_by( league: sec[:league],
                                                 season: sec[:season] )

     stage_rec  = if sec[:stage]
                    Sync::Stage.find_or_create( sec[:stage], event: event_rec )
                  else
                    nil
                  end

     ## todo/fix: check if all teams are unique
     ##   check if uniq works for club/national_team record (struct) - yes,no ??
     teams = sec[:teams]
     teams = teams.uniq

     ## add to database
     team_recs     =  stage_rec ? stage_rec.teams    : event_rec.teams
     team_ids      =  stage_rec ? stage_rec.team_ids : event_rec.team_ids

     new_team_recs = Sync::Team.find_or_create( teams )
     new_team_recs.each do |team_rec|
       ## add teams to event
       ##   for now check if team is alreay included
       ##   todo/fix: clear/destroy_all first - why? why not!!!
       team_recs << team_rec    unless team_ids.include?( team_rec.id )
     end
  end

  true   ## todo/fix: return true/false or something
end