Class: SportSearch::ClubSearch

Inherits:
Search
  • Object
show all
Defined in:
lib/sportdb/search/sport.rb

Instance Attribute Summary

Attributes inherited from Search

#service

Instance Method Summary collapse

Methods inherited from Search

#initialize

Constructor Details

This class inherits a constructor from SportSearch::Search

Instance Method Details

#build_mods(mods) ⇒ Object

more support methods



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/sportdb/search/sport.rb', line 208

def build_mods( mods )
  ## e.g.
  ##  { 'Arsenal   | Arsenal FC'    => 'Arsenal, ENG',
  ##    'Liverpool | Liverpool FC'  => 'Liverpool, ENG',
  ##    'Barcelona'                 => 'Barcelona, ESP',
  ##    'Valencia'                  => 'Valencia, ESP' }

  mods.reduce({}) do |h,(club_names, club_line)|

    values = club_line.split( ',' )
    values = values.map { |value| value.strip }  ## strip all spaces

    ## todo/fix: make sure country is present !!!!
    club_name, country_name = values
    club = find_by!( name: club_name, country: country_name )

    values = club_names.split( '|' )
    values = values.map { |value| value.strip }  ## strip all spaces

    values.each do |club_name|
      h[club_name] = club
    end
    h
  end
end

#find(name) ⇒ Object

“legacy” finders - return zero or one club

(if more than one match, exit/raise error/exception)


162
# File 'lib/sportdb/search/sport.rb', line 162

def find( name )   find_by( name: name ); end

#find!(name) ⇒ Object



163
# File 'lib/sportdb/search/sport.rb', line 163

def find!( name )  find_by!( name: name ); end

#find_by(name:, country: nil, league: nil) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sportdb/search/sport.rb', line 182

def find_by( name:, country: nil,
                    league:  nil )    ## todo/fix: add international or league flag?
  ## note: allow passing in of country key too (auto-counvert)
  ##       and country struct too
  ##     - country assumes / allows the country key or fifa code for now
  recs = match_by( name:    name,
                   country: country,
                   league:  league )

  club = nil
  if recs.empty?
    ## puts "** !!! WARN !!! no match for club >#{name}<"
  elsif recs.size > 1
    puts "** !!! ERROR - too many matches (#{recs.size}) for club >#{name}<:"
    pp recs
    exit 1
  else   # bingo; match - assume size == 1
    club = recs[0]
  end

  club
end

#find_by!(name:, country: nil, league: nil) ⇒ Object

find - always returns a single record / match or nil

if there is more than one match than find aborts / fails


167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/sportdb/search/sport.rb', line 167

def find_by!( name:, country: nil,
                     league:  nil )    ## todo/fix: add international or league flag?
  club = find_by( name:    name,
                  country: country,
                  league:  league )

  if club.nil?
    puts "** !!! ERROR - no match for club >#{name}<"
    exit 1
  end

  club
end

#match(name) ⇒ Object

more deriv support functions / helpers



157
# File 'lib/sportdb/search/sport.rb', line 157

def match( name ) match_by( name: name ); end

#match_by(name:, country: nil, league: nil, mods: nil) ⇒ Object

add mods here - why? why not?



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sportdb/search/sport.rb', line 115

def match_by( name:, country: nil,
                     league:  nil,
                     mods:    nil )
  ## for now assume "global" mods  - checks only for name
  ##
  if mods && mods[ name ]
    club = mods[ name ]
    return [club]   # note: wrap (single record) in array
  end

  ## note: add "auto-magic" country calculation via league record
  ##            if league is a national league for football clubs
  if league
      raise ArgumentError, "match_by - league AND country NOT supported; sorry"  if country
### find countries via league
###     support league.intl? too - why? why not?
###       or only nationa league
      raise ArgumentError, "match_by - league - only national club leagues supported (not int'l or national teams for now); sorry"   unless league.national? && league.clubs?

      ### calc countries
      ### uses "global" func in sports-catalogs for now
      ##   move code here - why? why not?
      country = find_countries_for_league( league )
      @service.match_by( name:    name,
                         country: country )
  else
      @service.match_by( name:    name,
                         country: country )
  end
end