icu_utils 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZmI4OGJlNjg3MjJkZjEwNDFlMmMyZTViNmExY2RmYTczN2Q3YWI0Yw==
5
+ data.tar.gz: !binary |-
6
+ YzUzYTA0ZDlhN2IxZGVmYWZlNjc1NjQ1NTNkMjFjMTI1NDVhOTk4ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmQ1Y2EyNzFlZWZiZjNiODhjMDM2Nzc4NWEyOTFlYjY3OGU4NTZjYjgwYWNi
10
+ MmRmNTI1ZTYwOTNhMDg2YmFkMzlhZDc2MWI5MWU3NWFmYzdmMzQ5M2EzMDdm
11
+ Njg5YjcwMDRkOThkMTFjNTZiNjM2MTZkOTEwODU4MzI3ODA5NTU=
12
+ data.tar.gz: !binary |-
13
+ NmFlZTU2YjNmZWM4NzM2YWE2ODJkNDM5Yzc5MWYzZjVlM2MwNTA1MDcwMThi
14
+ MDk1MDgxZTNlMTliOGYzZThlNWM4OWQxMzM2NTlhNDZjM2RkZTZhY2FmMzFl
15
+ ODM2ZGE4YTJjOGE3ZjA2NTI1MGFiNjcwZGEwMjQ0MWFiNWQxMGM=
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ doc/
6
+ pkg
7
+ rdoc
8
+ tmp
9
+ NOTES
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format nested
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ icu_utils (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.4)
10
+ rake (10.1.0)
11
+ rdoc (3.9.5)
12
+ rspec (2.14.1)
13
+ rspec-core (~> 2.14.0)
14
+ rspec-expectations (~> 2.14.0)
15
+ rspec-mocks (~> 2.14.0)
16
+ rspec-core (2.14.7)
17
+ rspec-expectations (2.14.3)
18
+ diff-lcs (>= 1.1.3, < 2.0)
19
+ rspec-mocks (2.14.4)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.3)
26
+ icu_utils!
27
+ rake
28
+ rdoc
29
+ rspec
data/Icon.png ADDED
Binary file
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mark Orr
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # IcuUtils
2
+
3
+ A place for utilities shared across the various ICU apps and gems.
4
+
5
+ So far just includes a class to handle FIDE federations.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'icu_utils'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install icu_utils
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/autospec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'autospec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'autospec')
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
data/bin/ri ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ri' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rdoc', 'ri')
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
data/icu_utils.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'icu_utils/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "icu_utils"
8
+ s.version = IcuUtils::VERSION
9
+ s.authors = ["Mark Orr"]
10
+ s.email = ["mark.j.l.orr@googlemail.com"]
11
+ s.description = %q{A place for shared utilities for sharing between the various ICU apps and gems}
12
+ s.summary = %q{Shared ICU utilities}
13
+ s.homepage = "http://github.com/sanichi/icu_utils"
14
+ s.license = "MIT"
15
+
16
+ s.extra_rdoc_files = %w(LICENSE.txt README.md)
17
+ s.files = `git ls-files`.split($/)
18
+ s.test_files = s.files.grep(%r{^(spec)/})
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "bundler", "~> 1.3"
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "rdoc"
25
+ end
@@ -0,0 +1,309 @@
1
+ module ICU
2
+ #
3
+ # This class can be used to map a string into an object representing a chess federation.
4
+ # In FIDE, chess federations are generally either referred to by their full names such as
5
+ # _Ireland_ or _Russia_ or by three letter codes such as _IRL_ or _RUS_. The three letter
6
+ # codes are mostly the same as those found in the international standard known as
7
+ # {ISO 3166-1 alpha-3}[http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3], but with
8
+ # some differences (e.g. for England, Scotland and Wales).
9
+ #
10
+ # You cannot directly create instances of this class using _new_. Instead, you supply
11
+ # a string to the class method _find_ and, if the string supplied uniguely identifies a
12
+ # federation, an instance is returned which responds to _name_ and _code_.
13
+ #
14
+ # fed = ICU::Federation.find('IRL')
15
+ # fed.name # => "Ireland"
16
+ # fed.code # => "IRL"
17
+ #
18
+ # If the string is not sufficient to identify a federation, the _find_ method returns _nil_.
19
+ #
20
+ # fed = ICU::Federation.find('ZYX') # => nil
21
+ #
22
+ # If the string is three letters long and matches (case insenstively) one of the unique
23
+ # federation codes, then the instance corresponding to that federation is returned.
24
+ #
25
+ # ICU::Federation.find('rUs').code # => "RUS"
26
+ #
27
+ # If the string is more than three letters long and if it is a substring (case insensitive)
28
+ # of exactly one federation name, then that federation is returned.
29
+ #
30
+ # ICU::Federation.find('ongoli').name # => "Mongolia"
31
+ #
32
+ # In all other cases, nil is returned. In the following example, the string matches more than one federation.
33
+ #
34
+ # ICU::Federation.find('land') # => nil
35
+ #
36
+ # The method is not fooled by irrelevant white space.
37
+ #
38
+ # ICU::Federation.find(' united states ').code # => 'USA'
39
+ #
40
+ # The class method _menu_ will return an array of two-element arrays each of which contain a name
41
+ # and a code.
42
+ #
43
+ # ICU::Federation.menu # => [['Afghanistan', 'AFG'], ['Albania', 'ALB], ...]
44
+ #
45
+ # Such an array could be used, for example, as the basis of a selection menu in a web application.
46
+ # Various options are available to alter the array returned. Use the _:order_ option to order by code
47
+ # instead of the default (by country name).
48
+ #
49
+ # ICU::Federation.menu(:order => 'code') # => [..., ['Ireland', 'IRL'], ['Iraq', 'IRQ], ...]
50
+ #
51
+ # To put one country at the top (followed by the rest, in order) supply the country's code with the _:top_ option:
52
+ #
53
+ # ICU::Federation.menu(:top => 'IRL') # => [['Ireland', 'IRL'], ['Afghanistan', 'AFG], ...]
54
+ #
55
+ # To supply an extra "None" item at the top, specify its label with the _:none_ option:
56
+ #
57
+ # ICU::Federation.menu(:none => 'None') # => [['None', ''], ['Afghanistan', 'AFG], ...]
58
+ #
59
+ # The "None" option's code is the empty string and it comes above the "top" option if both are specified.
60
+ #
61
+ class Federation
62
+ attr_reader :code, :name
63
+ private_class_method :new
64
+
65
+ # Given a code, name or part of a name, return the corresponding federation instance.
66
+ # If there is no match or more than one match, _nil_ is returned.
67
+ def self.find(str=nil)
68
+ return nil unless str
69
+ str = str.to_s
70
+ return nil if str.length < 3
71
+ compile
72
+ str = str.strip.squeeze(' ').downcase
73
+ return @@codes[str] if str.length == 3
74
+ return @@names[str] if @@names[str]
75
+ matches = Array.new
76
+ @@names.each_key do |name|
77
+ matches << @@names[name] if name.index(str)
78
+ end
79
+ matches.uniq!
80
+ return nil unless matches.length == 1
81
+ matches[0]
82
+ end
83
+
84
+ # Return an array of codes and names suitable for creating a federation menu in Rails.
85
+ # ICU::Federation.menu(:order => 'code') # order federations by code (instead of by name)
86
+ # ICU::Federation.menu(:top => 'IRL') # make this federation come first
87
+ # ICU::Federation.menu(:none => 'None') # add a dummy top entry with name "None" and blank code
88
+ def self.menu(opts = {})
89
+ compile
90
+ top, menu = nil, []
91
+ @@objects.each {|o| opts[:top] == o.code ? top = [o.name, o.code] : menu.push([o.name, o.code]) }
92
+ opts[:order] == 'code' ? menu.sort!{|a,b| a.last <=> b.last} : menu.sort!{|a,b| a.first <=> b.first}
93
+ menu.unshift(top) if top
94
+ menu.unshift([opts[:none], '']) if opts[:none]
95
+ menu
96
+ end
97
+
98
+ # Return an array of sorted federation codes.
99
+ def self.codes
100
+ compile
101
+ @@objects.map(&:code).sort
102
+ end
103
+
104
+ def initialize(code, name) # :nodoc: because new is private
105
+ @code = code
106
+ @name = name
107
+ end
108
+
109
+ # :enddoc:
110
+ private
111
+
112
+ def self.compile
113
+ return if @@objects
114
+ @@names = Hash.new
115
+ @@codes = Hash.new
116
+ @@objects = Array.new
117
+ @@data.each do |d|
118
+ object = new(d[0], d[1])
119
+ @@objects << object
120
+ @@codes[d[0].downcase] = object
121
+ (1..d.length-1).each do |i|
122
+ @@names[d[i].downcase] = object
123
+ end
124
+ end
125
+ end
126
+
127
+ # The data structures compiled.
128
+ @@objects, @@codes, @@names = nil, nil, nil
129
+
130
+ # An array of data that gets compiled into other data structures.
131
+ @@data =
132
+ [
133
+ ['AFG', 'Afghanistan'],
134
+ ['ALB', 'Albania'],
135
+ ['ALG', 'Algeria'],
136
+ ['AND', 'Andorra'],
137
+ ['ANG', 'Angola'],
138
+ ['ANT', 'Antigua'],
139
+ ['ARG', 'Argentina'],
140
+ ['ARM', 'Armenia'],
141
+ ['ARU', 'Aruba'],
142
+ ['AUS', 'Australia'],
143
+ ['AUT', 'Austria'],
144
+ ['AZE', 'Azerbaijan'],
145
+ ['BAH', 'Bahamas'],
146
+ ['BRN', 'Bahrain'],
147
+ ['BAN', 'Bangladesh'],
148
+ ['BAR', 'Barbados'],
149
+ ['BLR', 'Belarus'],
150
+ ['BEL', 'Belgium'],
151
+ ['BIZ', 'Belize'],
152
+ ['BEN', 'Benin Republic'],
153
+ ['BER', 'Bermuda'],
154
+ ['BHU', 'Bhutan'],
155
+ ['BOL', 'Bolivia'],
156
+ ['BIH', 'Bosnia and Herzegovina'],
157
+ ['BOT', 'Botswana'],
158
+ ['BRA', 'Brazil'],
159
+ ['IVB', 'British Virgin Islands'],
160
+ ['BRU', 'Brunei Darussalam'],
161
+ ['BUL', 'Bulgaria'],
162
+ ['BUR', 'Burkina Faso'],
163
+ ['BDI', 'Burundi'],
164
+ ['CAM', 'Cambodia'],
165
+ ['CMR', 'Cameroon'],
166
+ ['CAN', 'Canada'],
167
+ ['CHA', 'Chad'],
168
+ ['CHI', 'Chile'],
169
+ ['CHN', 'China'],
170
+ ['TPE', 'Chinese Taipei'],
171
+ ['COL', 'Colombia'],
172
+ ['CRC', 'Costa Rica'],
173
+ ['CRO', 'Croatia'],
174
+ ['CUB', 'Cuba'],
175
+ ['CYP', 'Cyprus'],
176
+ ['CZE', 'Czech Republic'],
177
+ ['DEN', 'Denmark'],
178
+ ['DJI', 'Djibouti'],
179
+ ['DOM', 'Dominican Republic'],
180
+ ['ECU', 'Ecuador'],
181
+ ['EGY', 'Egypt'],
182
+ ['ESA', 'El Salvador'],
183
+ ['ENG', 'England'],
184
+ ['EST', 'Estonia'],
185
+ ['ETH', 'Ethiopia'],
186
+ ['FAI', 'Faroe Islands'],
187
+ ['FIJ', 'Fiji'],
188
+ ['FIN', 'Finland'],
189
+ ['FRA', 'France'],
190
+ ['GAB', 'Gabon'],
191
+ ['GAM', 'Gambia'],
192
+ ['GEO', 'Georgia'],
193
+ ['GER', 'Germany'],
194
+ ['GHA', 'Ghana'],
195
+ ['GRE', 'Greece'],
196
+ ['GUA', 'Guatemala'],
197
+ ['GCI', 'Guernsey'],
198
+ ['GUY', 'Guyana'],
199
+ ['HAI', 'Haiti'],
200
+ ['HON', 'Honduras'],
201
+ ['HKG', 'Hong Kong'],
202
+ ['HUN', 'Hungary'],
203
+ ['ISL', 'Iceland'],
204
+ ['IND', 'India'],
205
+ ['INA', 'Indonesia'],
206
+ ['IRI', 'Iran'],
207
+ ['IRQ', 'Iraq'],
208
+ ['IRL', 'Ireland'],
209
+ ['ISR', 'Israel'],
210
+ ['ITA', 'Italy'],
211
+ ['CIV', 'Ivory Coast'],
212
+ ['JAM', 'Jamaica'],
213
+ ['JPN', 'Japan'],
214
+ ['JCI', 'Jersey'],
215
+ ['JOR', 'Jordan'],
216
+ ['KAZ', 'Kazakhstan'],
217
+ ['KEN', 'Kenya'],
218
+ ['KUW', 'Kuwait'],
219
+ ['KGZ', 'Kyrgyzstan'],
220
+ ['LAT', 'Latvia'],
221
+ ['LIB', 'Lebanon'],
222
+ ['LBA', 'Libya'],
223
+ ['LIE', 'Liechtenstein'],
224
+ ['LTU', 'Lithuania'],
225
+ ['LUX', 'Luxembourg'],
226
+ ['MAC', 'Macau'],
227
+ ['MKD', 'Macedonia', 'Former YUG Rep of Macedonia', 'Former Yugoslav Republic of Macedonia', 'FYROM'],
228
+ ['MAD', 'Madagascar'],
229
+ ['MAW', 'Malawi'],
230
+ ['MAS', 'Malaysia'],
231
+ ['MDV', 'Maldives'],
232
+ ['MLI', 'Mali'],
233
+ ['MLT', 'Malta'],
234
+ ['MAU', 'Mauritania'],
235
+ ['MRI', 'Mauritius'],
236
+ ['MEX', 'Mexico'],
237
+ ['MDA', 'Moldova'],
238
+ ['MNC', 'Monaco'],
239
+ ['MGL', 'Mongolia'],
240
+ ['MNE', 'Montenegro'],
241
+ ['MAR', 'Morocco'],
242
+ ['MOZ', 'Mozambique'],
243
+ ['MYA', 'Myanmar'],
244
+ ['NAM', 'Namibia'],
245
+ ['NEP', 'Nepal'],
246
+ ['NED', 'Netherlands'],
247
+ ['AHO', 'Netherlands Antilles'],
248
+ ['NZL', 'New Zealand'],
249
+ ['NCA', 'Nicaragua'],
250
+ ['NGR', 'Nigeria'],
251
+ ['NOR', 'Norway'],
252
+ ['PAK', 'Pakistan'],
253
+ ['PLW', 'Palau'],
254
+ ['PLE', 'Palestine'],
255
+ ['PAN', 'Panama'],
256
+ ['PNG', 'Papua New Guinea'],
257
+ ['PAR', 'Paraguay'],
258
+ ['PER', 'Peru'],
259
+ ['PHI', 'Philippines'],
260
+ ['POL', 'Poland'],
261
+ ['POR', 'Portugal'],
262
+ ['PUR', 'Puerto Rico'],
263
+ ['QAT', 'Qatar'],
264
+ ['ROU', 'Romania'],
265
+ ['RUS', 'Russia'],
266
+ ['RWA', 'Rwanda'],
267
+ ['SMR', 'San Marino'],
268
+ ['STP', 'Sao Tome and Principe'],
269
+ ['SCO', 'Scotland'],
270
+ ['SEN', 'Senegal'],
271
+ ['SRB', 'Serbia'],
272
+ ['SEY', 'Seychelles'],
273
+ ['SLE', 'Siera Leone'],
274
+ ['SIN', 'Singapore'],
275
+ ['SVK', 'Slovakia'],
276
+ ['SLO', 'Slovenia'],
277
+ ['SOM', 'Somalia'],
278
+ ['RSA', 'South Africa'],
279
+ ['KOR', 'South Korea'],
280
+ ['ESP', 'Spain'],
281
+ ['SRI', 'Sri Lanka'],
282
+ ['SUD', 'Sudan'],
283
+ ['SUR', 'Surinam'],
284
+ ['SWE', 'Sweden'],
285
+ ['SUI', 'Switzerland'],
286
+ ['SYR', 'Syria'],
287
+ ['TJK', 'Tajikistan'],
288
+ ['TAN', 'Tanzania'],
289
+ ['THA', 'Thailand'],
290
+ ['TRI', 'Trinidad and Tobago'],
291
+ ['TUN', 'Tunisia'],
292
+ ['TUR', 'Turkey'],
293
+ ['TKM', 'Turkmenistan'],
294
+ ['UGA', 'Uganda'],
295
+ ['UKR', 'Ukraine'],
296
+ ['UAE', 'United Arab Emirates'],
297
+ ['USA', 'United States of America'],
298
+ ['URU', 'Uruguay'],
299
+ ['ISV', 'US Virgin Islands'],
300
+ ['UZB', 'Uzbekistan'],
301
+ ['VEN', 'Venezuela'],
302
+ ['VIE', 'Vietnam'],
303
+ ['WLS', 'Wales'],
304
+ ['YEM', 'Yemen'],
305
+ ['ZAM', 'Zambia'],
306
+ ['ZIM', 'Zimbabwe'],
307
+ ]
308
+ end
309
+ end
@@ -0,0 +1,3 @@
1
+ module IcuUtils
2
+ VERSION = "1.0.0"
3
+ end
data/lib/icu_utils.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'icu_utils/version'
2
+ require 'icu_utils/federation'
@@ -0,0 +1,190 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module ICU
4
+ describe Federation do
5
+ context "#find using codes" do
6
+ it "should find a federation given a valid code" do
7
+ fed = Federation.find('IRL')
8
+ fed.code.should == 'IRL'
9
+ fed.name.should == 'Ireland'
10
+ end
11
+
12
+ it "should find a federation from code case insensitively" do
13
+ fed = Federation.find('rUs')
14
+ fed.code.should == 'RUS'
15
+ fed.name.should == 'Russia'
16
+ end
17
+
18
+ it "should find a federation despite irrelevant whitespace" do
19
+ fed = Federation.find(' mex ')
20
+ fed.code.should == 'MEX'
21
+ fed.name.should == 'Mexico'
22
+ end
23
+
24
+ it "should return nil for an invalid code" do
25
+ Federation.find('XYZ').should be_nil
26
+ end
27
+ end
28
+
29
+ context "#find using names" do
30
+ it "should find a federation given a valid name" do
31
+ fed = Federation.find('England')
32
+ fed.code.should == 'ENG'
33
+ fed.name.should == 'England'
34
+ end
35
+
36
+ it "should find a federation from name case insensitively" do
37
+ fed = Federation.find('franCE')
38
+ fed.code.should == 'FRA'
39
+ fed.name.should == 'France'
40
+ end
41
+
42
+ it "should not be fooled by irrelevant whitespace" do
43
+ fed = Federation.find(' united states of america ')
44
+ fed.code.should == 'USA'
45
+ fed.name.should == 'United States of America'
46
+ end
47
+
48
+ it "should return nil for an invalid name" do
49
+ Federation.find('Mordor').should be_nil
50
+ end
51
+ end
52
+
53
+ context "#find using parts of names" do
54
+ it "should find a federation given a substring which is unique and at least 4 characters" do
55
+ fed = Federation.find('bosni')
56
+ fed.code.should == 'BIH'
57
+ fed.name.should == 'Bosnia and Herzegovina'
58
+ end
59
+
60
+ it "should not be fooled by irrelevant whitespace" do
61
+ fed = Federation.find(' arab EMIRATES ')
62
+ fed.code.should == 'UAE'
63
+ fed.name.should == 'United Arab Emirates'
64
+ end
65
+
66
+ it "should not find a federation if the substring matches more than one" do
67
+ Federation.find('land').should be_nil
68
+ end
69
+
70
+ it "should return nil for any string smaller in length than 3" do
71
+ Federation.find('ze').should be_nil
72
+ end
73
+ end
74
+
75
+ context "#find federations with alternative names" do
76
+ it "should find Macedonia multiple ways" do
77
+ Federation.find('MKD').name.should == 'Macedonia'
78
+ Federation.find('FYROM').name.should == 'Macedonia'
79
+ Federation.find('macedoni').name.should == 'Macedonia'
80
+ Federation.find('Macedonia').name.should == 'Macedonia'
81
+ Federation.find('former YUG Rep').name.should == 'Macedonia'
82
+ Federation.find('Republic of Macedonia').name.should == 'Macedonia'
83
+ Federation.find('former yugoslav republic').name.should == 'Macedonia'
84
+ end
85
+ end
86
+
87
+ context "#find with alternative inputs" do
88
+ it "should behave robustly with completely invalid inputs" do
89
+ Federation.find().should be_nil
90
+ Federation.find(nil).should be_nil
91
+ Federation.find('').should be_nil
92
+ Federation.find(1).should be_nil
93
+ end
94
+ end
95
+
96
+ context "#new is private" do
97
+ it "#new cannot be called directly" do
98
+ lambda { Federation.new('IRL', 'Ireland') }.should raise_error(/private method/)
99
+ end
100
+ end
101
+
102
+ context "documentation examples" do
103
+ it "should all be correct for valid input" do
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
+ end
110
+
111
+ it "should return nil for invalid input" do
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 = 174
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
173
+ end
174
+ end
175
+
176
+ context "#codes" do
177
+ before(:all) do
178
+ @total = 174
179
+ end
180
+
181
+ it "should return array of codes ordered alphabetically" do
182
+ codes = Federation.codes
183
+ codes.should have(@total).items
184
+ all = codes.join(',')
185
+ all.index('AFG').should == 0
186
+ all.index('INA,IND,IRI,IRL,IRQ,ISL,ISR,ISV,ITA,IVB').should_not be_nil
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/icu_utils')
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: icu_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Orr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A place for shared utilities for sharing between the various ICU apps
70
+ and gems
71
+ email:
72
+ - mark.j.l.orr@googlemail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files:
76
+ - LICENSE.txt
77
+ - README.md
78
+ files:
79
+ - .gitignore
80
+ - .rspec
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - Icon.png
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/autospec
88
+ - bin/rake
89
+ - bin/ri
90
+ - bin/rspec
91
+ - icu_utils.gemspec
92
+ - lib/icu_utils.rb
93
+ - lib/icu_utils/federation.rb
94
+ - lib/icu_utils/version.rb
95
+ - spec/federation_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: http://github.com/sanichi/icu_utils
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.1.10
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Shared ICU utilities
121
+ test_files:
122
+ - spec/federation_spec.rb
123
+ - spec/spec_helper.rb