Class: CatalogDb::Metal::Ground

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



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

def self._build_ground( row )
    ## note: cache structs by key (do NOT rebuild duplicates; reuse)
    @cache ||= Hash.new
    @cache[ row[0] ] ||= begin 
                            ground = Sports::Ground.new(
                                     key:  row[0],
                                     name: row[1] )
                            
                            if row[2]
                              alt_names = row[2].split( '|' )
                              alt_names = alt_names.map { |name| name.strip }
                              ground.alt_names += alt_names 
                            end
                            ground.city     =  _to_city( row[3] )
                            ground.country  =  _to_country( row[4] )
                            ground.district =  row[5]
                            ground.address  =  row[6]
                            ground
                          end                                                     
end

._find_by_name(name) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/sportdb/catalogs/ground.rb', line 39

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

._find_by_name_and_city(name, city) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/sportdb/catalogs/ground.rb', line 60

def self._find_by_name_and_city( name, city )
  rows = execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM grounds 
  INNER JOIN ground_names ON grounds.key  = ground_names.key
  WHERE ground_names.name = '#{name}' AND 
        grounds.city_key = '#{city}'
SQL
  rows 
end

._find_by_name_and_country(name, country) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/sportdb/catalogs/ground.rb', line 49

def self._find_by_name_and_country( name, country )
  rows = execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM grounds 
  INNER JOIN ground_names ON grounds.key  = ground_names.key
  WHERE ground_names.name = '#{name}' AND 
        grounds.country_key = '#{country}'
SQL
  rows 
end

.match_by(name:, country: nil, city: 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
100
101
# File 'lib/sportdb/catalogs/ground.rb', line 74

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

  rows = if country && city.nil?
            country = _country( country )
            countryq = country.key
            _find_by_name_and_country( nameq, countryq )
         elsif city && country.nil?
            city    = _city( city )
            cityq   = city.key 
            _find_by_name_and_city( nameq, cityq )
         elsif city.nil? && country.nil?
           _find_by_name( nameq )
         else
            raise ArgumentError, "unsupported city query params pairs; sorry"
         end
  
  rows.map {|row| _build_ground( row )}
end