icu_tournament 1.3.15 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,8 +57,8 @@ module ICU
57
57
  # daffy.rating # => nil
58
58
  # daffy.fide_rating # => 2200
59
59
  #
60
- # ID numbers, on the other hand, are automatically classified as either FIDE or ICU on the basis of size,
61
- # since all FIDE IDs are greater than 100000 whereas ICU IDs are much smaller.
60
+ # ID numbers, on the other hand, are automatically classified as either FIDE or ICU on the basis of size.
61
+ # IDs larger than 100000 are assumed to be FIDE IDs, while smaller numbers are treated as ICU IDs.
62
62
  #
63
63
  # If the ranking numbers are missing from the file or inconsistent (e.g. player A is ranked above player B
64
64
  # but has less points) they are recalculated as a side effect of the parse.
@@ -78,6 +78,8 @@ module ICU
78
78
  # <em>parse_file!</em> is similar except that it raises errors, and the methods <em>parse</em>
79
79
  # and <em>parse!</em> are similar except their inputs are strings rather than file names.
80
80
  #
81
+ # == Serialization
82
+ #
81
83
  # A tournament can be serialized back to Krause format (the reverse of parsing) with the _serialize_ method of the parser.
82
84
  #
83
85
  # krause = parser.serialize(tournament)
@@ -92,24 +94,20 @@ module ICU
92
94
  # krause = parser.serialize(tournament, :fide => true)
93
95
  # krause = tournament.serialize('Krause', :fide => true)
94
96
  #
95
- # The following lists Krause data identification numbers, their description and, where available, their corresponding
96
- # attributes in an ICU::Tournament instance.
97
+ # By default all available information is output for each player, however, this is customizable. The player number,
98
+ # name, total points and results are always output but any of the remaining data (_gender_, _title_, _rating_ or _fide_rating_,
99
+ # _fed_, _id_ or _fide_id_, _dob_ and _rank_) can be omitted, if desired, by specifying an array of columns to include.
100
+ # To omitt all the optional data, supply an empty array:
97
101
  #
98
- # [001 Player record] Use _players_ to get all players or _player_ with a player number to get a single instance.
99
- # [012 Name] Get or set with _name_. Free text. A tounament name is mandatory.
100
- # [013 Teams] Create an ICU::Team, add player numbers to it, use _add_team_ to add to tournament, _get_team_/_teams_ to retrive it/them.
101
- # [022 City] Get or set with _city_. Free text.
102
- # [032 Federation] Get or set with _fed_. Getter returns either _nil_ or a three letter code. Setter can take various formats (see ICU::Federation).
103
- # [042 Start date] Get or set with _start_. Getter returns <em>yyyy-mm-dd</em> format, but setter can use any reasonable date format. Start date is mandadory.
104
- # [052 End date] Get or set with _finish_. Returns either <em>yyyy-mm-dd</em> format or _nil_ if not set. Like _start_, can be set with various date formats.
105
- # [062 Number of players] Not used. Treated as comment in parsed files. Can be determined from the size of the _players_ array.
106
- # [072 Number of rated players] Not used. Treated as comment in parsed files. Can be determined by analysing the array returned by _players_.
107
- # [082 Number of teams] Not used. Treated as comment in parsed files.
108
- # [092 Type of tournament] Get or set with _type_. Free text.
109
- # [102 Arbiter(s)] Get or set with -arbiter_. Free text.
110
- # [112 Deputy(ies)] Get or set with _deputy_. Free text.
111
- # [122 Time control] Get or set with _time_control_. Free text.
112
- # [132 Round dates] Get an array of dates using _round_dates_ or one specific round date by calling _round_date_ with a round number.
102
+ # krause = tournament.serialize('Krause', :columns => [])
103
+ #
104
+ # To omitt, for example, just federation and rating, include all columns but those two:
105
+ #
106
+ # krause = tournament.serialize('Krause', :columns => [:gender, :title, :id, :dob, :rank])
107
+ #
108
+ # To output FIDE IDs and ratings use the _fide_ option in conjunctions with the _id_ and _rating_ columns:
109
+ #
110
+ # krause = tournament.serialize('Krause', :columns => [:id, :rating], :fide => true)
113
111
  #
