Class: SportDb::Sync::Club

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

Class Method Summary collapse

Class Method Details

.cacheObject

auto-cache all clubs by find_or_create for later mapping / lookup



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

def self.cache() @cache ||= {}; end

.club(q, league: nil) ⇒ Object

“internal” search helper using catalog



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

def self.club( q, league: nil)   ## "internal" search helper using catalog
  ## note: league.country might return nil (e.g. for intl leagues)
  country = league ? league.country : nil
  club = Import.catalog.clubs.find_by( name: q, country: country )

  if club.nil?
     ## todo/check: exit if no match - why? why not?
     puts "!!! *** ERROR *** no matching club found for >#{q}< - add to clubs setup"
     exit 1
  end
  club
end

.find_or_create(club) ⇒ Object

finders



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

def self.find_or_create( club )
  ## note: assume "canonical uniquie" names for now for clubs
  rec = Model::Team.find_by( name: club.name )
  if rec.nil?

    ## todo/fix:  move auto-key gen to structs for re(use)!!!!!!
    ## check if key is present otherwise generate e.g. remove all non-ascii a-z chars
    key  =  club.key || club.name.downcase.gsub( /[^a-z]/, '' )
    puts "add club: #{key}, #{club.name}, #{club.country.name} (#{club.country.key})"

    attribs = {
        key:        key,
        name:       club.name,
        country_id: Sync::Country.find_or_create( club.country ).id,
        club:       true,
        national:   false  ## check -is default anyway - use - why? why not?
        ## todo/fix: add city if present - why? why not?
    }

    attribs[:code] = club.code   if club.code   ## add code (abbreviation) if present

    if club.alt_names.empty? == false
      attribs[:alt_names] = club.alt_names.join('|')
    end

    rec = Model::Team.create!( attribs )
  end
  ## auto-add to cache
  cache[club.name] = rec

  rec
end

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

todo/fix - move array support for now to attic!!!



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sportdb/sync/club.rb', line 27

def self.search_or_create_by!( name:, league: nil, season: nil )
  ## note: season is for now optional (and unused) - add/use in the future!!!

  ## note: allow search by single name/q
  ##   or  allow search by list/array of names/qs tooo!!!
  if name.is_a?( Array )
    ## assume batch search return array of mappings
    club_recs = []
    name.each do |q|
      club        = club( q, league: league )
      clubs_recs << find_or_create( club )
    end
    club_recs
  else
    ## assume single search
    q = name
    club = club( q, league: league )
    find_or_create( club )
  end
end