Class: SportDb::Reader
- Inherits:
-
Object
- Object
- SportDb::Reader
- Includes:
- LogUtils::Logging, SportDB::FixtureHelpers, SportDB::Models
- Defined in:
- lib/sportdb/reader.rb
Instance Attribute Summary collapse
-
#include_path ⇒ Object
readonly
Returns the value of attribute include_path.
Instance Method Summary collapse
-
#initialize(include_path, opts = {}) ⇒ Reader
constructor
A new instance of Reader.
-
#load(ary) ⇒ Object
convenience helper for all-in-one reader.
- #load_event(name) ⇒ Object
-
#load_fixture_setup(name) ⇒ Object
fix/todo: rename ??.
-
#load_fixtures(event_key, name) ⇒ Object
load from file system.
-
#load_fixtures_from_string(event_key, text) ⇒ Object
load from string (e.g. passed in via web form).
- #load_leagues(name, more_attribs = {}) ⇒ Object
- #load_persons(name, more_attribs = {}) ⇒ Object
- #load_races(name) ⇒ Object
- #load_races_worker(reader) ⇒ Object
- #load_records(name) ⇒ Object
- #load_records_worker(reader) ⇒ Object
- #load_rosters(name) ⇒ Object
- #load_rosters_worker(reader) ⇒ Object
- #load_seasons(name) ⇒ Object
- #load_setup(setup) ⇒ Object
- #load_teams(name, more_attribs = {}) ⇒ Object
- #load_tracks(name, more_attribs = {}) ⇒ Object
Constructor Details
#initialize(include_path, opts = {}) ⇒ Reader
Returns a new instance of Reader.
16 17 18 |
# File 'lib/sportdb/reader.rb', line 16 def initialize( include_path, opts={}) @include_path = include_path end |
Instance Attribute Details
#include_path ⇒ Object (readonly)
Returns the value of attribute include_path.
14 15 16 |
# File 'lib/sportdb/reader.rb', line 14 def include_path @include_path end |
Instance Method Details
#load(ary) ⇒ Object
convenience helper for all-in-one reader
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 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 142 143 144 145 146 147 148 149 150 |
# File 'lib/sportdb/reader.rb', line 76 def load( ary ) # convenience helper for all-in-one reader logger.debug "enter load(include_path=>>#{include_path}<<):" logger.debug ary.to_json ary.each do |rec| if rec.kind_of?( String ) ## assume single fixture name name = rec if name =~ /^circuits/ # e.g. circuits.txt in formula1.db load_tracks( name ) elsif name =~ /^drivers/ # e.g. drivers.txt in formula1.db load_persons( name ) elsif name =~ /^teams/ # e.g. teams.txt in formula1.db load_teams( name ) elsif name =~ /\/races/ # e.g. 2013/races.txt in formula1.db load_races( name ) elsif name =~ /\/squads/ || name =~ /\/rosters/ # e.g. 2013/squads.txt in formula1.db load_rosters( name ) elsif name =~ /\/([0-9]{2})-/ load_records( name ) # e.g. 2013/04-gp-monaco.txt in formula1.db elsif name =~ /^seasons/ load_seasons( name ) elsif name =~ /^leagues/ if name =~ /club/ # e.g. leagues_club load_leagues( name, club: true ) else # e.g. leagues load_leagues( name ) end elsif name =~ /^([a-z]{2})\/leagues/ # auto-add country code (from folder structure) for country-specific leagues # e.g. at/leagues country_key = $1 country = Country.find_by_key!( country_key ) load_leagues( name, club: true, country_id: country.id ) elsif name =~ /^([a-z]{2})\/teams/ # auto-add country code (from folder structure) for country-specific teams # e.g. at/teams at/teams.2 de/teams etc. country_key = $1 country = Country.find_by_key!( country_key ) load_teams( name, club: true, country_id: country.id ) elsif name =~ /\/teams/ if name =~ /club/ # club teams (many countries) # e.g. club/europe/teams load_teams( name, club: true ) else # assume national teams # e.g. world/teams amercia/teams_n load_teams( name, national: true ) end else logger.error "unknown sportdb fixture type >#{name}<" # todo/fix: exit w/ error end else # more than one item in record? assume fixture starting w/ event key # assume first item is key # assume second item is event plus fixture # assume option third,etc are fixtures (e.g. bl2, etc.) event_key = rec[0] # e.g. at.2012/13 event_name = rec[1] # e.g. at/2012_13/bl fixture_names = rec[1..-1] # e.g. at/2012_13/bl, at/2012_13/bl2 load_event( event_name ) fixture_names.each do |fixture_name| load_fixtures( event_key, fixture_name ) end end end # each ary end |
#load_event(name) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/sportdb/reader.rb', line 238 def load_event( name ) #### ## fix!!!!! ## use Event.create_or_update_from_hash or similar ## use Event.create_or_update_from_hash_reader?? or similar # move parsing code to model reader = HashReaderV2.new( name, include_path ) event_attribs = {} reader.each_typed do |key, value| ## puts "processing event attrib >>#{key}<< >>#{value}<<..." if key == 'league' league = League.find_by_key( value.to_s.strip ) ## check if it exists if league.present? event_attribs['league_id'] = league.id else logger.error "league with key >>#{value.to_s.strip}<< missing" exit 1 end elsif key == 'season' season = Season.find_by_key( value.to_s.strip ) ## check if it exists if season.present? event_attribs['season_id'] = season.id else logger.error "season with key >>#{value.to_s.strip}<< missing" exit 1 end elsif key == 'start_at' if value.is_a?(DateTime) || value.is_a?(Date) start_at = value else # assume it's a string start_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' ) end event_attribs['start_at'] = start_at elsif key == 'teams' ## assume teams value is an array team_ids = [] value.each do |item| team_key = item.to_s.strip team = Team.find_by_key!( team_key ) team_ids << team.id end event_attribs['team_ids'] = team_ids elsif key == 'team3' ## for now always assume false # todo: fix - use value and convert to boolean if not boolean event_attribs['team3'] = false else ## todo: add a source location struct to_s or similar (file, line, col) logger.error "unknown event attrib; skipping attrib" end end # each key,value event = Event.find_by_league_id_and_season_id( event_attribs['league_id'], event_attribs['season_id']) ## check if it exists if event.present? logger.debug "*** update event #{event.id}-#{event.key}:" else logger.debug "*** create event:" event = Event.new end logger.debug event_attribs.to_json event.update_attributes!( event_attribs ) end |
#load_fixture_setup(name) ⇒ Object
fix/todo: rename ??
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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/sportdb/reader.rb', line 27 def load_fixture_setup( name ) ## todo/fix: cleanup quick and dirty code path = "#{include_path}/#{name}.yml" logger.info "parsing data '#{name}' (#{path})..." text = File.read_utf8( path ) hash = YAML.load( text ) ### build up array for fixtures from hash ary = [] hash.each do |key_wild, value_wild| key = key_wild.to_s.strip logger.debug "yaml key:#{key_wild.class.name} >>#{key}<<, value:#{value_wild.class.name} >>#{value_wild}<<" if value_wild.kind_of?( String ) # assume non-event data ary << value_wild elsif value_wild.kind_of?( Array ) # assume non_event data as array of strings ary = ary + value_wild elsif value_wild.kind_of?( Hash ) # assume event data value_wild.each do |event_key, event_values| # e.g. # at.2012/13: at/2012_13/bl, at/2012_13/bl2 # becomes # [ 'at.2012/13', 'at/2012_13/bl', 'at/2012_13/bl2' ] ary << ( [ event_key.to_s ] + event_values.split(',') ) end else logger.error "unknow fixture type in setup (yaml key:#{key_wild.class.name} >>#{key}<<, value:#{value_wild.class.name} >>#{value_wild}<<); skipping" end end logger.debug "fixture setup:" logger.debug ary.to_json ary end |
#load_fixtures(event_key, name) ⇒ Object
load from file system
337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/sportdb/reader.rb', line 337 def load_fixtures( event_key, name ) # load from file system path = "#{include_path}/#{name}.txt" logger.info "parsing data '#{name}' (#{path})..." SportDB.lang.lang = LangChecker.new.analyze( name, include_path ) reader = LineReader.new( path ) load_fixtures_worker( event_key, reader ) Prop.create_from_fixture!( name, path ) end |
#load_fixtures_from_string(event_key, text) ⇒ Object
load from string (e.g. passed in via web form)
326 327 328 329 330 331 332 333 334 335 |
# File 'lib/sportdb/reader.rb', line 326 def load_fixtures_from_string( event_key, text ) # load from string (e.g. passed in via web form) ## todo/fix: move code into LineReader e.g. use LineReader.fromString() - why? why not? reader = StringLineReader.new( text ) load_fixtures_worker( event_key, reader ) ## fix add prop ### Prop.create!( key: "db.#{fixture_name_to_prop_key(name)}.version", value: "file.txt.#{File.mtime(path).strftime('%Y.%m.%d')}" ) end |
#load_leagues(name, more_attribs = {}) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/sportdb/reader.rb', line 153 def load_leagues( name, more_attribs={} ) reader = ValuesReaderV2.new( name, include_path, more_attribs ) reader.each_line do |new_attributes, values| League.create_or_update_from_values( new_attributes, values ) end # each lines end |
#load_persons(name, more_attribs = {}) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/sportdb/reader.rb', line 176 def load_persons( name, more_attribs={} ) reader = ValuesReaderV2.new( name, include_path, more_attribs ) reader.each_line do |new_attributes, values| Person.create_or_update_from_values( new_attributes, values ) end # each lines end |
#load_races(name) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/sportdb/reader.rb', line 441 def load_races( name ) path = "#{include_path}/#{name}.txt" logger.info "parsing data '#{name}' (#{path})..." ### SportDB.lang.lang = LangChecker.new.analyze( name, include_path ) reader = LineReader.new( path ) ## for now: use all tracks (later filter/scope by event) @known_tracks = Track.known_tracks_table load_races_worker( reader ) Prop.create_from_fixture!( name, path ) end |
#load_races_worker(reader) ⇒ Object
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/sportdb/reader.rb', line 459 def load_races_worker( reader ) reader.each_line do |line| logger.debug " line: >#{line}<" ### fix: use new find_leading_pos! pos = find_game_pos!( line ) # alias -> rename to find_pos! or better use find_leading_pos!( line ) match_track!( line ) track_key = find_track!( line ) date = find_date!( line ) race_attribs = { pos: pos, track_key: track_key, # fix: use track_id start_at: date } pp race_attribs end # lines.each end |
#load_records(name) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/sportdb/reader.rb', line 354 def load_records( name ) path = "#{include_path}/#{name}.txt" logger.info "parsing data '#{name}' (#{path})..." ### SportDB.lang.lang = LangChecker.new.analyze( name, include_path ) reader = LineReader.new( path ) ## for now: use all tracks (later filter/scope by event) # @known_tracks = Track.known_tracks_table ## fix: add @known_teams - for now; use teams (not scoped by event) load_records_worker( reader ) Prop.create_from_fixture!( name, path ) end |
#load_records_worker(reader) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/sportdb/reader.rb', line 373 def load_records_worker( reader ) reader.each_line do |line| logger.debug " line: >#{line}<" ### fix: use new find_leading_pos! # pos = find_game_pos!( line ) # alias -> rename to find_pos! or better use find_leading_pos!( line ) # match_person!( line ) # match_teams!( line ) record_attribs = { # pos: pos, # team_key: team_key # fix: use team_id } pp record_attribs end # lines.each end |
#load_rosters(name) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/sportdb/reader.rb', line 397 def load_rosters( name ) path = "#{include_path}/#{name}.txt" logger.info "parsing data '#{name}' (#{path})..." ### SportDB.lang.lang = LangChecker.new.analyze( name, include_path ) reader = LineReader.new( path ) ## for now: use all tracks (later filter/scope by event) # @known_tracks = Track.known_tracks_table ## fix: add @known_teams - for now; use teams (not scoped by event) load_rosters_worker( reader ) Prop.create_from_fixture!( name, path ) end |
#load_rosters_worker(reader) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/sportdb/reader.rb', line 417 def load_rosters_worker( reader ) reader.each_line do |line| logger.debug " line: >#{line}<" ### fix: use new find_leading_pos! pos = find_game_pos!( line ) # alias -> rename to find_pos! or better use find_leading_pos!( line ) # match_person!( line ) # match_teams!( line ) roster_attribs = { pos: pos, # team_key: team_key # fix: use team_id } pp roster_attribs end # lines.each end |
#load_seasons(name) ⇒ Object
188 189 190 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/sportdb/reader.rb', line 188 def load_seasons( name ) reader = HashReaderV2.new( name, include_path ) #### ## fix!!!!! ## use Season.create_or_update_from_hash or similar ## use Season.create_or_update_from_hash_reader?? or similar # move parsing code to model reader.each_typed do |key, value| ## puts "processing event attrib >>#{key}<< >>#{value}<<..." if key == 'seasons' logger.debug "#{value.class.name}: >>#{value}<<" ## nb: assume value is an array value.each do |item| season_attribs = {} season = Season.find_by_key( item.to_s.strip ) ## check if it exists if season.present? logger.debug "update season #{season.id}-#{season.key}:" else logger.debug "create season:" season = Season.new season_attribs[ :key ] = item.to_s.strip end season_attribs[:title] = item.to_s.strip logger.debug season_attribs.to_json season.update_attributes!( season_attribs ) end else logger.error "unknown seasons key; skipping" end end # each key,value end |
#load_setup(setup) ⇒ Object
20 21 22 23 |
# File 'lib/sportdb/reader.rb', line 20 def load_setup( setup ) ary = load_fixture_setup( setup ) load( ary ) end |
#load_teams(name, more_attribs = {}) ⇒ Object
483 484 485 486 487 488 489 |
# File 'lib/sportdb/reader.rb', line 483 def load_teams( name, more_attribs={} ) reader = ValuesReaderV2.new( name, include_path, more_attribs ) reader.each_line do |new_attributes, values| Team.create_or_update_from_values( new_attributes, values ) end # each lines end |
#load_tracks(name, more_attribs = {}) ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'lib/sportdb/reader.rb', line 164 def load_tracks( name, more_attribs={} ) reader = ValuesReaderV2.new( name, include_path, more_attribs ) reader.each_line do |new_attributes, values| Track.create_or_update_from_values( new_attributes, values ) end # each lines end |