icu_tournament 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -56,7 +56,7 @@ module ICU
56
56
  #
57
57
  # ICU::Federation.menu(:none => 'None') # => [['None', ''], ['Afghanistan', 'AFG], ...]
58
58
  #
59
- # The "None" option's code is the empty string and it come above the "top" option if both are specified.
59
+ # The "None" option's code is the empty string and it comes above the "top" option if both are specified.
60
60
  #
61
61
  class Federation
62
62
  attr_reader :code, :name
@@ -68,7 +68,7 @@ module ICU
68
68
  return nil unless str
69
69
  str = str.to_s
70
70
  return nil if str.length < 3
71
- compile unless @@objects
71
+ compile
72
72
  str = str.strip.squeeze(' ').downcase
73
73
  return @@codes[str] if str.length == 3
74
74
  return @@names[str] if @@names[str]
@@ -82,7 +82,7 @@ module ICU
82
82
  end
83
83
 
84
84
  def self.menu(opts = {})
85
- compile unless @@objects;
85
+ compile
86
86
  top, menu = nil, []
87
87
  @@objects.each {|o| opts[:top] == o.code ? top = [o.name, o.code] : menu.push([o.name, o.code]) }
88
88
  opts[:order] == 'code' ? menu.sort!{|a,b| a.last <=> b.last} : menu.sort!{|a,b| a.first <=> b.first}
@@ -90,6 +90,11 @@ module ICU
90
90
  menu.unshift([opts[:none], '']) if opts[:none]
91
91
  menu
92
92
  end
93
+
94
+ def self.codes
95
+ compile
96
+ @@objects.map(&:code).sort
97
+ end
93
98
 
94
99
  def initialize(code, name) # :nodoc: because new is private
95
100
  @code = code
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ICU
4
4
  class Tournament
5
- VERSION = "1.2.2"
5
+ VERSION = "1.2.3"
6
6
  end
7
7
  end
@@ -8,70 +8,70 @@ module ICU
8
8
  fed.code.should == 'IRL'
9
9
  fed.name.should == 'Ireland'
10
10
  end
11
-
11
+
12
12
  it "should find a federation from code case insensitively" do
13
13
  fed = Federation.find('rUs')
14
14
  fed.code.should == 'RUS'
15
15
  fed.name.should == 'Russia'
16
16
  end
17
-
17
+
18
18
  it "should find a federation despite irrelevant whitespace" do
19
19
  fed = Federation.find(' mex ')
20
20
  fed.code.should == 'MEX'
21
21
  fed.name.should == 'Mexico'
22
22
  end
23
-
23
+
24
24
  it "should return nil for an invalid code" do
25
25
  Federation.find('XYZ').should be_nil
26
26
  end
27
27
  end
28
-
28
+
29
29
  context "#find using names" do
30
30
  it "should find a federation given a valid name" do
31
31
  fed = Federation.find('England')
32
32
  fed.code.should == 'ENG'
33
33
  fed.name.should == 'England'
34
34
  end
35
-
35
+
36
36
  it "should find a federation from name case insensitively" do
37
37
  fed = Federation.find('franCE')
38
38
  fed.code.should == 'FRA'
39
39
  fed.name.should == 'France'
40
40
  end
41
-
41
+
42
42
  it "should not be fooled by irrelevant whitespace" do
43
43
  fed = Federation.find(' united states of america ')
44
44
  fed.code.should == 'USA'
45
45
  fed.name.should == 'United States of America'
46
46
  end
47
-
47
+
48
48
  it "should return nil for an invalid name" do
49
49
  Federation.find('Mordor').should be_nil
50
50
  end
51
51
  end
52
-
52
+
53
53
  context "#find using parts of names" do
54
54
  it "should find a federation given a substring which is unique and at least 4 characters" do
55
55
  fed = Federation.find('bosni')
56
56
  fed.code.should == 'BIH'
57
57
  fed.name.should == 'Bosnia and Herzegovina'
58
58
  end
59
-
59
+
60
60
  it "should not be fooled by irrelevant whitespace" do
61
61
  fed = Federation.find(' arab EMIRATES ')
62
62
  fed.code.should == 'UAE'
63
63
  fed.name.should == 'United Arab Emirates'
64
64
  end
65
-
65
+
66
66
  it "should not find a federation if the substring matches more than one" do
