Class: CatalogDb::Metal::City

Inherits:
Record show all
Defined in:
lib/sportdb/catalogs/city.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_city(row) ⇒ Object



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

def self._build_city( row )
    ## note: cache structs by key (do NOT rebuild duplicates; reuse)
    cache[ row[0] ] ||= begin 
                            city = Sports::City.new(
                                     key:     row[0],
                                     name:    row[1],
                                     ## fix - add alt_names here too 
                                     country: _to_country( row[3] ) 
                                   )
                            city
                          end                                                    
end

._find_by_name(name) ⇒ Object



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

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

._record(key) ⇒ Object

use _record! as name - why? why not?



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

def self._record( key )  ## use _record! as name - why? why not?
    if (rec = cache[ key ])
      rec   ## return cached
    else  ## query and cache and return
    rows = execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM cities 
  WHERE cities.key = '#{key}' 
SQL
 
      ## todo/fix: also assert for rows == 1 AND NOT MULTIPLE records - why? why not?
      if rows.empty? 
        raise ArgumentError, "city record with key #{key} not found" 
      else 
        _build_city( rows[0] )
      end
    end
end

.cacheObject



13
# File 'lib/sportdb/catalogs/city.rb', line 13

def self.cache() @cache ||= Hash.new; end

.match_by(name:) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sportdb/catalogs/city.rb', line 60

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

  rows = _find_by_name( nameq )
         
  rows.map {|row| _build_city( row )}
end