Class: SportDb::Leagueset
- Inherits:
-
Object
- Object
- SportDb::Leagueset
- Defined in:
- lib/leagues/leagueset.rb
Class Method Summary collapse
Instance Method Summary collapse
- #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) ⇒ Leagueset
constructor
A new instance of Leagueset.
- #pretty_print(printer) ⇒ Object
- #size ⇒ Object
-
#validate!(source_path: ['.']) ⇒ Object
use a function for (re)use note - may add seasons in place!! (if seasons is empty).
Constructor Details
#initialize(recs) ⇒ Leagueset
Returns a new instance of Leagueset.
79 80 81 |
# File 'lib/leagues/leagueset.rb', line 79 def initialize( recs ) @recs = recs end |
Class Method Details
.parse(txt) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/leagues/leagueset.rb', line 43 def self.parse( txt ) ### 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) end |
.parse_args(args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/leagues/leagueset.rb', line 22 def self.parse_args( args ) ### split args in datasets with leagues and seasons datasets = [] args.each do |arg| if arg =~ %r{^[0-9/-]+$} ## season if datasets.empty? puts "!! ERROR - 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) end |
.read(path) ⇒ Object
75 |
# File 'lib/leagues/leagueset.rb', line 75 def self.read( path ) parse( read_text( path )); end |
Instance Method Details
#each(&blk) ⇒ Object
85 86 87 88 89 |
# File 'lib/leagues/leagueset.rb', line 85 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)???
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/leagues/leagueset.rb', line 152 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
177 178 179 180 181 |
# File 'lib/leagues/leagueset.rb', line 177 def pretty_print( printer ) printer.text( "<Leagueset: " ) printer.text( @recs ) printer.text( ">") end |
#size ⇒ Object
83 |
# File 'lib/leagues/leagueset.rb', line 83 def size() @recs.size; end |
#validate!(source_path: ['.']) ⇒ Object
use a function for (re)use
note - may add seasons in place!! (if seasons is empty)
todo/check - change source_path to (simply) path - why? why not?
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/leagues/leagueset.rb', line 96 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 if seasons.empty? ## simple heuristic to find current season [ Season( '2024/25'), Season( '2025') ].each do |season| league_info = LeagueCodes.find_by( code: league_key, season: season ) filename = "#{season.to_path}/#{league_info['code']}.csv" path = find_file( filename, path: source_path ) if path seasons << season break end end if seasons.empty? puts "!! ERROR - (leagueset) no latest auto-season via source found for #{league_key}; sorry" exit 1 end 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 |