Class: SportDb::Sync::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/sync/sync.rb

Class Method Summary collapse

Class Method Details

.create_or_update(match, event:) ⇒ Object



140
141
142
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
# File 'lib/sportdb/sync/sync.rb', line 140

def self.create_or_update( match, event: )
   ## note: MUST find round, thus, use bang (!)

   ## todo/check: allow strings too - why? why not?

   ## query for round - allow string or round rec
   round_name  = match.round.is_a?( String ) ? match.round : match.round.name
   round_rec   = Model::Round.find_by!( event_id: event.id,
                                        name:     round_name )

   ## 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 once match? exception raised??
   rec = Model::Match.find_by( round_id: round_rec.id,
                              team1_id: team1_rec.id,
                              team2_id: team2_rec.id )
   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?
                 round_id: round_rec.id,
                 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 }

     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
   end
   rec
end