class SportDb::ConfParser

Constants

COUNTRY_RE
TABLE_RE

standings table row regex matcher e.g.

1  Manchester City         38  32  4  2 106-27 100

or 1. Manchester City 38 32 4 2 106:27 100

Public Class Methods

new( lines ) click to toggle source
# File lib/sportdb/formats/match/conf_parser.rb, line 14
def initialize( lines )
  # for convenience split string into lines
  ##    note: removes/strips empty lines
  ## todo/check: change to text instead of array of lines - why? why not?
  @lines        = lines.is_a?( String ) ? read_lines( lines ) : lines
end
parse( lines ) click to toggle source
# File lib/sportdb/formats/match/conf_parser.rb, line 5
def self.parse( lines )
  parser = new( lines )
  parser.parse
end

Public Instance Methods

parse() click to toggle source
# File lib/sportdb/formats/match/conf_parser.rb, line 66
def parse
  teams = {}    ## convert lines to teams

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


    ## quick hack - check for/extract (optional) county code (for teams) first
    ##  allow as separators <>‹›,  NOTE: includes (,) comma for now too
    m = nil
    country = nil
    if m=COUNTRY_RE.match( line )
      country = m[:country]
      line = line.sub( m[0], '' )  ## replace match with nothing for now
    end

    if m=TABLE_RE.match( line )
      puts "  matching table entry >#{line}<"

      name = m[:team]
      rank = m[:rank] ? Integer(m[:rank]) : nil

      standing = {
        pld: Integer(m[:pld]),
        w:   Integer(m[:w]),
        d:   Integer(m[:d]),
        l:   Integer(m[:l]),
        gf:  Integer(m[:gf]),
        ga:  Integer(m[:ga]),
      }
      standing[ :gd ]        = Integer(m[:gd].gsub(/[±+]/,''))    if m[:gd]
      standing[ :pts ]       = Integer(m[:pts])
      standing[ :deduction ] = Integer(m[:deduction])  if m[:deduction]


      ## todo/fix: track double usage - why? why not? report/raise error/exception on duplicates?
      team = teams[ name ] ||= { }
      team[ :country ]   = country     if country

      team[ :rank ]      = rank        if rank
      team[ :standing ]  = standing    if standing
    else
      ## assume team is full line
      name = line.strip  # note: strip leading and trailing spaces

      team = teams[ name ] ||= { }
      team[ :country ]  = country     if country
    end
  end

  teams
end