Class: SportDb::QuickMatchReader

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

Constant Summary collapse

SEASON_IN_HEADING_RE =

try to find season in heading

e.g. Ă–sterr. Bundesliga 2015/16  or 2015-16
     World Cup 2018 or  2018 World Cup etc.
%r{
                \b
              (?<season>\d{4}
                 (?:[\/-]\d{1,4})?     ## optional 2nd year in season
                 )\b
}x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ QuickMatchReader

Returns a new instance of QuickMatchReader.



25
26
27
28
29
30
31
# File 'lib/sportdb/quick/quick_match_reader.rb', line 25

def initialize( txt )
  @errors = []
  @txt    = txt

  @league_name = ''     
  @matches     = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



33
34
35
# File 'lib/sportdb/quick/quick_match_reader.rb', line 33

def errors
  @errors
end

Class Method Details

.debug=(value) ⇒ Object



7
# File 'lib/sportdb/quick/quick_match_reader.rb', line 7

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

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


8
# File 'lib/sportdb/quick/quick_match_reader.rb', line 8

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

.parse(txt) ⇒ Object



18
19
20
# File 'lib/sportdb/quick/quick_match_reader.rb', line 18

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

.read(path) ⇒ Object

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



13
14
15
16
# File 'lib/sportdb/quick/quick_match_reader.rb', line 13

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

#debug?Boolean

Returns:

  • (Boolean)


9
# File 'lib/sportdb/quick/quick_match_reader.rb', line 9

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

#errors?Boolean

Returns:

  • (Boolean)


34
# File 'lib/sportdb/quick/quick_match_reader.rb', line 34

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

#league_nameObject

helpers get matches & more after parse; all stored in data

change/rename to event - why? why not?

note - may or may not include season


42
# File 'lib/sportdb/quick/quick_match_reader.rb', line 42

def league_name() @league_name; end

#matchesObject



43
# File 'lib/sportdb/quick/quick_match_reader.rb', line 43

def matches() @matches; end

#parseObject



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
# File 'lib/sportdb/quick/quick_match_reader.rb', line 57

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

    ## note - source file MUST always start with heading 1 for now
    tree   = []
    parser = RaccMatchParser.new( @txt, debug: debug? )   ## use own parser instance (not shared) - why? why not?
    tree = parser.parse

    
##
## !! (QUICK) PARSE ERROR - source MUST start with Heading1; got 34 nodes:
## [<BlankLine>,
##  <BlankLine>,
## <Heading1 World Cup 1930>,

      ## remove leading BlankLines if any!!
      while tree[0].is_a? RaccMatchParser::BlankLine
          tree.shift  ## remove (leading) blank line from parse tree 
      end


    if tree[0].is_a? RaccMatchParser::Heading1
         ## try to get league and season
         @league_name = tree[0].text
    else  
        ### report error - source MUST start with heading 1
       puts "!! (QUICK) PARSE ERROR - source MUST start with Heading1; got #{tree.size} nodes:"
       pp tree
       exit
    end

      ## todo/fix
      ##  make season optional
      m = SEASON_IN_HEADING_RE.match( @league_name )
      if m.nil?
        puts "!! (QUICK) PARSE ERROR - no season found in Heading1 >#{@league_name}; sorry"
        exit 
      end
      season = Season.parse( m[:season] )   ## convert (str) to season obj!!!
      start =  if season.year?
                  Date.new( season.start_year, 1, 1 )
                else
                  Date.new( season.start_year, 7, 1 )
                end


    ############
    ### "walk" tree to get structs (matches/teams/etc.)
    conv = MatchTree.new( tree, start: start )
    
    auto_conf_teams, matches, rounds, groups = conv.convert


      ## 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


      @matches = matches
      ## note - skip teams, rounds, and groups for now
      @matches
end