114
112
  # == Parser Strictness
115
113
  #
@@ -172,6 +170,27 @@ module ICU
172
170
  # 001 1 Mouse,Minerva 1.0 2 2 b 0 0000 - +
173
171
  # 001 2 Mouse,Mickey 1.5 1 1 w 1 0000 - =
174
172
  #
173
+ # == Tournament Attributes
174
+ #
175
+ # The following lists Krause data identification numbers, their description and, where available, their corresponding
176
+ # attributes in an ICU::Tournament instance.
177
+ #
178
+ # [001 Player record] Use _players_ to get all players or _player_ with a player number to get a single instance.
179
+ # [012 Name] Get or set with _name_. Free text. A tounament name is mandatory.
180
+ # [013 Teams] Create an ICU::Team, add player numbers to it, use _add_team_ to add to tournament, _get_team_/_teams_ to retrive it/them.
181
+ # [022 City] Get or set with _city_. Free text.
182
+ # [032 Federation] Get or set with _fed_. Getter returns either _nil_ or a three letter code. Setter can take various formats (see ICU::Federation).
183
+ # [042 Start date] Get or set with _start_. Getter returns <em>yyyy-mm-dd</em> format, but setter can use any reasonable date format. Start date is mandadory.
184
+ # [052 End date] Get or set with _finish_. Returns either <em>yyyy-mm-dd</em> format or _nil_ if not set. Like _start_, can be set with various date formats.
185
+ # [062 Number of players] Not used. Treated as comment in parsed files. Can be determined from the size of the _players_ array.
186
+ # [072 Number of rated players] Not used. Treated as comment in parsed files. Can be determined by analysing the array returned by _players_.
187
+ # [082 Number of teams] Not used. Treated as comment in parsed files.
188
+ # [092 Type of tournament] Get or set with _type_. Free text.
189
+ # [102 Arbiter(s)] Get or set with -arbiter_. Free text.
190
+ # [112 Deputy(ies)] Get or set with _deputy_. Free text.
191
+ # [122 Time control] Get or set with _time_control_. Free text.
192
+ # [132 Round dates] Get an array of dates using _round_dates_ or one specific round date by calling _round_date_ with a round number.
193
+ #
175
194
  class Krause
176
195
  attr_reader :error, :comments
177
196
 
@@ -481,17 +500,40 @@ module ICU
481
500
  class Player
482
501
  # Format a player's 001 record as it would appear in a Krause formatted file (including the final newline).
483
502
  def to_krause(rounds, arg)
503
+ default = [:gender, :title, :rating, :fed, :id, :dob, :rank]
504
+
505
+ # Optional columns.
506
+ optional = arg[:columns] if arg[:columns].instance_of?(Array)
507
+ optional.map!(&:to_s).map!(&:to_sym) if optional
508
+ optional = default.dup unless optional
509
+ optional = optional.inject({}) { |m, a| m[a] = true; m }
510
+
511
+ # Get the values to use.
512
+ val = default.inject({}) do |m, a|
513
+ if optional[a]
514
+ if arg[:fide] && (a == :rating || a == :id)
515
+ m[a] = send("fide_#{a}")
516
+ else
517
+ m[a] = send(a)
518
+ end
519
+ end
520
+ m
521
+ end
522
+
523
+ # Output the mandatory and optional values.
484
524
  krause = '001'
485
525
  krause << sprintf(' %4d', @num)
