Class: SportDb::ConfReaderV2

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

Overview

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

Class Method Summary collapse

Class Method Details

.configObject

shortcut convenience helper



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

def self.config() Import.config; end

.parse(txt, season: nil) ⇒ Object



16
17
18
19
20
21
22
23
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
# File 'lib/sportdb/readers/conf_reader.rb', line 16

def self.parse( txt, season: nil )
  recs = LeagueOutlineReader.parse( txt, season: season )
  pp recs

  ## pass 2 - check & map; replace inline (string with record)
  recs.each do |rec|
    league = rec[:league]
    clubs = []    ## convert lines to clubs
    rec[:lines].each do |line|

      next if line =~ /^[ -]+$/   ## skip decorative lines with dash only (e.g. ---- or - - - -) etc.

      scan = StringScanner.new( line )

      if scan.check( /\d{1,2}[ ]+/ )    ## entry with standaning starts with ranking e.g. 1,2,3, etc.
        puts "  table entry >#{line}<"
        rank = scan.scan( /\d{1,2}[ ]+/ ).strip   # note: strip trailing spaces

        ## note: uses look ahead scan until we hit at least two spaces
        ##  or the end of string  (standing records for now optional)
        name = scan.scan_until( /(?=\s{2})|$/ )
        if scan.eos?
          standing = nil
        else
          standing = scan.rest.strip   # note: strip leading and trailing spaces
        end
        puts "   rank: >#{rank}<, name: >#{name}<, standing: >#{standing}<"

        ## note: rank and standing gets ignored (not used) for now
      else
        ## assume club is full line
        name = line
      end

      clubs << config.clubs.find_by( name: name, country: league.country )
    end

    rec[:clubs] = clubs
    rec.delete( :lines )   ## remove lines entry
  end

  ## pass 3 - import (insert/update) into db
  recs.each do |rec|
     league = Sync::League.find_or_create( rec[:league] )
     season = Sync::Season.find_or_create( rec[:season] )


     event  = Sync::Event.find_or_create( league: league, season: season )
     if rec[:stage]
       stage = Sync::Stage.find_or_create( rec[:stage], event: event )
     else
       stage = nil
     end


     rec[:clubs].each do |club_rec|
       club = Sync::Club.find_or_create( club_rec )
       ## add teams to event
       ##   todo/fix: check if team is alreay included?
       ##    or clear/destroy_all first!!!
       if stage
         stage.teams << club
       else
         event.teams << club
       end
     end
  end

  recs
end

.read(path, season: nil) ⇒ Object

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



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

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' ).read
  parse( txt, season: season )
end