Class: SportDb::Import::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/config/config.rb

Constant Summary collapse

CLUBS_REGEX1 =
%r{  (?:^|/)            # beginning (^) or beginning of path (/)
    (?<country>[a-z]{1,3})\.clubs\.txt$
}x
CLUBS_REGEX2 =
%r{  (?:^|/)            # beginning (^) or beginning of path (/)
    (?<country>[a-z]{2,3})-[^/]+?
      /clubs\.txt$
}x
CLUBS_REGEX =
Regexp.union( CLUBS_REGEX1, CLUBS_REGEX2 )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



9
10
11
# File 'lib/sportdb/config/config.rb', line 9

def initialize
  @errors = []     ## make parsing errors "global" for now
end

Instance Attribute Details

#clubs_dirObject

todo/fix: find a better way to configure club / team datasets



32
33
34
# File 'lib/sportdb/config/config.rb', line 32

def clubs_dir
  @clubs_dir
end

#errorsObject

Returns the value of attribute errors.



33
34
35
# File 'lib/sportdb/config/config.rb', line 33

def errors
  @errors
end

Instance Method Details

#find_clubs_datafiles(path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sportdb/config/config.rb', line 51

def find_clubs_datafiles( path )
   datafiles = []   ## note: [country, path] pairs for now

   ## check all txt files as candidates  (MUST include country code for now)
   candidates = Dir.glob( "#{path}/**/*.txt" )
   pp candidates
   candidates.each do |candidate|
     m = CLUBS_REGEX.match( candidate )
     if m
       datafiles << [m[:country], candidate]
     end
   end

   pp datafiles
   datafiles
end

#leaguesObject



24
25
26
27
# File 'lib/sportdb/config/config.rb', line 24

def leagues
  read_leagues()      if @leagues.nil?
  @leagues
end

#read_leaguesObject



178
179
180
181
182
183
184
# File 'lib/sportdb/config/config.rb', line 178

def read_leagues
  #####
  # add / read-in leagues config
  @leagues = LeagueConfig.new

  self  ## return self for chaining
end

#read_teamsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sportdb/config/config.rb', line 120

def read_teams
    ## unify team names; team (builtin/known/shared) name mappings
    ## cleanup team names - use local ("native") name with umlaut etc.
    recs = []
    @errors = []    ## reset errors

    ## todo/fix: pass along / use country code too
    datafiles = find_clubs_datafiles( clubs_dir )
    datafiles.each do |datafile|
       country  = datafile[0]    ## country code e.g. eng, at, de, etc.
       path     = datafile[1]
       recs += TeamReader.read( path )
    end

    ############################
    ## add team mappings
    ##   alt names to canonical pretty (recommended unique) name
    @team_mappings = {}

    recs.each do |rec|
       rec.alt_names.each do |alt_name|
         name = @team_mappings[ alt_name ]
         if name  ## todo/fix: add better warn about duplicates (if key exits) ???????
            msg = "** !!! WARN !!! - alt name conflict/duplicate - >#{alt_name}< will overwrite >#{name}< with >#{rec.name}<"
            puts msg
            @errors << msg
         else
           @team_mappings[ alt_name ] = rec.name
         end
       end
    end

###
## reverse hash for lookup/list of "official / registered(?)"
##    pretty recommended canonical unique (long form)
##    team names


##
##  todo/fix: move to new TeamConfig class (for reuse) !!!!!!
##   todo/fix:  add check for duplicates/conflicts too!!!
    @teams = {}
    recs.each do |rec|
      @teams[ rec.name ] = rec
    end

    if @errors.size > 0
      puts ""
      puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
      puts " #{@errors.size} errors:"
      pp @errors
      ## exit 1
    end

    self  ## return self for chaining
end

#team_mappingsObject



14
15
16
17
# File 'lib/sportdb/config/config.rb', line 14

def team_mappings
  read_teams()        if @team_mappings.nil?
  @team_mappings
end

#teamsObject



19
20
21
22
# File 'lib/sportdb/config/config.rb', line 19

def teams
  read_teams()        if @teams.nil?
  @teams
end