icu_tournament 1.3.9 → 1.3.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,7 +70,7 @@ module ICU
70
70
  # tournament.serialize('SPExport', :columns => [])
71
71
  #
72
72
  # No Name 1 2 3
73
- #
73
+ #
74
74
  # 1 Griffiths, Ryan-Rhys 4:W 2:W 3:W
75
75
  # 2 Flynn, Jamie 3:W 1:L 4:W
76
76
  # 3 Hulleman, Leon 2:L 4:W 1:L
@@ -107,8 +107,21 @@ module ICU
107
107
  #
108
108
  class SPExport
109
109
  attr_reader :error
110
-
111
- EXPORT_OPTIONS = [:fed, :fide_id, :id, :fide_rating, :rating, :title, :points]
110
+
111
+ COLUMNS =
112
+ [
113
+ [:num, "No"],
114
+ [:name, "Name"],
115
+ [:fed, "Feder"],
116
+ [:fide_id, "Intl Id"],
117
+ [:id, "Loc Id"],
118
+ [:fide_rating, "Rtg"],
119
+ [:rating, "Loc"],
120
+ [:title, "Title"],
121
+ [:points, "Total"],
122
+ ]
123
+ KEY2NAM = COLUMNS.inject({}) { |h,c| h[c.first] = c.last; h }
124
+ NAM2KEY = COLUMNS.inject({}) { |h,c| h[c.last] = c.first; h }
112
125
 
113
126
  # Parse SwissPerfect export data returning a Tournament on success or raising an exception on error.
114
127
  def parse!(spx, arg={})
@@ -182,51 +195,35 @@ module ICU
182
195
  # Ensure a nice set of player numbers and get the number of rounds.
183
196
  t.renumber(:order)
184
197
  rounds = t.last_round
185
-
198
+
186
199
  # Optional columns.
187
200
  optional = arg[:columns] if arg.instance_of?(Hash) && arg[:columns].instance_of?(Array)
188
201
  optional = [:id, :points] unless optional
189
-
202
+
190
203
  # Columns identifiers in SwissPerfect order.
191
204
  columns = Array.new
192
205
  columns.push(:num)
193
206
  columns.push(:name)
194
- EXPORT_OPTIONS.each { |x| columns.push(x) if optional.include?(x) }
195
-
196
- # SwissPerfect headers for each column (other than the rounds, which are treated separately).
197
- header = Hash.new
198
- columns.each do |col|
199
- header[col] = case col
200
- when :num then "No"
201
- when :name then "Name"
202
- when :fed then "Feder"
203
- when :fide_id then "Intl Id"
204
- when :id then "Loc Id"
205
- when :fide_rating then "Rtg"
206
- when :rating then "Loc"
207
- when :title then "Title"
208
- when :points then "Total"
209
- end
210
- end
211
-
207
+ COLUMNS.map(&:first).each { |x| columns.push(x) if optional.include?(x) && x != :num && x != :name }
208
+
212
209
  # Widths and formats for each column.
213
210
  width = Hash.new
214
211
  format = Hash.new
215
212
  columns.each do |col|
216
- width[col] = t.players.inject(header[col].length) { |l, p| p.send(col).to_s.length > l ? p.send(col).to_s.length : l }
213
+ width[col] = t.players.inject(KEY2NAM[col].length) { |l, p| p.send(col).to_s.length > l ? p.send(col).to_s.length : l }
217
214
  format[col] = "%-#{width[col]}s"
218
215
  end
219
216
 
220
217
  # The header, followed by a blank line.
221
218
  formats = columns.map{ |col| format[col] }
222
219
  (1..rounds).each { |r| formats << "%#{width[:num]}d " % r }
223
- sp = formats.join("\t") % columns.map{ |col| header[col] }
220
+ sp = formats.join("\t") % columns.map{ |col| KEY2NAM[col] }
224
221
  sp << "\r\n\r\n"
225
222
 
226
223
  # The round formats for players are slightly different to those for the header.
227
224
  formats.pop(rounds)
228
225
  (1..rounds).each{ |r| formats << "%#{2+width[:num]}s" }
229
-
226
+
230
227
  # Serialize the formats already.
231
228
  formats = formats.join("\t") + "\r\n"
232
229
 
@@ -258,21 +255,11 @@ module ICU
258
255
  @header = Hash.new
259
256
  @rounds = 1
260
257
  items.each_with_index do |item, i|
261
- key = case item
262
- when 'No' then :num
263
- when 'Name' then :name
264
- when 'Feder' then :fed
265
- when 'Intl Id' then :fide_id
266
- when 'Loc Id' then :id
267
- when 'Rtg' then :fide_rating
268
- when 'Loc' then :rating
269
- when 'Title' then :title
270
- when 'Total' then :points
271
- when /^[1-9]\d*$/
272
- round = item.to_i
273
- @rounds = round if round > @rounds
274
- round
275
- else nil
258
+ if item.match(/^[1-9]\d*$/)
259
+ key = item.to_i
260
+ @rounds = key if key > @rounds
261
+ else
262
+ key = NAM2KEY[item]
276
263
  end
277
264
  @header[key] = i if key
278
265
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ICU
4
4
  class Tournament
5
- VERSION = "1.3.9"
5
+ VERSION = "1.3.10"
6
6
  end
7
7
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 3
8
- - 9
9
- version: 1.3.9
8
+ - 10
9
+ version: 1.3.10
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mark Orr