Class: CatalogDb::Metal::Club

Inherits:
Record show all
Defined in:
lib/sportdb/catalogs/club.rb

Class Method Summary collapse

Methods inherited from Record

_city, _league, _to_city, _to_country, _to_league, database, database=, database?

Methods inherited from BaseRecord

_country, _to_bool, columns, columns=, count, execute, tablename, tablename=

Class Method Details

._build_club(row) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sportdb/catalogs/club.rb', line 15

def self._build_club( row )
    ## note: cache structs by key (do NOT rebuild duplicates; reuse)
    @cache ||= Hash.new
    @cache[ row[0] ] ||= begin 
                            club = Sports::Club.new(
                                     key:  row[0],
                                     name: row[1],
                                     code: row[2] )

                            club.city     = row[3]  ## might be nil
                            club.district = row[4]  
                            ## note: country for now NOT supported 
                            ##       via keyword on init!!!
                            ##    fix - why? why not?
                            club.country = row[5] ? _to_country( row[5] ) : nil
                            club 
                          end   
                                               
end

._find_by_name(name) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/sportdb/catalogs/club.rb', line 36

def self._find_by_name( name )
    rows = execute( <<-SQL )
    SELECT #{self.columns.join(', ')}
    FROM clubs 
    INNER JOIN club_names ON clubs.key  = club_names.key
    WHERE club_names.name = '#{name}' 
SQL
   rows 
end

._find_by_name_and_country(name, country_or_countries) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sportdb/catalogs/club.rb', line 47

def self._find_by_name_and_country( name, country_or_countries )
      sql = <<-SQL
  SELECT #{self.columns.join(', ')}
  FROM clubs 
  INNER JOIN club_names ON clubs.key  = club_names.key
  WHERE club_names.name = '#{name}' AND 
SQL
   
    ## check if single country or n countries         
     if country_or_countries.is_a?(Array) 
      countries = country_or_countries
      ## add (single) quotes and join with comma 
      ##       e.g.  'at','de'  
      country_list = countries.map {|country| %Q<'#{country}'>}.join(',')
      sql <<  "clubs.country_key in (#{country_list})\n"
     else ## assume string/symbol (single country key)
      country = country_or_countries
      sql <<  "clubs.country_key = '#{country}'\n"
     end
      
  rows = execute( sql )
  rows 
end

.match_by(name:, country: nil) ⇒ Object

match - always returns an array (with one or more matches)



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

def self.match_by( name:, 
                   country: nil )
  ## 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

  ##  note: ALWAYS unaccent 1st in normalize  
  ##            - add upstream - why? why not?
  # note: returns empty array (e.g. []) if no match and NOT nil
  nameq = normalize( unaccent(name) )

  ## todo - check for empty array passed in?
  rows = if country    
            ### always wrap country in array
            countries =  country.is_a?(Array) ? country : [country]
            country_keys = countries.map {|country| _country( country ).key }
            ## unwrap key if array with single country key
            countryq = country_keys.size == 1 ? country_keys[0] : country_keys

            _find_by_name_and_country( nameq, countryq )
         else
            _find_by_name( nameq )
         end
  
  rows.map {|row| _build_club( row )}
end