Class: CatalogDb::Metal::Country
- Inherits:
-
Record
- Object
- BaseRecord
- Record
- CatalogDb::Metal::Country
- Defined in:
- lib/sportdb/catalogs/country.rb
Class Method Summary collapse
- ._build_country(row) ⇒ Object
-
._record(key) ⇒ Object
use _record! as name - why? why not?.
- .cache ⇒ Object
-
.find_by_code(code) ⇒ Object
fix/todo: add find_by (alias for find_by_name/find_by_code).
-
.find_by_name(name) ⇒ Object
note - need to escape name ? e.g.
- .find_by_name_or_code(q) ⇒ Object
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_country(row) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sportdb/catalogs/country.rb', line 37 def self._build_country( row ) ## note: cache structs by key (do NOT rebuild duplicates; reuse) cache[ row[0] ] ||= begin ## note: split tags & alt names (PLUS remove leading//trailing spaces) = row[3].split(',').map {|tag| tag.strip } alt_names = row[4].split('|').map {|alt_name| alt_name.strip } country = Sports::Country.new( key: row[0], name: row[1], code: row[2], tags: ) country.alt_names = alt_names country end end |
._record(key) ⇒ Object
use _record! as name - why? why not?
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sportdb/catalogs/country.rb', line 17 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 countries WHERE countries.key = '#{key}' SQL ## todo/fix: also assert for rows == 1 AND NOT MULTIPLE records - why? why not? if rows.empty? raise ArgumentError, "country record with key #{key} not found" else _build_country( rows[0] ) end end end |
.cache ⇒ Object
14 |
# File 'lib/sportdb/catalogs/country.rb', line 14 def self.cache() @cache ||= Hash.new; end |
.find_by_code(code) ⇒ Object
fix/todo: add find_by (alias for find_by_name/find_by_code)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sportdb/catalogs/country.rb', line 56 def self.find_by_code( code ) q = code.to_s.downcase ## allow symbols (and always downcase e.g. AUT to aut etc.) ## note: results in ## Côte d'Ivoire => côte d'ivoire ## quote will break sql!!! ## remove - spaces etc. ## for now remove only single quote (will break sql) - add more? ## ## or use escape_sql_string - possible?? q = q.gsub( /[']/, '' ) rows = execute( <<-SQL ) SELECT #{self.columns.join(', ')} FROM countries INNER JOIN country_codes ON countries.key = country_codes.key WHERE country_codes.code = '#{q}' SQL if rows.empty? nil else ## todo/fix - raise error if more than once record found!!! _build_country( rows[0] ) end end |
.find_by_name(name) ⇒ Object
note - need to escape name ?
e.g. Côte d'Ivoire
make sure normalize removes single quotes (')!!!
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sportdb/catalogs/country.rb', line 92 def self.find_by_name( name ) q = normalize( unaccent( name.to_s )) ## allow symbols too (e.g. use to.s first) rows = execute( <<-SQL ) SELECT #{self.columns.join(', ')} FROM countries INNER JOIN country_names ON countries.key = country_names.key WHERE country_names.name = '#{q}' SQL if rows.empty? nil else ## todo/fix - raise error if more than once record found!!! _build_country( rows[0] ) end end |
.find_by_name_or_code(q) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/sportdb/catalogs/country.rb', line 111 def self.find_by_name_or_code( q ) name = normalize( unaccent( q.to_s )) ## allow symbols too (e.g. use to.s first) code = q.to_s.downcase ## allow symbols (and always downcase e.g. AUT to aut etc.) ## note: results in ## Côte d'Ivoire => côte d'ivoire ## quote will break sql!!! ## remove - spaces etc. ## for now remove only single quote (will break sql) - add more? ## ## or use escape_sql_string - possible?? code = code.gsub( /[']/, '' ) rows = execute( <<-SQL ) SELECT #{self.columns.join(', ')} FROM countries INNER JOIN country_names ON countries.key = country_names.key WHERE country_names.name = '#{name}' UNION SELECT #{self.columns.join(', ')} FROM countries INNER JOIN country_codes ON countries.key = country_codes.key WHERE country_codes.code = '#{code}' SQL if rows.empty? nil else ## todo/fix - raise error if more than once record found!!! _build_country( rows[0] ) end end |