Class: Sports::Team

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/search/sport-teams.rb,
lib/sportdb/search/sport-history.rb

Constant Summary collapse

CLUB_NAME_RE =
%r{^
  (?<name>[^()]+?)     ## non-greedy
  (?:
     \s+
     \(
       (?<code>[A-Z][A-Za-z]{2,3})    ## optional (country) code; support single code e.g. (A) - why? why not?
     \)
  )?
$}x

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._find_by!(name:, league:, mods: nil) ⇒ Object

note: missing teams will get

   auto-created if possible
only ambigious results (too many matches) raise expection!!!


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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sportdb/search/sport-teams.rb', line 54

def self._find_by!( name:, league:, mods: nil )
  if mods && mods[ league.key ] && mods[ league.key ][ name ]
    mods[ league.key ][ name ]
  else
    ### quick hack

    ##    rename more placeholder teams to N.N.

    name = 'N.N.'  if ['Verlierer HF 1',
                       'Verlierer HF 2'].include?(name)

    if league.clubs?

     ## check for placeholder/global dummy clubs first

     if ['N.N.', 'N. N.'].include?( name )
         Club.find!( name )
     else
       if league.intl?    ## todo/fix: add intl? to ActiveRecord league!!!

             ###

             ##  get country code from name

             ##    e.g. Liverpool FC (ENG) or

             ##         Liverpool FC (URU) etc.


             ## check for country code

             if m=CLUB_NAME_RE.match( name )
               if m[:code]
                 rec =  Club.find_by( name: m[:name],
                                      country: m[:code] )
                 if rec.nil?
                   puts "auto-create (missing) club #{name}"
                   ##  todo/fix: add auto flag!!!!

                   ###              like in rounds!!!

                   ##   to track auto-created clubs

                   rec = Club.new( name: m[:name], auto: true )
                   rec.country = Country.find_by( code: m[:code] )   ## fix: country kwarg not yet supported!!

                   pp rec
                 end
                 rec
               else
                  Club.find!( name )
               end
             else
               puts "!! PARSE ERROR - invalid club name; cannot match with CLUB_NAME_RE >#{team}<"
               exit 1
             end
      else  ## assume clubs in domestic/national league tournament

         ## note - search by league countries (may incl. more than one country

         ##             e.g. us incl. ca, fr incl. mc, ch incl. li, etc.

        rec = Club.find_by( name: name, league: league )
        if rec.nil?
            puts "auto-create (missing) club #{name}"
            ##  todo/fix: add auto flag!!!!

            ###              like in rounds!!!

            ##   to track auto-created clubs

            rec = Club.new( name: name, auto: true )
            rec.country = league.country  ## fix: country kwarg not yet supported!!

            pp rec
        end
        rec
        end
      end
    else   ## assume national teams (not clubs)

      NationalTeam.find!( name )
    end
  end
end

.find_by!(name:, league:, mods: nil) ⇒ Object

todo/check: rename to/use map_by! for array version - why? why not?



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sportdb/search/sport-teams.rb', line 26

def self.find_by!( name:, league:, mods: nil )
  if name.is_a?( Array )
    recs = []
    name.each do |q|
      recs << _find_by!( name: q, league: league, mods: mods )
    end
    recs
  else  ## assume single name

    _find_by!( name: name, league: league, mods: mods )
  end
end

Instance Method Details

#name_by_season(season) ⇒ Object

add convenience lookup helper / method for name by season for now

use clubs history - for now kept separate from struct - why? why not?


9
10
11
12
13
# File 'lib/sportdb/search/sport-history.rb', line 9

def name_by_season( season )
      ## note: returns / fallback to "regular" name if no records found in history

      club_history = SportDb::Import.catalog.clubs_history
      club_history.find_name_by( name: name, season: season ) || name
end