Class: ICU::Federation

Inherits:
Object
  • Object
show all
Defined in:
lib/icu_utils/federation.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, name) ⇒ Federation

:nodoc: because new is private



107
108
109
110
# File 'lib/icu_utils/federation.rb', line 107

def initialize(code, name) # :nodoc: because new is private
  @code = code
  @name = name
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



62
63
64
# File 'lib/icu_utils/federation.rb', line 62

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



62
63
64
# File 'lib/icu_utils/federation.rb', line 62

def name
  @name
end

Class Method Details

.codesObject

Return an array of sorted federation codes.



102
103
104
105
# File 'lib/icu_utils/federation.rb', line 102

def self.codes
  compile
  @@objects.map(&:code).sort
end

.find(str = nil) ⇒ Object

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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

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


91
92
93
94
95
96
97
98
99
# File 'lib/icu_utils/federation.rb', line 91

def self.menu(opts = {})
  compile
  top, menu = nil, []
  @@objects.each {|o| opts[:top] == o.code ? top = [o.name, o.code] : menu.push([o.name, o.code]) }
  opts[:order] == 'code' ? menu.sort!{|a,b| a.last <=> b.last} : menu.sort!{|a,b| a.first <=> b.first}
  menu.unshift(top) if top
  menu.unshift([opts[:none], '']) if opts[:none]
  menu
end