Class: Sports::Country

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/search/world.rb

Class Method Summary collapse

Class Method Details

._searchObject



14
# File 'lib/sportdb/search/world.rb', line 14

def self._search() CatalogDb::Metal::Country; end

.find(q) ⇒ Object Also known as: []

find by code (first) or name (second)



27
28
29
# File 'lib/sportdb/search/world.rb', line 27

def self.find( q )   ## find by code (first) or name (second)
    _search.find_by_name_or_code( q )
end

.find_by(code: nil, name: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/sportdb/search/world.rb', line 16

def self.find_by( code: nil, name: nil )
    ## todo/fix upstream - change to find_by( code:, name:, ) too - why? why not?
    if code && name.nil?
      _search.find_by_code( code )
    elsif name && code.nil?
      _search.find_by_name( name )
    else
      raise ArgumentError, "Country#find_by - one (and only one arg) required - code: or name:"
    end
end

.parse_heading(line) ⇒ Object Also known as: heading

split/parse country line

split on bullet e.g.
 split into name and code with regex - make code optional

Examples:
  Österreich • Austria (at)
  Österreich • Austria
  Austria
  Deutschland (de) • Germany

 todo/check: support more formats - why? why not?
     e.g.  Austria, AUT  (e.g. with comma - why? why not?)


45
46
47
48
49
50
51
52
53
54
55
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sportdb/search/world.rb', line 45

def self.parse_heading( line )
  values = line.split( '' )   ## use/support multi-lingual separator
  country = nil
  values.each do |value|
     value = value.strip
     ## check for trailing country code e.g. (at), (eng), etc
     ##   allow code 1 to 5 for now - northern cyprus(fifa) with 5 letters?.
     ##     add/allow  gb-eng, gb-wal (official iso2!!), in the future too - why? why not?
     if value =~ /[ ]+\((?<code>[A-Za-z]{1,5})\)$/  ## e.g. Austria (at)
       code =  $~[:code]
       name = value[0...(value.size-code.size-2)].strip  ## note: add -2 for brackets
       candidates = [ find_by( code: code ), find_by( name: name ) ]
       if candidates[0].nil?
         puts "** !!! ERROR Country.parse_heading - unknown code >#{code}< in line: #{line}"
         pp line
         exit 1
       end
       if candidates[1].nil?
         puts "** !!! ERROR Country.parse_heading - unknown name >#{code}< in line: #{line}"
         pp line
         exit 1
       end
       if candidates[0] != candidates[1]
         puts "** !!! ERROR Country.parse_heading - name and code do NOT match the same country:"
         pp line
         pp candidates
         exit 1
       end
       if country && country != candidates[0]
         puts "** !!! ERROR Country.parse_heading - names do NOT match the same country:"
         pp line
         pp country
         pp candidates
         exit 1
       end
       country = candidates[0]
     else
       ## just assume value is name or code
       candidate = find( value )
       if candidate.nil?
         puts "** !!! ERROR Country.parse_heading - unknown name or code >#{value}< in line: #{line}"
         pp line
         exit 1
       end
       if country && country != candidate
         puts "** !!! ERROR Country.parse_heading - names do NOT match the same country:"
         pp line
         pp country
         pp candidate
         exit 1
       end
       country = candidate
     end
  end
  country
end