sanichi-chess_icu 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,4 +1,4 @@
1
1
  * 0.2.0 2009-04-11
2
2
  * Early version that just reads and writes foreign CSV files and has some basic name and date utilities.
3
3
  * 0.3.1 2009-05-04
4
- * Can now read and write Krause files and deals properly with federations.
4
+ * Can now read and write Krause files and deals properly with federations.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 3
4
- :patch: 2
4
+ :patch: 3
data/lib/tournament.rb CHANGED
@@ -182,7 +182,7 @@ raise an exception if the players it references through their tournament numbers
182
182
  # * no two players have the same rank
183
183
  # * the highest rank is 1
184
184
  # * the lowest rank is equal to the total of players
185
- def ranking_consistent?
185
+ def ranking_consistent?(option={})
186
186
  # No two players can have the same rank.
187
187
  ranks = Hash.new
188
188
  @player.values.each do |p|
@@ -192,7 +192,10 @@ raise an exception if the players it references through their tournament numbers
192
192
  end
193
193
  end
194
194
 
195
- # Every player has to have a rank.
195
+ # The special case of complete absence of ranking information is an option.
196
+ return true if ranks.size == 0 && option[:allow_none]
197
+
198
+ # Otherwie, every player has to have a rank.
196
199
  return false unless ranks.size == @player.size
197
200
 
198
201
  # The higest and lowest ranks respectively should be 1 and the number of players.
@@ -223,5 +226,27 @@ raise an exception if the players it references through their tournament numbers
223
226
  v[0].rank = i + 1
224
227
  end
225
228
  end
229
+
230
+ # Raise an exception if a tournament is not valid.
231
+ def validate!
232
+ error = invalid
233
+ raise error if error
234
+ end
235
+
236
+ # Is a tournament invalid? Either returns false (if it's valid) or an error message.
237
+ # Covers all the ways a tournament can be invalid not already enforced by the setters.
238
+ def invalid
239
+ # There must be at least two players.
240
+ return "number of players (#{@player.size}) must be at least 2" if @player.size < 2
241
+
242
+ # Every player must have at least one result.
243
+ @player.each { |num, p| return "player #{num} has no results" if p.results.size == 0 }
244
+
245
+ # The ranking should be consistent, with the proviso that no ranking at all is allowed.
246
+ return "ranking is not consistent" unless ranking_consistent?(:allow_none => true)
247
+
248
+ # If there is a start date and an end date, the start should not come after the end.
249
+ return "start date (#{start}) is after end date (#{finish})" if start && finish && start > finish
250
+ end
226
251
  end
227
252
  end
@@ -137,6 +137,8 @@ A tournament can be serialized back to CSV format (the reverse of parsing) with
137
137
  end
138
138
  raise "line #{@line}: no players found in file" if @tournament.players.size == 0
139
139
 
140
+ @tournament.validate!
141
+
140
142
  @tournament
141
143
  end
142
144
 
@@ -149,8 +149,11 @@ The following lists Krause data identification numbers, their description and, w
149
149
  end
150
150
  end
151
151
 
152
- # Validate the data now that we have everything.
153
- validate
152
+ # Now that we have everything, perform final checks and tidy ups.
153
+ finish_up
154
+
155
+ # Finally, exercise the tournament object's own internal validation.
156
+ @tournament.validate!
154
157
 
155
158
  @tournament
156
159
  end
@@ -237,25 +240,19 @@ The following lists Krause data identification numbers, their description and, w
237
240
  @comments << "\n"
238
241
  end
239
242
 
240
- def validate
241
- # Certain attributes are mandatory.
243
+ def finish_up
244
+ # Certain attributes are mandatory and should have been specifically set.
242
245
  raise "tournament name missing" unless @name_set
243
246
  raise "tournament start date missing" unless @start_set
244
-
245
- # There must be at least two players.
246
- raise "minimum number of players is 2" if @tournament.players.length < 2
247
-
248
- # Every player must have at least one result.
249
- @tournament.players.each { |p| raise "player #{p.num} has no results" if p.results.size == 0 }
250
-
251
- # Rerank the tournament if there are no ranking values or if there are but they're not consistent.
252
- @tournament.rerank unless @tournament.ranking_consistent?
253
247
 
254
248
  # Set the number of rounds.
255
249
  @tournament.rounds = @tournament.players.inject(0) do |pa, p|
256
250
  pm = p.results.inject(0){ |ra, r| ra < r.round ? r.round : ra }
257
251
  pa < pm ? pm : pa
258
252
  end
253
+
254
+ # Rerank the tournament if there are no ranking values or if there are, but they're not consistent.
255
+ @tournament.rerank unless @tournament.ranking_consistent?
259
256
  end
260
257
  end
261
258
  end
@@ -237,6 +237,11 @@ KRAUSE
237
237
  end
238
238
 
239
239
  it "blanking the start date should cause an error" do
240
+ @k.sub!('2008-02-01', '2008-02-04')
241
+ lambda { t = @p.parse!(@k) }.should raise_error(/start.*after.*end/)
242
+ end
243
+
244
+ it "the start cannot be later than the end date" do
240
245
  @k.sub!('2008-02-01', '')
241
246
  lambda { t = @p.parse!(@k) }.should raise_error(/start date missing/)
242
247
  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.3.2
4
+ version: 0.3.3
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-05-04 00:00:00 -07:00
12
+ date: 2009-05-05 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency