Class: Sports::Club
- Inherits:
-
Object
- Object
- Sports::Club
- Defined in:
- lib/sportdb/search/sport-clubs.rb
Class Method Summary collapse
- ._search ⇒ Object
-
.build_mods(mods) ⇒ Object
more support methods.
-
.find(name) ⇒ Object
“legacy” finders - return zero or one club (if more than one match, exit/raise error/exception).
- .find!(name) ⇒ Object
- .find_by(name:, country: nil, league: nil) ⇒ Object
-
.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.
-
.match(name) ⇒ Object
more deriv support functions / helpers.
-
.match_by(name:, country: nil, league: nil, mods: nil) ⇒ Object
add mods here - why? why not?.
Class Method Details
._search ⇒ Object
6 |
# File 'lib/sportdb/search/sport-clubs.rb', line 6 def self._search() CatalogDb::Metal::Club; end |
.build_mods(mods) ⇒ Object
more support methods
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sportdb/search/sport-clubs.rb', line 105 def self.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)
59 |
# File 'lib/sportdb/search/sport-clubs.rb', line 59 def self.find( name ) find_by( name: name ); end |
.find!(name) ⇒ Object
60 |
# File 'lib/sportdb/search/sport-clubs.rb', line 60 def self.find!( name ) find_by!( name: name ); end |
.find_by(name:, country: nil, league: nil) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sportdb/search/sport-clubs.rb', line 79 def self.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
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sportdb/search/sport-clubs.rb', line 64 def self.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
54 |
# File 'lib/sportdb/search/sport-clubs.rb', line 54 def self.match( name ) match_by( name: name ); end |
.match_by(name:, country: nil, league: nil, mods: nil) ⇒ Object
add mods here - why? why not?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sportdb/search/sport-clubs.rb', line 12 def self.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 ) _search.match_by( name: name, country: country ) else _search.match_by( name: name, country: country ) end end |