Class: SportDb::Sync::Event

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

Class Method Summary collapse

Class Method Details

.find_by(league:, season:) ⇒ Object

finders

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sportdb/sync/event.rb', line 34

def self.find_by( league:, season: )
  ## note: allow passing in of activerecord db records too - why? why not?
  raise ArgumentError.new( "league struct record expected; got #{league.class.name}" ) unless league.is_a?( Import::League )
  raise ArgumentError.new( "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



49
50
51
52
53
54
55
56
57
58
# File 'lib/sportdb/sync/event.rb', line 49

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)


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

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

.league(q) ⇒ Object



5
# File 'lib/sportdb/sync/event.rb', line 5

def self.league( q ) League.league( q ); end

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

searchers

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
# File 'lib/sportdb/sync/event.rb', line 11

def self.search_by!( league:, season: )
  raise ArgumentError.new( "league query string expected; got #{league.class.name}" ) unless league.is_a?( String )
  raise ArgumentError.new( "season query string expected; got #{season.class.name}" ) unless season.is_a?( String )

  league = league( league )
  season = season( season )

  find_by( league: league, season: season )
end

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

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
# File 'lib/sportdb/sync/event.rb', line 21

def self.search_or_create_by!( league:, season: )
   raise ArgumentError.new( "league query string expected; got #{league.class.name}" ) unless league.is_a?( String )
   raise ArgumentError.new( "season query string expected; got #{season.class.name}" ) unless season.is_a?( String )

   league = league( league )
   season = season( season )

   find_or_create_by( league: league, season: season )
end

.season(q) ⇒ Object



6
# File 'lib/sportdb/sync/event.rb', line 6

def self.season( q ) Season.season( q ); end