class SportDb::Import::ClubHistoryIndex

Attributes

errors[R]

Public Class Methods

build( path ) click to toggle source
# File lib/sportdb/formats/team/club_index_history.rb, line 9
def self.build( path )
  pack = Package.new( path )   ## lets us use direcotry or zip archive

  recs = []
  pack.each_clubs_history do |entry|
    recs += ClubHistoryReader.parse( entry.read )
  end
  recs

  index = new
  index.add( recs )
  index
end
new() click to toggle source

note: keep name history for now separate from

   from club struct - why? why not?
later yes, yes, yes, merge name history into club struct!!!!!

for now the name history is experimental

# File lib/sportdb/formats/team/club_index_history.rb, line 34
def initialize
  @clubs          = {}   ## clubs (indexed) by canonical name
  @errors         = []
end

Public Instance Methods

add( rec_or_recs ) click to toggle source
# File lib/sportdb/formats/team/club_index_history.rb, line 57
def add( rec_or_recs )   ## add club record / alt_names
  recs = rec_or_recs.is_a?( Array ) ? rec_or_recs : [rec_or_recs]      ## wrap (single) rec in array

  recs.each do |rec|

    keyword    = rec[0]
    season_key = rec[1]
    args       = rec[2..-1]   ## get rest of args e.g. one, two or more

    ## note: for now only add (re)name history season records,
    ##          that is, skip MERGE and BANKRUPT for now
    ##           and incl. only RENAME, REFORM, MOVE for now
    next if ['MERGE', 'BANKRUPT'].include?( keyword )


    name_old = strip_geo( args[0][0] )  ## note: strip optional geo part from name
    name_new = strip_geo( args[1][0] )

    country_old = args[0][1]
    country_new = args[1][1]

    club_old = catalog.clubs.find_by!( name: name_old, country: country_old )
    club_new = catalog.clubs.find_by!( name: name_new, country: country_new )

    ## note use season obj for now (and NOT key) - why? why not?
    season = Season.parse( season_key )

    ## todo/check:
    ##   check if  club_old and club_new reference different club record!!
    ##    examples - RB II            -> Liefering ??  or
    ##               FC Pasching      -> OOE Juniors ??
    ##               Austria Salzburg -> RB Salburg ??
    ##   for now always add name history to both - why? why not?

    add_history( club_old, keyword, season, args )
    ## note: allow for now different club references
    ##    but maybe warn later - why? why not?
    ## add history to both for now
    add_history( club_new, keyword, season, args )  if club_old != club_new
  end # each rec
end
add_history( club_rec, keyword, season, args ) click to toggle source
# File lib/sportdb/formats/team/club_index_history.rb, line 45
def add_history( club_rec, keyword, season, args )
  ## note use season obj for now (and NOT key) - why? why not?
  rec = @clubs[ club_rec.name ] ||= []

  rec << [season, [keyword, args]]

  ## note: always keep records sorted by season_key for now
  ##   check if 2010 and 2010/11 is in order using alpha sort?? (see argentina)
  rec.sort! { |l,r| r[0] <=> l[0] }
end
catalog() click to toggle source
# File lib/sportdb/formats/team/club_index_history.rb, line 25
def catalog() Import.catalog; end
errors?() click to toggle source
# File lib/sportdb/formats/team/club_index_history.rb, line 40
def errors?() @errors.empty? == false; end
find_name_by( name:, season: ) click to toggle source
  todo/check: move as method to club struct later - to always use club reference
 returns (simply) name as string for now or nil - why? why not?

history entry example

Arsenal FC“=> [[1927/28, [”RENAME“, [[”The Arsenal FC, London“, ”eng“], [”Arsenal FC“, ”eng“]]]],

[1914/15, ["RENAME", [["Woolwich Arsenal FC, London", "eng"], ["The Arsenal FC", "eng"]]]],
[1892/93, ["RENAME", [["Royal Arsenal FC, London", "eng"], ["Woolwich Arsenal FC", "eng"]]]]],
# File lib/sportdb/formats/team/club_index_history.rb, line 108
def find_name_by( name:, season: )
  recs = @clubs[ name ]
  if recs
    season = Season( season )   ## make sure season is a season obj (and NOT a string)
    ## check season records for name; use linear search (assume only few records)
    recs.each do |rec|
      if season >= rec[0]
         return strip_geo( rec[1][1][1][0] )  # use second arg
      end
    end
    ## if we get here use last name
    strip_geo( recs[-1][1][1][0][0] )   # use first arg
  else
    nil
  end
end
mappings() click to toggle source
# File lib/sportdb/formats/team/club_index_history.rb, line 42
def mappings() @clubs; end
strip_geo( name ) click to toggle source

helpers

# File lib/sportdb/formats/team/club_index_history.rb, line 127
def strip_geo( name )
  ## e.g. Arsenal, London   =>   Arsenal
  name.split(',')[0].strip
end