class ICU::Federation

This class can be used to map a string into an object representing a chess federation. In FIDE, chess federations are generally either referred to by their full names such as Ireland or Russia or by three letter codes such as IRL or RUS. The three letter codes are mostly the same as those found in the international standard known as ISO 3166-1 alpha-3, but with some differences (e.g. for England, Scotland and Wales).

You cannot directly create instances of this class using new. Instead, you supply a string to the class method find and, if the string supplied uniguely identifies a federation, an instance is returned which responds to name and code.

fed = ICU::Federation.find('IRL')
fed.name                                           # => "Ireland"
fed.code                                           # => "IRL"

If the string is not sufficient to identify a federation, the find method returns nil.

fed = ICU::Federation.find('ZYX')                  # => nil

If the string is three letters long and matches (case insenstively) one of the unique federation codes, then the instance corresponding to that federation is returned.

ICU::Federation.find('rUs').code                   # => "RUS"

If the string is more than three letters long and if it is a substring (case insensitive) of exactly one federation name, then that federation is returned.

ICU::Federation.find('ongoli').name                # => "Mongolia"

In all other cases, nil is returned. In the following example, the string matches more than one federation.

ICU::Federation.find('land')                       # => nil

The method is not fooled by irrelevant white space.

ICU::Federation.find('  united   states   ').code  # => 'USA'

The class method menu will return an array of two-element arrays each of which contain a name and a code.

ICU::Federation.menu                               # => [['Afghanistan', 'AFG'], ['Albania', 'ALB], ...]

Such an array could be used, for example, as the basis of a selection menu in a web application. Various options are available to alter the array returned. Use the :order option to order by code instead of the default (by country name).

ICU::Federation.menu(:order => 'code')             # => [..., ['Ireland', 'IRL'], ['Iraq', 'IRQ], ...]

To put one country at the top (followed by the rest, in order) supply the country's code with the :top option:

ICU::Federation.menu(:top => 'IRL')                # => [['Ireland', 'IRL'], ['Afghanistan', 'AFG], ...]

To supply an extra “None” item at the top, specify its label with the :none option:

ICU::Federation.menu(:none => 'None')              # => [['None', ''], ['Afghanistan', 'AFG], ...]

The “None” option's code is the empty string and it comes above the “top” option if both are specified.

Attributes

code[R]
name[R]

Public Class Methods

codes() click to toggle source

Return an array of sorted federation codes.

# File lib/icu_utils/federation.rb, line 102
def self.codes
  compile
  @@objects.map(&:code).sort
end
find(str=nil) click to toggle source

Given a code, name or part of a name, return the corresponding federation instance. If there is no match or more than one match, nil is returned.

# File lib/icu_utils/federation.rb, line 67
def self.find(str=nil)
  return nil unless str
  str = str.to_s
  return nil if str.length < 3
  compile
  str = str.strip.squeeze(' ').downcase
  if str.length == 3
    str = correct_common_errors(str)
    return @@codes[str]
  end
  return @@names[str] if @@names[str]
  matches = Array.new
  @@names.each_key do |name|
    matches << @@names[name] if name.index(str)
  end
  matches.uniq!
  return nil unless matches.length == 1
  matches[0]
end
menu(opts = {}) click to toggle source

Return an array of codes and names suitable for creating a federation menu in Rails.

ICU::Federation.menu(:order => 'code')     # order federations by code (instead of by name)
ICU::Federation.menu(:top => 'IRL')        # make this federation come first
ICU::Federation.menu(:none => 'None')      # add a dummy top entry with name "None" and blank code