Class: SportDb::Sync::Match
- Inherits:
-
Object
- Object
- SportDb::Sync::Match
- Defined in:
- lib/sportdb/sync/sync.rb
Class Method Summary collapse
-
.create_or_update(match, event:) ⇒ Object
todo/fix: rename to create!! (add update support later) !!!! use update_by_round or update_by_date or update_by_teams or such NO easy (unique always auto-id match) possible!!!!!!.
Class Method Details
.create_or_update(match, event:) ⇒ Object
todo/fix: rename to create!! (add update support later) !!!!
use update_by_round or update_by_date or update_by_teams or such
NO easy (unique always auto-id match) possible!!!!!!
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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 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 |
# File 'lib/sportdb/sync/sync.rb', line 143 def self.create_or_update( match, event: ) ## note: MUST find round, thus, use bang (!) ## todo/check: allow strings too - why? why not? round_rec = if match.round ## query for round - allow string or round rec round_name = match.round.is_a?( String ) ? match.round : match.round.name Model::Round.find_by!( event_id: event.id, name: round_name ) else # note: allow matches WITHOUT rounds too (e.g. England Football League 1888 and others) nil end ## todo/check: allow fallback with db lookup if NOT found in cache - why? why not? ## or better use Sync::Team.find_or_create( team ) !!!!!!! to auto-create on first hit! ## || Team.find_or_create( team1 ) -- note: does NOT work for string (only recs) - what to do? ## || Model::Team.find_by!( name: team1_name ) team1_name = match.team1.is_a?( String ) ? match.team1 : match.team1.name team1_rec = Team.cache[ team1_name ] team2_name = match.team2.is_a?( String ) ? match.team2 : match.team2.name team2_rec = Team.cache[ team2_name ] ## check optional group (e.g. Group A, etc.) group_rec = if match.group group_name = match.group.is_a?( String ) ? match.group : match.group.name Model::Group.find_by!( event_id: event.id, name: group_name ) else nil end ## check optional stage (e.g. Regular, Play Off, Relegation, etc. ) stage_rec = if match.stage stage_name = match.stage.is_a?( String ) ? match.stage : match.stage.name Model::Stage.find_by!( event_id: event.id, name: stage_name ) else nil end ### todo/check: what happens if there's more than one match? exception raised?? rec = if round_rec ## add match status too? allows [abandoned] and [replay] in same round Model::Match.find_by( round_id: round_rec.id, team1_id: team1_rec.id, team2_id: team2_rec.id ) else ## always assume new record for now ## check for date or such - why? why not? nil end if rec.nil? ## find last pos - check if it can be nil? yes, is nil if no records found max_pos = Model::Match.where( event_id: event.id ).maximum( 'pos' ) max_pos = max_pos ? max_pos+1 : 1 attribs = { event_id: event.id, ## todo/fix: change to data struct too? team1_id: team1_rec.id, team2_id: team2_rec.id, pos: max_pos, date: match.date.to_date, ## todo/fix: split and add date & time!!!! score1: match.score1, score2: match.score2, score1i: match.score1i, score2i: match.score2i, score1et: match.score1et, score2et: match.score2et, score1p: match.score1p, score2p: match.score2p, status: match.status } attribs[ :round_id ] = round_rec.id if round_rec attribs[ :group_id ] = group_rec.id if group_rec attribs[ :stage_id ] = stage_rec.id if stage_rec rec = Model::Match.create!( attribs ) else # update - todo puts "!! ERROR - match updates not yet supported (only inserts); sorry" exit 1 end rec end |