486
- krause << sprintf(' %1s', case @gender; when 'M' then 'm'; when 'F' then 'w'; else ''; end)
487
- krause << sprintf(' %2s', case @title; when nil then ''; when 'IM' then 'm'; when 'WIM' then 'wm'; else @title[0, @title.length-1].downcase; end)
526
+ krause << sprintf(' %1s', case val[:gender]; when 'M' then 'm'; when 'F' then 'w'; else ''; end)
527
+ krause << sprintf(' %2s', case val[:title]; when nil then ''; when 'IM' then 'm'; when 'WIM' then 'wm'; else val[:title][0, val[:title].length-1].downcase; end)
488
528
  krause << sprintf(' %-33s', "#{@last_name},#{@first_name}")
489
- krause << sprintf(' %4s', arg[:fide] ? @fide_rating : @rating)
490
- krause << sprintf(' %3s', @fed)
491
- krause << sprintf(' %11s', arg[:fide] ? @fide_id : @id)
492
- krause << sprintf(' %10s', @dob)
529
+ krause << sprintf(' %4s', val[:rating])
530
+ krause << sprintf(' %3s', val[:fed])
531
+ krause << sprintf(' %11s', val[:id])
532
+ krause << sprintf(' %10s', val[:dob])
493
533
  krause << sprintf(' %4.1f', points)
494
- krause << sprintf(' %4s', @rank)
534
+ krause << sprintf(' %4s', val[:rank])
535
+
536
+ # And finally the round scores.
495
537
  (1..rounds).each do |r|
496
538
  result = find_result(r)
497
539
  krause << sprintf(' %8s', result ? result.to_krause : '')
@@ -197,8 +197,9 @@ module ICU
197
197
  rounds = t.last_round
198
198
 
199
199
  # Optional columns.
200
- optional = arg[:columns] if arg.instance_of?(Hash) && arg[:columns].instance_of?(Array)
200
+ optional = arg[:columns] if arg[:columns].instance_of?(Array)
201
201
  optional = [:id, :points] unless optional
202
+ optional.map!(&:to_s).map!(&:to_sym)
202
203
 
203
204
  # Columns identifiers in SwissPerfect order.
204
205
  columns = Array.new
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ICU
4
4
  class Tournament
5
- VERSION = "1.3.15"
5
+ VERSION = "1.4.0"
6
6
  end
7
7
  end
@@ -321,6 +321,120 @@ KRAUSE
321
321
  end
322
322
  end
323
323
 
324
+ context "customised serialisation with ICU IDs" do
325
+ before(:all) do
326
+ @k = <<KRAUSE
327
+ 012 Las Vegas National Open
328
+ 042 2008-06-07
329
+ 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
330
+ 001 1 w Ui Laighleis,Gearoidin 1985 IRL 3364 1964-06-10 1.0 2 2 b 0 3 w 1
331
+ 001 2 m Orr,Mark 2258 IRL 1350 1955-11-09 2.0 1 1 w 1 3 b 1
332
+ 001 3 g Svidler,Peter 2663 RUS 16790 1971-01-01 0.0 3 1 b 0 2 w 0
333
+ KRAUSE
334
+ @p = ICU::Tournament::Krause.new
335
+ @t = @p.parse(@k)
336
+ end
337
+
338
+ it "should include all data without any explict cusromisation" do
339
+ text = @t.serialize('Krause')
340
+ text.should match(/001 1 w Ui Laighleis,Gearoidin 1985 IRL 3364 1964-06-10 1.0 2/)
341
+ text.should match(/001 2 m Orr,Mark 2258 IRL 1350 1955-11-09 2.0 1/)
342
+ text.should match(/001 3 g Svidler,Peter 2663 RUS 16790 1971-01-01 0.0 3/)
343
+ end
344
+
345
+ it "should omitt ratings and IDs if FIDE option is chosen" do
346
+ text = @t.serialize('Krause', :fide => true)
347
+ text.should match(/001 1 w Ui Laighleis,Gearoidin IRL 1964-06-10 1.0 2/)
348
+ text.should match(/001 2 m Orr,Mark IRL 1955-11-09 2.0 1/)
349
+ text.should match(/001 3 g Svidler,Peter RUS 1971-01-01 0.0 3/)
350
+ end
351
+
352
+ it "should omitt all optional data if columns option is an empty hash" do
353
+ text = @t.serialize('Krause', :columns => [])
354
+ text.should match(/001 1 Ui Laighleis,Gearoidin 1.0 /)
355
+ text.should match(/001 2 Orr,Mark 2.0 /)
356
+ text.should match(/001 3 Svidler,Peter 0.0 /)
357
+ end
358
+
359
+ it "should should be able to include a subset of attributes, test 1" do
360
+ text = @t.serialize('Krause', :columns => [:gender, "dob", :id, "rubbish"])
361
+ text.should match(/001 1 w Ui Laighleis,Gearoidin 3364 1964-06-10 1.0 /)
362
+ text.should match(/001 2 Orr,Mark 1350 1955-11-09 2.0 /)
363
+ text.should match(/001 3 Svidler,Peter 16790 1971-01-01 0.0 /)
364
+ end
365
+
366
+ it "should should be able to include a subset of attributes, test 2" do
367
+ text = @t.serialize('Krause', :columns => [:rank, "title", :fed, "rating"])
368
+ text.should match(/001 1 Ui Laighleis,Gearoidin 1985 IRL 1.0 2/)
369
+ text.should match(/001 2 m Orr,Mark 2258 IRL 2.0 1/)
370
+ text.should match(/001 3 g Svidler,Peter 2663 RUS 0.0 3/)
371
+ end
372
+
373
+ it "should should be able to include all attributes" do
374
+ text = @t.serialize('Krause', :columns => [:gender, :title, :rating, :fed, :id, :dob, :rank])
375
+ text.should match(/001 1 w Ui Laighleis,Gearoidin 1985 IRL 3364 1964-06-10 1.0 2/)
376
+ text.should match(/001 2 m Orr,Mark 2258 IRL 1350 1955-11-09 2.0 1/)
377
+ text.should match(/001 3 g Svidler,Peter 2663 RUS 16790 1971-01-01 0.0 3/)
378
+ end
379
+ end
380
+
381
+ context "customised serialisation with FIDE IDs" do
382
+ before(:all) do
383
+ @k = <<KRAUSE
384
+ 012 Las Vegas National Open
385
+ 042 2008-06-07
386
+ 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
387
+ 001 1 w Ui Laighleis,Gearoidin 1985 IRL 2501171 1964-06-10 1.0 2 2 b 0 3 w 1
388
+ 001 2 m Orr,Mark 2258 IRL 2500035 1955-11-09 2.0 1 1 w 1 3 b 1
389
+ 001 3 g Svidler,Peter 2663 RUS 4102142 1971-01-01 0.0 3 1 b 0 2 w 0
390
+ KRAUSE
391
+ @p = ICU::Tournament::Krause.new
392
+ @t = @p.parse(@k, :fide => true)
393
+ end
394
+
395
+ it "should include all data without any explict cusromisation" do
396
+ text = @t.serialize('Krause', :fide => true)
397
+ text.should match(/001 1 w Ui Laighleis,Gearoidin 1985 IRL 2501171 1964-06-10 1.0 2/)
398
+ text.should match(/001 2 m Orr,Mark 2258 IRL 2500035 1955-11-09 2.0 1/)
399
+ text.should match(/001 3 g Svidler,Peter 2663 RUS 4102142 1971-01-01 0.0 3/)
400
+ end
401
+
402
+ it "should omitt ratings and IDs if FIDE option is not chosen" do
403
+ text = @t.serialize('Krause')
404
+ text.should match(/001 1 w Ui Laighleis,Gearoidin IRL 1964-06-10 1.0 2/)
405
+ text.should match(/001 2 m Orr,Mark IRL 1955-11-09 2.0 1/)
406
+ text.should match(/001 3 g Svidler,Peter RUS 1971-01-01 0.0 3/)
407
+ end
408
+
409
+ it "should omitt all optional data if columns option is an empty hash" do
410
+ text = @t.serialize('Krause', :columns => [])
411
+ text.should match(/001 1 Ui Laighleis,Gearoidin 1.0 /)
412
+ text.should match(/001 2 Orr,Mark 2.0 /)
413
+ text.should match(/001 3 Svidler,Peter 0.0 /)
414
+ end
415
+
416
+ it "should should be able to include a subset of attributes, test 1" do
417
+ text = @t.serialize('Krause', :columns => [:gender, "dob", :id], :fide => true)
418
+ text.should match(/001 1 w Ui Laighleis,Gearoidin 2501171 1964-06-10 1.0 /)
419
+ text.should match(/001 2 Orr,Mark 2500035 1955-11-09 2.0 /)
420
+ text.should match(/001 3 Svidler,Peter 4102142 1971-01-01 0.0 /)
421
+ end
422
+
423
+ it "should should be able to include a subset of attributes, test 2" do
424
+ text = @t.serialize('Krause', :columns => [:rank, "title", :fed, "rating", :rubbish], :fide => true)
425
+ text.should match(/001 1 Ui Laighleis,Gearoidin 1985 IRL 1.0 2/)
426
+ text.should match(/001 2 m Orr,Mark 2258 IRL 2.0 1/)
427
+ text.should match(/001 3 g Svidler,Peter 2663 RUS 0.0 3/)
428
+ end
429
+
430
+ it "should should be able to include all attributes" do
431
+ text = @t.serialize('Krause', :columns => [:gender, :title, :rating, :fed, :id, :dob, :rank], :fide => true)
432
+ text.should match(/001 1 w Ui Laighleis,Gearoidin 1985 IRL 2501171 1964-06-10 1.0 2/)
433
+ text.should match(/001 2 m Orr,Mark 2258 IRL 2500035 1955-11-09 2.0 1/)
434
+ text.should match(/001 3 g Svidler,Peter 2663 RUS 4102142 1971-01-01 0.0 3/)
435
+ end
436
+ end
437
+
324
438
  context "errors" do
325
439
  before(:each) do
326
440
  @k = <<KRAUSE
@@ -156,9 +156,9 @@ EXPORT
156
156
  @x.should match(/^No\s*Name\s*Loc Id\s*Total\s*1\s*2\s*3\s*/)
157
157
  @x = @t.serialize('SPExport', :columns => [:points, :id, :fed])
158
158
  @x.should match(/^No\s*Name\s*Feder\s*Loc Id\s*Total\s*1\s*2\s*3\s*/)
159
- @x = @t.serialize('SPExport', :columns => [:points, :id, :fed, :fed, :rubbish, :fide_id])
159
+ @x = @t.serialize('SPExport', :columns => [:points, :id, :fed, "fed", :rubbish, :fide_id])
160
160
  @x.should match(/^No\s*Name\s*Feder\s*Intl Id\s*Loc Id\s*Total\s*1\s*2\s*3\s*/)
161
- @x = @t.serialize('SPExport', :columns => [:fed, :fide_id, :points, :id, :rating])
161
+ @x = @t.serialize('SPExport', :columns => [:fed, "fide_id", :points, :id, :rating])
162
162
  @x.should match(/^No\s*Name\s*Feder\s*Intl Id\s*Loc Id\s*Loc\s*Total\s*1\s*2\s*3\s*/)
163
163
  @x = @t.serialize('SPExport', :columns => [:fed, :fide_id, :fide_rating, :points, :id, :rating])
164
164
  @x.should match(/^No\s*Name\s*Feder\s*Intl Id\s*Loc Id\s*Rtg\s*Loc\s*Total\s*1\s*2\s*3\s*/)
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 3
8
- - 15
9
- version: 1.3.15
7
+ - 4
8
+ - 0
9
+ version: 1.4.0
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-04-16 00:00:00 +01:00
17
+ date: 2011-04-22 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency