class SportDb::GoalsParser

Constants

MINUTES_REGEX

todo/check: change to MINUTE_REGEX ?? add MINUTE_SKIP_REGEX or MINUTE_SEP_REGEX /^[ ,]+/ todo/fix: split out penalty and owngoal flag in PATTERN constant for reuse

NAME_REGEX

note: use ^ for start of string only!!!

  • for now slurp everything up to digits (inlc. spaces - use strip to remove)

todo/check: use/rename to NAME_UNTIL_REGEX ??? ( add lookahead for spaces?)

Public Class Methods

new() click to toggle source
# File lib/sportdb/formats/goals.rb, line 202
def initialize
  # nothing here for now
end

Public Instance Methods

parse!( line, opts={} ) click to toggle source
# File lib/sportdb/formats/goals.rb, line 206
def parse!( line, opts={} )

  ## for now assume
  ##    everything up-to  0-9 and , and () is part of player name

  ## try parsing lhs
  ##  todo: check for  empty -    remove (make it same as empty string)

  players = []

  name = get_player_name!( line )
  while name
    logger.debug "  found player name >#{name}< - remaining >#{line}<"

    player = GoalsPlayerStruct.new
    player.name = name

    minute_hash = get_minute_hash!( line )
    while minute_hash
      logger.debug "  found minutes >#{minute_hash.inspect}< - remaining >#{line}<"

      minute = GoalsMinuteStruct.new
      minute.minute = minute_hash[:minute].to_i
      minute.offset = minute_hash[:offset].to_i  if minute_hash[:offset]
      if minute_hash[:type]
        minute.owngoal = true  if minute_hash[:type] =~ /o\.g\./
        minute.penalty = true  if minute_hash[:type] =~ /P|pen\./
      end
      player.minutes << minute

      # remove commas and spaces (note: use ^ for start of string only!!!)
      line.sub!( /^[ ,]+/, '' )
      minute_hash = get_minute_hash!( line )
    end

    players << player
    name = get_player_name!( line )
  end

  players
end

Private Instance Methods

get_minute_hash!( line ) click to toggle source
# File lib/sportdb/formats/goals.rb, line 260
def get_minute_hash!( line )
  m = MINUTES_REGEX.match( line ) # note: use ^ for start of string only!!!
  if m
    h = {}
    # - note: do NOT forget to turn name into symbol for lookup in new hash (name.to_sym)
    m.names.each { |n| h[n.to_sym] = m[n] } # or use match_data.names.zip( match_data.captures ) - more cryptic but "elegant"??

    ## remove matched string from line
    line.slice!( 0...m[0].length )
    h
  else
    nil
  end
end
get_player_name!( line ) click to toggle source
# File lib/sportdb/formats/goals.rb, line 249
def get_player_name!( line )
  m = NAME_REGEX.match( line )
  if m
    ## remove from line
    line.slice!( 0...m[0].length )
    m[0].strip    # remove leading and trailing spaces
  else
    nil
  end
end