Class: SportDb::Sync::Event

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

Class Method Summary collapse

Class Method Details

.find_by(league:, season:) ⇒ Object

finders

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sportdb/readers/sync/event.rb', line 8

def self.find_by( league:, season: )
  ## note: allow passing in of activerecord db records too - why? why not?
  ##   fix - change ArgumentError to TypeError !! (if TypeError exists)
  raise ArgumentError, "league struct record expected; got #{league.class.name}"   unless league.is_a?( Import::League )
  raise ArgumentError, "season struct record expected; got #{season.class.name}"   unless season.is_a?( Import::Season )

  ##  auto-create league and season (db) records if missing? - why? why not?
  season_rec = Season.find( season )
  league_rec = League.find( league )

  rec = nil
  rec = Model::Event.find_by( league_id: league_rec.id,
                              season_id: season_rec.id )  if season_rec && league_rec
  rec
end

.find_by!(league:, season:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/sportdb/readers/sync/event.rb', line 24

def self.find_by!( league:, season: )
  rec = find_by( league: league, season: season )
  if rec.nil?
    puts "** !!!ERROR!!! db sync - no event match found for:"
    pp league
    pp season
    exit 1
  end
  rec
end

.find_or_create_by(league:, season:) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/sportdb/readers/sync/event.rb', line 35

def self.find_or_create_by( league:, season: )
  ## note: allow passing in of activerecord db records too - why? why not?
  raise ArgumentError, "league struct record expected; got #{league.class.name}"  unless league.is_a?( Import::League )
  raise ArgumentError, "season struct record expected; got #{season.class.name}"  unless season.is_a?( Import::Season )

  ## note: auto-creates league and season (db) records if missing - why? why not?
  season_rec = Season.find_or_create( season )
  league_rec = League.find_or_create( league )

  rec = Model::Event.find_by( league_id: league_rec.id,
                              season_id: season_rec.id )
  if rec.nil?
    attribs = {
      league_id: league_rec.id,
      season_id: season_rec.id,
    }

    ## quick hack/change later !!
    ##  todo/fix: check season  - if is length 4 (single year) use 2017, 1, 1
    ##                               otherwise use 2017, 7, 1
    ##  start_at use year and 7,1 e.g. Date.new( 2017, 7, 1 )
    ## hack:  fix/todo1!!
    ##   add "fake" start_date for now
    attribs[:start_date]  = if season.year?  ## e.g. assume 2018 etc.
                             Date.new( season.start_year, 1, 1 )
                          else  ## assume 2014/15 etc.
                             Date.new( season.start_year, 7, 1 )
                          end

    rec = Model::Event.create!( attribs )
  end
  rec
end