sanichi-chess_icu 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 4
4
- :patch: 8
4
+ :patch: 9
data/lib/federation.rb CHANGED
@@ -33,14 +33,35 @@ of exactly one federation name, then that federation is returned.
33
33
 
34
34
  ICU::Federation.find('ongoli').name # => "Mongolia"
35
35
 
36
- In all other cases, nil is returned. In the following example, the string more than one federation.
36
+ In all other cases, nil is returned. In the following example, the string matches more than one federation.
37
37
 
38
38
  ICU::Federation.find('land') # => nil
39
39
 
40
40
  The method is not fooled by irrelevant white space.
41
41
 
42
42
  ICU::Federation.find(' united states ').code # => 'USA'
43
+
44
+ The class method _menu_ will return an array of two-element arrays each of which contain a name
45
+ and a code.
46
+
47
+ ICU::Federation.menu # => [['Afghanistan', 'AFG'], ['Albania', 'ALB], ...]
48
+
49
+ Such an array could be used, for example, as the basis of a selection menu in a web application.
50
+ Various options are available to alter the array returned. Use the _:order_ option to order by code
51
+ instead of the default (by country name).
52
+
53
+ ICU::Federation.menu(:order => 'code') # => [..., ['Ireland', 'IRL'], ['Iraq', 'IRQ], ...]
54
+
55
+ To put one country at the top (followed by the rest, in order) supply the country's code with the _:top_ option:
43
56
 
57
+ ICU::Federation.menu(:top => 'IRL') # => [['Ireland', 'IRL'], ['Afghanistan', 'AFG], ...]
58
+
59
+ To supply an extra "None" item at the top, specify its label with the _:none_ option:
60
+
61
+ ICU::Federation.menu(:none => 'None') # => [['None', ''], ['Afghanistan', 'AFG], ...]
62
+
63
+ The "None" option's code is the empty string and it come above the "top" option if both are specified.
64
+
44
65
  =end
45
66
 
46
67
  class Federation
@@ -66,6 +87,16 @@ The method is not fooled by irrelevant white space.
66
87
  matches[0]
67
88
  end
68
89
 
90
+ def self.menu(opts = {})
91
+ compile unless @@objects;
92
+ top, menu = nil, []
93
+ @@objects.each {|o| opts[:top] == o.code ? top = [o.name, o.code] : menu.push([o.name, o.code]) }
94
+ opts[:order] == 'code' ? menu.sort!{|a,b| a.last <=> b.last} : menu.sort!{|a,b| a.first <=> b.first}
95
+ menu.unshift(top) if top
96
+ menu.unshift([opts[:none], '']) if opts[:none]
97
+ menu
98
+ end
99
+
69
100
  def initialize(code, name) # :nodoc: because new is private
70
101
  @code = code
71
102
  @name = name
@@ -101,16 +101,75 @@ module ICU
101
101
 
102
102
  context "documentation examples" do
103
103
  it "should all be correct for valid input" do
104
- ICU::Federation.find('IRL').name.should == 'Ireland'
105
- ICU::Federation.find('IRL').code.should == 'IRL'
106
- ICU::Federation.find('rUs').code.should == 'RUS'
107
- ICU::Federation.find('ongoli').name.should == 'Mongolia'
108
- ICU::Federation.find(' united states ').code.should == 'USA'
104
+ Federation.find('IRL').name.should == 'Ireland'
105
+ Federation.find('IRL').code.should == 'IRL'
106
+ Federation.find('rUs').code.should == 'RUS'
107
+ Federation.find('ongoli').name.should == 'Mongolia'
108
+ Federation.find(' united states ').code.should == 'USA'
109
109
  end
110
110
 
111
111
  it "should return nil for invalid input" do
112
- ICU::Federation.find('ZYX').should be_nil
113
- ICU::Federation.find('land').should be_nil
112
+ Federation.find('ZYX').should be_nil
113
+ Federation.find('land').should be_nil
114
+ end
115
+ end
116
+
117
+ context "#menu" do
118
+ before(:all) do
119
+ @total = 173
120
+ end
121
+
122
+ it "should return array of name-code pairs in order of name by default" do
123
+ menu = Federation.menu
124
+ menu.should have(@total).items
125
+ names = menu.map{|m| m.first}.join(',')
126
+ codes = menu.map{|m| m.last}.join(',')
127
+ names.index('Afghanistan').should == 0
128
+ names.index('Iraq,Ireland,Israel').should_not be_nil
129
+ codes.index('AFG').should == 0
130
+ codes.index('IRQ,IRL,ISR').should_not be_nil
131
+ end
132
+
133
+ it "should be configuarble to order the list by codes" do
134
+ menu = Federation.menu(:order => "code")
135
+ menu.should have(@total).items
136
+ names = menu.map{|m| m.first}.join(',')
137
+ codes = menu.map{|m| m.last}.join(',')
138
+ names.index('Afghanistan').should == 0
139
+ names.index('Ireland,Iraq,Iceland').should_not be_nil
140
+ codes.index('AFG').should == 0
141
+ codes.index('IRL,IRQ,ISL').should_not be_nil
142
+ end
143
+
144
+ it "should be configuarble to have a selected country at the top" do
145
+ menu = Federation.menu(:top => 'IRL')
146
+ menu.should have(@total).items
147
+ names = menu.map{|m| m.first}.join(',')
148
+ codes = menu.map{|m| m.last}.join(',')
149
+ names.index('Ireland,Afghanistan').should == 0
150
+ names.index('Iraq,Israel').should_not be_nil
151
+ codes.index('IRL,AFG').should == 0
152
+ codes.index('IRQ,ISR').should_not be_nil
153
+ end
154
+
155
+ it "should be configuarble to have 'None' entry at the top" do
156
+ menu = Federation.menu(:none => 'None')
157
+ menu.should have(@total + 1).items
158
+ names = menu.map{|m| m.first}.join(',')
159
+ codes = menu.map{|m| m.last}.join(',')
160
+ names.index('None,Afghanistan').should == 0
161
+ codes.index(',AFG').should == 0
162
+ end
163
+
164
+ it "should be able to handle multiple configuarations" do
165
+ menu = Federation.menu(:top => 'IRL', :order => 'code', :none => 'None')
166
+ menu.should have(@total + 1).items
167
+ names = menu.map{|m| m.first}.join(',')
168
+ codes = menu.map{|m| m.last}.join(',')
169
+ names.index('None,Ireland,Afghanistan').should == 0
170
+ names.index('Iraq,Iceland').should_not be_nil
171
+ codes.index(',IRL,AFG').should == 0
172
+ codes.index('IRQ,ISL').should_not be_nil
114
173
  end
115
174
  end
116
175
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanichi-chess_icu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Orr
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-26 00:00:00 -07:00
12
+ date: 2009-08-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - spec/util_spec.rb
60
60
  has_rdoc: true
61
61
  homepage: http://github.com/sanichi/chess_icu
62
+ licenses:
62
63
  post_install_message:
63
64
  rdoc_options:
64
65
  - --charset=UTF-8
@@ -79,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
80
  requirements: []
80
81
 
81
82
  rubyforge_project:
82
- rubygems_version: 1.2.0
83
+ rubygems_version: 1.3.5
83
84
  signing_key:
84
85
  specification_version: 2
85
86
  summary: For parsing files of chess tournament data into ruby classes.