Class: SportDb::QuickMatchReader

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

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

def initialize( txt )
  @errors = []
  @outline = QuickLeagueOutline.parse( txt )
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



30
31
32
# File 'lib/sportdb/quick/quick_match_reader.rb', line 30

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)


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

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?



38
39
40
41
42
43
# File 'lib/sportdb/quick/quick_match_reader.rb', line 38

def league_name
   league = @data.keys[0]
   season = @data[ league ].keys[0]

   "#{league} #{season}"
end

#matchesObject



45
46
47
48
49
# File 'lib/sportdb/quick/quick_match_reader.rb', line 45

def matches
  league = @data.keys[0]
  season = @data[ league ].keys[0]
  @data[league][season]
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
# File 'lib/sportdb/quick/quick_match_reader.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

  @outline.each_sec do |sec|   ## sec(tion)s
    ### move season parse into outline upstream - why? why not?
    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

   # if debug?
   #   puts "  (sec) lines:"
   #   pp lines
   # end

    ### note - skip section if no lines !!!!!
    next if lines.empty?     ## or use lines.size == 0


    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

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