Class: CatalogDb::Metal::Player

Inherits:
PlayerRecord show all
Defined in:
lib/sportdb/catalogs/player.rb

Class Method Summary collapse

Methods inherited from PlayerRecord

database, database=, database?

Methods inherited from BaseRecord

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

Class Method Details

._build_player(row) ⇒ Object

todo/fix:

change sqlite config to return records as hash NOT array!!!!


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sportdb/catalogs/player.rb', line 24

def self._build_player( row )
    ## note: cache structs by key (do NOT rebuild duplicates; reuse)
    @cache ||= Hash.new
    @cache[ row[0] ] ||= begin 
                            ## use Sports::Player.new  - why? why not?
                            player = Sports::Player.new(
                                       name:       row[1],
                                       nat:        row[3],
                                       height:     row[4],
                                       pos:        row[5],  ## make pos into an array??
                                       birthdate:  row[6],  ## todo/fix - convert to date??
                                       birthplace: row[7],
                                        )
                            
                            if row[2]
                              alt_names = row[2].split( '|' )
                              alt_names = alt_names.map { |name| name.strip }
                              player.alt_names += alt_names 
                            end
                            player
                          end                                                     
end

._find_by_name(name) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/sportdb/catalogs/player.rb', line 48

def self._find_by_name( name )
    rows = execute( <<-SQL )
    SELECT #{self.columns.join(', ')}
    FROM persons 
    INNER JOIN person_names ON persons.id  = person_names.person_id
    WHERE person_names.name = '#{name}' 
SQL
   rows 
end

._find_by_name_and_country(name, country) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/sportdb/catalogs/player.rb', line 58

def self._find_by_name_and_country( name, country )
  rows = execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM persons 
  INNER JOIN person_names ON persons.id  = person_names.person_id
  WHERE person_names.name = '#{name}' AND 
        persons.nat = '#{country}'
SQL
  rows 
end

._find_by_name_and_year(name, year) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sportdb/catalogs/player.rb', line 69

def self._find_by_name_and_year( name, year )
  ## check if there's a date function for year 
  ##  (using integer compare instead of string with qoutes???) 
  ##  use cast to convert year to integer - why? why not? 

  rows = execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM persons 
  INNER JOIN person_names ON persons.id  = person_names.person_id
  WHERE person_names.name = '#{name}' AND 
        CAST(strftime('%Y',persons.birthdate) AS INTEGER) = #{year}
SQL
  rows 
end

._find_by_name_and_year_and_country(name, year, country) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sportdb/catalogs/player.rb', line 84

def self._find_by_name_and_year_and_country( name, year, country )
  rows = execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM persons 
  INNER JOIN person_names ON persons.id  = person_names.person_id
  WHERE person_names.name = '#{name}' AND 
        CAST(strftime('%Y',persons.birthdate) AS INTEGER) = #{year} AND
        persons.nat = '#{country}'
SQL
  rows 
end

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

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sportdb/catalogs/player.rb', line 98

def self.match_by( name:, 
                   country: nil,
                   year:    nil )
  nameq = normalize( unaccent(name) )

  rows = if country 
            country = _country( country )
            countryq = country.key
            if year
              _find_by_name_and_year_and_country( nameq, year, countryq ) 
            else    ## no year (country only)
              _find_by_name_and_country( nameq, countryq )
            end
         elsif year && country.nil?
            _find_by_name_and_year( nameq, year )
         elsif year.nil? && country.nil?
           _find_by_name( nameq )
         else
            raise ArgumentError, "unsupported query for player; sorry"
         end
  
  rows.map {|row| _build_player( row )}
end