Class: SportDb::CsvEventImporter
- Inherits:
-
Object
- Object
- SportDb::CsvEventImporter
- Defined in:
- lib/sportdb/importers/event.rb
Class Method Summary collapse
- .parse(txt, league:, season:, headers: nil) ⇒ Object
- .read(path, league:, season:, headers: nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(txt, league:, season:, headers: nil) ⇒ CsvEventImporter
constructor
A new instance of CsvEventImporter.
- #parse ⇒ Object
Constructor Details
#initialize(txt, league:, season:, headers: nil) ⇒ CsvEventImporter
Returns a new instance of CsvEventImporter.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sportdb/importers/event.rb', line 22 def initialize( txt, league:, season:, headers: nil ) @txt = txt @headers = headers raise ArgumentError("string expected for league; got #{league.class.name}") unless league.is_a? String raise ArgumentError("string expected for season; got #{season.class.name}") unless season.is_a? String ## try mapping of league here - why? why not? @league = Import.catalog.leagues.find!( league ) @season = Season.parse( season ) end |
Class Method Details
.parse(txt, league:, season:, headers: nil) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/sportdb/importers/event.rb', line 14 def self.parse( txt, league:, season:, headers: nil ) new( txt, league: league, season: season, headers: headers ).parse end |
.read(path, league:, season:, headers: nil) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/sportdb/importers/event.rb', line 6 def self.read( path, league:, season:, headers: nil ) txt = File.open( path, 'r:utf-8' ) {|f| f.read } parse( txt, league: league, season: season, headers: headers ) end |
Instance Method Details
#parse ⇒ Object
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 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 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 |
# File 'lib/sportdb/importers/event.rb', line 35 def parse ## todo/fix: add headers options (pass throughto CsvMatchReader) ## add filters too why? why not? ## todo/fix: ## add normalize: false/mapping: false flag for NOT mapping club/team names ## make normalize: false the default, anyways - why? why not? opts = {} opts[:headers] = @headers if @headers matches = CsvMatchParser.parse( @txt, **opts ) matchlist = Import::Matchlist.new( matches ) team_names = matchlist.teams ## was: find_teams_in_matches_txt( matches_txt ) puts "#{team_names.size} teams:" pp team_names ## note: allows duplicates - will return uniq struct recs in teams teams = Import.catalog.teams.find_by!( name: team_names, league: @league ) ## build mapping - name => team struct record team_mappings = team_names.zip( teams ).to_h pp team_mappings ####### # start with database updates / sync here event_rec = Sync::Event.find_or_create_by( league: @league, season: @season ) ## todo/fix: ## add check if event has teams ## if yes - only double check and do NOT create / add teams ## number of teams must match (use teams only for lookup/alt name matches) ## note: allows duplicates - will return uniq db recs in teams ## and mappings from names to uniq db recs ## todo/fix: rename to team_recs_cache or team_cache - why? why not? # maps struct record "canonical" team name to active record db record!! ## note: use "canonical" team name as hash key for now (and NOT the object itself) - why? why not? team_recs = Sync::Team.find_or_create( team_mappings.values.uniq ) ## todo/fix/check: ## add check if event has teams ## if yes - only double check and do NOT create / add teams ## number of teams must match (use teams only for lookup/alt name matches) ## add teams to event ## todo/fix: check if team is alreay included? ## or clear/destroy_all first!!! event_rec.teams = team_recs ## todo/check/fix: use team_ids instead - why? why not? ## add catch-all/unclassified "dummy" round # round_rec = Model::Round.create!( # event_id: event_rec.id, # title: 'Matchday ??? / Missing / Catch-All', ## find a better name? # pos: 999, # start_at: event_rec.start_at.to_date # ) ## add matches matches.each do |match| team1_rec = Sync::Team.cache[ team_mappings[match.team1].name ] team2_rec = Sync::Team.cache[ team_mappings[match.team2].name ] if match.date.nil? puts "!!! WARN: skipping match - play date missing!!!!!" pp match else ## find last pos - check if it can be nil? yes, is nil if no records found max_pos = Model::Match.where( event_id: event_rec.id ).maximum( 'pos' ) max_pos = max_pos ? max_pos+1 : 1 rec = Model::Match.create!( event_id: event_rec.id, team1_id: team1_rec.id, team2_id: team2_rec.id, ## round_id: round_rec.id, -- note: now optional pos: max_pos, date: Date.strptime( match.date, '%Y-%m-%d' ), score1: match.score1, score2: match.score2, score1i: match.score1i, score2i: match.score2i, ) end end # each match event_rec # note: return event database record end |