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.
96 97 98 99 100 101 102 |
# File 'lib/leagues/leagueset.rb', line 96 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/leagues/leagueset.rb', line 74 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 seasons_str = rec['seasons'] seasons = Season.parse_line( seasons_str ) datasets << [key, seasons] 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
90 91 92 |
# File 'lib/leagues/leagueset.rb', line 90 def self.read( path, autofill: nil ) parse( read_text( path ), autofill: autofill ) end |
Instance Method Details
#_autofill(datasets, autofill:) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/leagues/leagueset.rb', line 130 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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/leagues/leagueset.rb', line 105 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
154 155 156 157 158 |
# File 'lib/leagues/leagueset.rb', line 154 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)???
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/leagues/leagueset.rb', line 212 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
237 238 239 240 241 |
# File 'lib/leagues/leagueset.rb', line 237 def pretty_print( printer ) printer.text( "<Leagueset: " ) printer.text( @recs ) printer.text( ">") end |
#size ⇒ Object
152 |
# File 'lib/leagues/leagueset.rb', line 152 def size() @recs.size; end |
#validate!(source_path: ['.']) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/leagues/leagueset.rb', line 175 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 |