67
67
  Federation.find('land').should be_nil
68
68
  end
69
-
69
+
70
70
  it "should return nil for any string smaller in length than 3" do
71
71
  Federation.find('ze').should be_nil
72
72
  end
73
73
  end
74
-
74
+
75
75
  context "#find federations with alternative names" do
76
76
  it "should find Macedonia multiple ways" do
77
77
  Federation.find('MKD').name.should == 'Macedonia'
@@ -83,7 +83,7 @@ module ICU
83
83
  Federation.find('former yugoslav republic').name.should == 'Macedonia'
84
84
  end
85
85
  end
86
-
86
+
87
87
  context "#find with alternative inputs" do
88
88
  it "should behave robustly with completely invalid inputs" do
89
89
  Federation.find().should be_nil
@@ -92,13 +92,13 @@ module ICU
92
92
  Federation.find(1).should be_nil
93
93
  end
94
94
  end
95
-
95
+
96
96
  context "#new is private" do
97
97
  it "#new cannot be called directly" do
98
98
  lambda { Federation.new('IRL', 'Ireland') }.should raise_error(/private method/)
99
99
  end
100
100
  end
101
-
101
+
102
102
  context "documentation examples" do
103
103
  it "should all be correct for valid input" do
104
104
  Federation.find('IRL').name.should == 'Ireland'
@@ -107,18 +107,18 @@ module ICU
107
107
  Federation.find('ongoli').name.should == 'Mongolia'
108
108
  Federation.find(' united states ').code.should == 'USA'
109
109
  end
110
-
110
+
111
111
  it "should return nil for invalid input" do
112
112
  Federation.find('ZYX').should be_nil
113
113
  Federation.find('land').should be_nil
114
114
  end
115
115
  end
116
-
116
+
117
117
  context "#menu" do
118
118
  before(:all) do
119
119
  @total = 173
120
120
  end
121
-
121
+
122
122
  it "should return array of name-code pairs in order of name by default" do
123
123
  menu = Federation.menu
124
124
  menu.should have(@total).items
@@ -129,7 +129,7 @@ module ICU
129
129
  codes.index('AFG').should == 0
130
130
  codes.index('IRQ,IRL,ISR').should_not be_nil
131
131
  end
132
-
132
+
133
133
  it "should be configuarble to order the list by codes" do
134
134
  menu = Federation.menu(:order => "code")
135
135
  menu.should have(@total).items
@@ -140,7 +140,7 @@ module ICU
140
140
  codes.index('AFG').should == 0
141
141
  codes.index('IRL,IRQ,ISL').should_not be_nil
142
142
  end
143
-
143
+
144
144
  it "should be configuarble to have a selected country at the top" do
145
145
  menu = Federation.menu(:top => 'IRL')
146
146
  menu.should have(@total).items
@@ -151,7 +151,7 @@ module ICU
151
151
  codes.index('IRL,AFG').should == 0
152
152
  codes.index('IRQ,ISR').should_not be_nil
153
153
  end
154
-
154
+
155
155
  it "should be configuarble to have 'None' entry at the top" do
156
156
  menu = Federation.menu(:none => 'None')
157
157
  menu.should have(@total + 1).items
@@ -160,7 +160,7 @@ module ICU
160
160
  names.index('None,Afghanistan').should == 0
161
161
  codes.index(',AFG').should == 0
162
162
  end
163
-
163
+
164
164
  it "should be able to handle multiple configuarations" do
165
165
  menu = Federation.menu(:top => 'IRL', :order => 'code', :none => 'None')
166
166
  menu.should have(@total + 1).items
@@ -172,5 +172,20 @@ module ICU
172
172
  codes.index('IRQ,ISL').should_not be_nil
173
173
  end
174
174
  end
175
+
176
+ context "#codes" do
177
+ before(:all) do
178
+ @total = 173
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
+ puts all
186
+ all.index('AFG').should == 0
187
+ all.index('INA,IND,IRI,IRL,IRQ,ISL,ISR,ISV,ITA,IVB').should_not be_nil
188
+ end
189
+ end
175
190
  end
176
191
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 2
9
- version: 1.2.2
8
+ - 3
9
+ version: 1.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mark Orr
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-02 00:00:00 +00:00
17
+ date: 2011-01-09 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency