Class: SportDb::Leagueset
- Inherits:
-
Object
- Object
- SportDb::Leagueset
- Defined in:
- lib/leagues/leagueset.rb
Class Method Summary collapse
-
.autofiller(league_query, source_path: ['.']) ⇒ Object
autofiller helper - simple heuristic to find current (latest) season.
- .parse(txt, autofill: nil) ⇒ Object
-
.parse_args(args, autofill: nil) ⇒ Object
note - requires autofill (for seasons) if league querykey without season/empty season.
- .read(path, autofill: nil) ⇒ Object
Instance Method Summary collapse
- #_autofill(datasets, autofill:) ⇒ Object
- #_norm(recs) ⇒ Object
- #each(&blk) ⇒ Object
-
#filter(queries = []) ⇒ Object
todo/check: find a better name for helper? find_all_datasets, filter_datatsets - add alias(es??? queries (lik ARGV) e.g. [‘at’] or [‘eng’, ‘de’] etc. list of strings.
-
#initialize(recs, autofill: nil) ⇒ Leagueset
constructor
A new instance of Leagueset.
- #pretty_print(printer) ⇒ Object
- #size ⇒ Object
- #validate!(source_path: ['.']) ⇒ Object
Constructor Details
#initialize(recs, autofill: nil) ⇒ Leagueset
Returns a new instance of Leagueset.
112 113 114 115 116 117 118 |
# File 'lib/leagues/leagueset.rb', line 112 def initialize( recs, autofill: nil ) ### @org_recs = recs ## keep a record of orginal (MUST clone) - why? why not? ##### check for empty seasons recs = _autofill( recs, autofill: autofill ) @recs = _norm( recs ) end |
Class Method Details
.autofiller(league_query, source_path: ['.']) ⇒ Object
autofiller helper
- simple heuristic to find current (latest) season
maybe move autofiller to fbup or such - why? why not?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/leagues/leagueset.rb', line 27 def self.autofiller( league_query, source_path: ['.'] ) [ Season('2024/25'), Season('2025') ].each do |season| league_info = LeagueCodes.find_by( code: league_query, season: season ) league_code = league_info['code'] filename = "#{season.to_path}/#{league_code}.csv" path = find_file( filename, path: source_path ) if path return season end end nil ## return nil if not found end |
.parse(txt, autofill: nil) ⇒ Object
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 |
# File 'lib/leagues/leagueset.rb', line 73 def self.parse( txt, autofill: nil ) ### split args in datasets with leagues and seasons datasets = [] recs = parse_csv( txt ) recs.each do |rec| key = rec['league'].downcase datasets << [key, []] seasons_str = rec['seasons'] seasons = seasons_str.split( /[ ]+/ ) seasons.each do |season_str| ## note - add support for ranges e.g. 2001/02..2010/11 if season_str.index( '..' ) fst,snd = season_str.split( '..' ) # pp [fst,snd] fst = Season.parse( fst ) snd = Season.parse( snd ) if fst < snd && fst.year? == snd.year? datasets[-1][1] += (fst..snd).to_a else raise ArgumentError, "parse error - invalid season range >#{str}<, 1) two seasons required, 2) first < second, 3) same (year/academic) type" end else season = Season.parse( season_str ) ## check season datasets[-1][1] << season end end end new(datasets, autofill: autofill) end |
.parse_args(args, autofill: nil) ⇒ Object
note - requires autofill (for seasons)
if league querykey without season/empty season
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/leagues/leagueset.rb', line 49 def self.parse_args( args, autofill: nil ) ### split args in datasets with leagues and seasons ## e.g. at1 eng1 or ## at1 2024/25 br1 2025 etc. datasets = [] args.each do |arg| if arg =~ %r{^[0-9/-]+$} ## season if datasets.empty? puts "!! ERROR [leagueset.parse_args] - league required before season arg; sorry" exit 1 end season = Season.parse( arg ) ## check season datasets[-1][1] << season else ## assume league key key = arg.downcase datasets << [key, []] end end new(datasets, autofill: autofill) end |
.read(path, autofill: nil) ⇒ Object
106 107 108 |
# File 'lib/leagues/leagueset.rb', line 106 def self.read( path, autofill: nil ) parse( read_text( path ), autofill: autofill ) end |
Instance Method Details
#_autofill(datasets, autofill:) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/leagues/leagueset.rb', line 146 def _autofill( datasets, autofill: ) ##### check for empty seasons datasets.each do |league_query, seasons| ### try autofill if seasons.empty? && autofill.is_a?(Proc) season = autofill.call( league_query ) if season ## note - all season as string for autfiller too seasons << Season(season) end end if seasons.empty? puts "!! ERROR [leagueset] - empty seasons; autofill found no latest season for #{league_query}; sorry" exit 1 end end end |
#_norm(recs) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/leagues/leagueset.rb', line 121 def _norm( recs ) datasets = {} recs.each do |league_query, seasons| unless LeagueCodes.valid?( league_query ) puts "!! ERROR - (leagueset) no league (config) found for code >#{league_query}<; sorry" exit 1 end seasons.each do |season| ## check league code config too - why? why not? league_info = LeagueCodes.find_by( code: league_query, season: season ) if league_info.nil? puts "!! ERROR - (leagueset) no league config found for code #{league_query} AND season #{season}; sorry" exit 1 end rec = datasets[ league_info['code'] ] ||= [] rec << season end end # each record datasets.to_a ## convert hash to array end |
#each(&blk) ⇒ Object
170 171 172 173 174 |
# File 'lib/leagues/leagueset.rb', line 170 def each( &blk ) @recs.each do |league_key, seasons| blk.call( league_key, seasons ) end end |
#filter(queries = []) ⇒ Object
todo/check: find a better name for helper?
find_all_datasets, filter_datatsets - add alias(es???
queries (lik ARGV) e.g. ['at'] or ['eng', 'de'] etc. list of strings
todo/fix - check if used anywhere???
- check if works with new alt codes too (or needs update)???
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/leagues/leagueset.rb', line 228 def filter( queries=[] ) ## find all matching leagues (that is, league keys) if queries.empty? ## no filter - get all league keys self else recs = @recs.find_all do |league_key, seasons| found = false ## note: normalize league key ## (remove dot and downcase) norm_key = league_key.gsub( '.', '' ) queries.each do |query| q = query.gsub( '.', '' ).downcase if norm_key.start_with?( q ) found = true break end end found end ## return new typed leagueset self.class.new( recs ) end end |
#pretty_print(printer) ⇒ Object
253 254 255 256 257 |
# File 'lib/leagues/leagueset.rb', line 253 def pretty_print( printer ) printer.text( "<Leagueset: " ) printer.text( @recs ) printer.text( ">") end |
#size ⇒ Object
168 |
# File 'lib/leagues/leagueset.rb', line 168 def size() @recs.size; end |
#validate!(source_path: ['.']) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/leagues/leagueset.rb', line 191 def validate!( source_path: ['.'] ) each do |league_key, seasons| unless LeagueCodes.valid?( league_key ) puts "!! ERROR - (leagueset) no league (config) found for code >#{league_key}<; sorry" exit 1 end ## check source path too upfront - why? why not? seasons.each do |season| ## check league code config too - why? why not? league_info = LeagueCodes.find_by( code: league_key, season: season ) if league_info.nil? puts "!! ERROR - (leagueset) no league config found for code #{league_key} AND season #{season}; sorry" exit 1 end filename = "#{season.to_path}/#{league_info['code']}.csv" path = find_file( filename, path: source_path ) if path.nil? puts "!! ERROR - (leagueset) no source found for #{filename}; sorry" exit 1 end end end # each record end |