Class: ICU::Federation
- Inherits:
-
Object
- Object
- ICU::Federation
- Defined in:
- lib/icu_tournament/federation.rb
Overview
Federations
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. # => [['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.(: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.(:top => 'IRL') # => [['Ireland', 'IRL'], ['Afghanistan', 'AFG], ...]
To supply an extra “None” item at the top, specify its label with the :none option:
ICU::Federation.(:none => 'None') # => [['None', ''], ['Afghanistan', 'AFG], ...]
The “None” option’s code is the empty string and it come above the “top” option if both are specified.
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.find(str = nil) ⇒ Object
Given a code, name or part of a name, return the corresponding federation instance.
- .menu(opts = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(code, name) ⇒ Federation
constructor
:nodoc: because new is private.
Constructor Details
#initialize(code, name) ⇒ Federation
:nodoc: because new is private
100 101 102 103 |
# File 'lib/icu_tournament/federation.rb', line 100 def initialize(code, name) # :nodoc: because new is private @code = code @name = name end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
68 69 70 |
# File 'lib/icu_tournament/federation.rb', line 68 def code @code end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
68 69 70 |
# File 'lib/icu_tournament/federation.rb', line 68 def name @name end |
Class Method Details
.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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/icu_tournament/federation.rb', line 73 def self.find(str=nil) return nil unless str str = str.to_s return nil if str.length < 3 compile unless @@objects str = str.strip.squeeze(' ').downcase return @@codes[str] if str.length == 3 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 = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/icu_tournament/federation.rb', line 90 def self.(opts = {}) compile unless @@objects; top, = nil, [] @@objects.each {|o| opts[:top] == o.code ? top = [o.name, o.code] : .push([o.name, o.code]) } opts[:order] == 'code' ? .sort!{|a,b| a.last <=> b.last} : .sort!{|a,b| a.first <=> b.first} .unshift(top) if top .unshift([opts[:none], '']) if opts[:none] end |