Class: SportDb::QuickMatchReader
- Inherits:
-
Object
- Object
- SportDb::QuickMatchReader
- Includes:
- LogUtils::Logging
- Defined in:
- lib/sportdb/quick/quick_match_reader.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
- .debug=(value) ⇒ Object
-
.debug? ⇒ Boolean
note: default is FALSE.
- .parse(txt) ⇒ Object
-
.read(path) ⇒ Object
use - rename to read_file or from_file etc.
Instance Method Summary collapse
- #debug? ⇒ Boolean
- #errors? ⇒ Boolean
-
#initialize(txt) ⇒ QuickMatchReader
constructor
A new instance of QuickMatchReader.
- #parse ⇒ Object
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 = [] @txt = txt end |
Instance Attribute Details
#errors ⇒ Object (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
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
9 |
# File 'lib/sportdb/quick/quick_match_reader.rb', line 9 def debug?() self.class.debug?; end |
#errors? ⇒ Boolean
31 |
# File 'lib/sportdb/quick/quick_match_reader.rb', line 31 def errors?() @errors.size > 0; end |
#parse ⇒ Object
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sportdb/quick/quick_match_reader.rb', line 33 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 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 |