Class: SportDb::QuickMatchLinter

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/formats/quick_match_linter.rb

Constant Summary collapse

CLUB_NAME_RE =
%r{^
  (?<name>[^()]+?)     ## non-greedy
  (?:
     \s+
     \(
       (?<code>[A-Z][A-Za-z]{2,3})    ## optional (country) code; support single code e.g. (A) - why? why not?
     \)
  )?
$}x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt, check_teams: true) ⇒ QuickMatchLinter

Returns a new instance of QuickMatchLinter.



26
27
28
29
30
31
# File 'lib/sportdb/formats/quick_match_linter.rb', line 26

def initialize( txt,
                check_teams: true )
  @errors = []
  @txt = txt
  @check_teams = check_teams
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



37
38
39
# File 'lib/sportdb/formats/quick_match_linter.rb', line 37

def errors
  @errors
end

Class Method Details

.debug=(value) ⇒ Object



8
# File 'lib/sportdb/formats/quick_match_linter.rb', line 8

def self.debug=(value) @@debug = value; end

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


9
# File 'lib/sportdb/formats/quick_match_linter.rb', line 9

def self.debug?() @@debug ||= false; end

.parse(txt) ⇒ Object



19
20
21
# File 'lib/sportdb/formats/quick_match_linter.rb', line 19

def self.parse( txt )
  new( txt ).parse
end

.read(path) ⇒ Object

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



14
15
16
17
# File 'lib/sportdb/formats/quick_match_linter.rb', line 14

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

Instance Method Details

#check_teams?Boolean

Returns:

  • (Boolean)


33
# File 'lib/sportdb/formats/quick_match_linter.rb', line 33

def check_teams?() @check_teams; end

#debug?Boolean

Returns:

  • (Boolean)


10
# File 'lib/sportdb/formats/quick_match_linter.rb', line 10

def debug?()  self.class.debug?; end

#errors?Boolean

Returns:

  • (Boolean)


38
# File 'lib/sportdb/formats/quick_match_linter.rb', line 38

def errors?() @errors.size > 0; end

#parseObject



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sportdb/formats/quick_match_linter.rb', line 53

def parse
  ## note: every (new) read call - resets errors list to empty
  @errors = []

  data = {}   # return data hash with leagues
              #    and seasons
              #   for now merge stage into matches

  secs = QuickLeagueOutlineReader.parse( @txt )
  pp secs    if debug?

  secs.each do |sec|   ## sec(tion)s
    season = Season.parse( sec[:season] )   ## convert (str) to season obj!!!
    league = sec[:league]
    stage  = sec[:stage]
    lines  = sec[:lines]

    start =  if season.year?
                Date.new( season.start_year, 1, 1 )
              else
                Date.new( season.start_year, 7, 1 )
              end

     league_rec = nil


 ##   if all upcase or digits - assume/try code first
 ##    todo/fix - change to find_by( code:  ) !!!!
     period = Import::LeaguePeriod.find_by( code: league, season: season )
     if period
        ## find league by qname (assumed to be unique!!)
        ##    todo/fix - use League.find_by!( name: ) !!!!
        ##      make more specifi
        league_rec = Import::League.find!( period.qname )
        puts "        #{period.pretty_inspect}"
        puts "     OK #{league}+#{season} => #{league_rec.pretty_inspect}"
     else
        ## try "generic" match by name (& more alt codes)
        ###
        ##      db check - check league
        ##       fix/fix - add season to match_by   !!!!!!!
        ##        note - changed to match (incl. code) from match_by(name:) only!!!
      recs =  Import::League.match( league )
      if recs.size == 1
        league_rec = recs[0]
        ## note - use pp (pretty_inspect) instead of league_rec.name
        puts "     OK #{league} => #{league_rec.pretty_inspect}"
      elsif recs.size == 0
        msg = "NAME ERROR - no league match found for >#{league}<"
        @errors << [msg]
        puts "!! #{msg}"
     else
        msg = "NAME ERROR - ambigous; too many league matches (#{recs.size}) found for >#{league}<"
        @errors << [msg]
        puts "!! #{msg}"
     end
    end



    parser = MatchParser.new( lines,
                              start )   ## note: keep season start_at date for now (no need for more specific stage date need for now)

    auto_conf_teams,  matches, rounds, groups = parser.parse

    ## auto-add "upstream" errors from parser
    @errors += parser.errors  if parser.errors?


    if debug?
      puts ">>> #{auto_conf_teams.size} teams:"
      pp auto_conf_teams
      puts ">>> #{matches.size} matches:"
      ## pp matches
      puts ">>> #{rounds.size} rounds:"
      pp rounds
      puts ">>> #{groups.size} groups:"
      pp groups
    end

###
##      db check - check teams
##     only clubs for now
##       fix add better support for champs etc
##               and national teams!!!
if check_teams?
  auto_conf_teams.each do |team|
    recs =  if league_rec && !league_rec.intl?
               Import::Club.match_by( name: team, league: league_rec )
            else
               ###
               ##  get country code from name
               ##    e.g. Liverpool FC (ENG) or
               ##         Liverpool FC (URU) etc.

               ## check for country code
               if m=CLUB_NAME_RE.match( team )
                 if m[:code]
                   Import::Club.match_by( name:    m[:name],
                                          country: m[:code] )
                 else
                    msg = "PARSE ERROR - country code missing for club name in int'l tournament; may not be unique >#{team}<"
                    @errors << [msg]
                    Import::Club.match_by( name: team )
                  end
               else
                 msg = "PARSE ERROR - invalid club name; cannot match with CLUB_NAME_RE >#{team}<"
                 @errors << [msg]
                 []
               end
            end
      team_rec = nil
      if recs.size == 1
        team_rec = recs[0]
        puts "     OK #{team} => #{team_rec.pretty_inspect}"
      elsif recs.size == 0
        msg = "NAME ERROR - no team match found for >#{team}<"
        @errors << [msg]
        puts "!! #{msg}"
      else
        msg = "NAME ERROR - ambigous; too many team matches (#{recs.size}) found for >#{team}<"
        @errors << [msg]
        puts "!! #{msg}"
      end
  end
end


    ## note: pass along stage (if present): stage  - optional from heading!!!!
    if stage
      matches.each do |match|
        match = match.update( stage: stage )
      end
    end

    data[ league ] ||= {}
    data[ league ][ season.key ] ||= []

    data[ league ][ season.key ] += matches
    ## note - skip teams, rounds, and groups for now
  end

## check - only one league and one season
##             allowed in quick style


  leagues = data.keys
  if leagues.size != 1
      puts "!! (QUICK) PARSE ERROR - expected one league only; got #{leagues.size}:"
      pp leagues
      exit 1
  end

  seasons = data[ leagues[0] ].keys
  if seasons.size != 1
      puts "!! (QUICK) PARSE ERROR - expected one #{leagues[0]} season only; got #{seasons.size}:"
      pp seasons
      exit 1
  end

  data[ leagues[0] ][ seasons[0] ]
end