icu_tournament 1.0.12 → 1.0.13
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/icu_tournament/tournament.rb +28 -0
- data/lib/icu_tournament/version.rb +1 -1
- data/spec/tournament_spec.rb +54 -0
- metadata +3 -3
@@ -126,6 +126,22 @@ argument to specify how the renumbering is done.
|
|
126
126
|
|
127
127
|
The return value from _renumber_ is the tournament object itself.
|
128
128
|
|
129
|
+
|
130
|
+
== Parsing Files
|
131
|
+
|
132
|
+
As an alternative to processing files by first instantiating a parser of the appropropriate class
|
133
|
+
(such as ICU::Tournament::SwissPerfect, ICU::Tournament::Krause and ICU::Tournament::ForeignCSV)
|
134
|
+
and then calling the parser's <em>parse_file</em> or <em>parse_file!</em> instance method,
|
135
|
+
a convenience class method, <em>parse_file!</em>, is available when a parser instance is not required.
|
136
|
+
For example:
|
137
|
+
|
138
|
+
t = ICU::Tournament.parse_file!('champs.zip', 'SwissPerfect', :start => '2010-07-03')
|
139
|
+
|
140
|
+
The method takes a filename, format and an options hash as arguments. It either returns
|
141
|
+
an instance of ICU::Tournament or throws an exception. See the documentation for the
|
142
|
+
different formats for what options are available. For some, no options are available,
|
143
|
+
in which case any options supplied to this method will be silently ignored.
|
144
|
+
|
129
145
|
=end
|
130
146
|
|
131
147
|
class Tournament
|
@@ -356,6 +372,18 @@ The return value from _renumber_ is the tournament object itself.
|
|
356
372
|
check_ranks(:allow_none => true)
|
357
373
|
true
|
358
374
|
end
|
375
|
+
|
376
|
+
# Convenience method to parse a file.
|
377
|
+
def self.parse_file!(file, format, opts={})
|
378
|
+
type = format.to_s
|
379
|
+
raise "Invalid format" unless type.match(/^(SwissPerfect|Krause|ForeignCSV)$/);
|
380
|
+
parser = "ICU::Tournament::#{format}".constantize.new
|
381
|
+
if type == 'SwissPerfect'
|
382
|
+
parser.parse_file!(file, opts)
|
383
|
+
else
|
384
|
+
parser.parse_file!(file)
|
385
|
+
end
|
386
|
+
end
|
359
387
|
|
360
388
|
# Convenience method to serialise the tournament into a supported format.
|
361
389
|
# Throws and exception unless the name of a supported format is supplied (e.g. _Krause_).
|
data/spec/tournament_spec.rb
CHANGED
@@ -799,5 +799,59 @@ EOS
|
|
799
799
|
@t.player(6).rank.should == 6 # 0/5
|
800
800
|
end
|
801
801
|
end
|
802
|
+
|
803
|
+
context "convenience file parser" do
|
804
|
+
before(:all) do
|
805
|
+
@s = File.dirname(__FILE__) + '/samples'
|
806
|
+
@c = ICU::Tournament
|
807
|
+
end
|
808
|
+
|
809
|
+
it "should parse a valid SwissPerfect file" do
|
810
|
+
t = nil
|
811
|
+
lambda { t = @c.parse_file!("#{@s}/sp/nccz.zip", 'SwissPerfect', :start => '2010-05-08') }.should_not raise_error
|
812
|
+
t.players.size.should == 77
|
813
|
+
t.start.should == '2010-05-08'
|
814
|
+
end
|
815
|
+
|
816
|
+
it "should parse a valid CSV file" do
|
817
|
+
t = nil
|
818
|
+
lambda { t = @c.parse_file!("#{@s}/fcsv/valid.csv", 'ForeignCSV') }.should_not raise_error
|
819
|
+
t.players.size.should == 16
|
820
|
+
end
|
821
|
+
|
822
|
+
it "should parse a valid Krause file" do
|
823
|
+
t = nil
|
824
|
+
lambda { t = @c.parse_file!("#{@s}/krause/valid.tab", 'Krause') }.should_not raise_error
|
825
|
+
t.players.size.should == 12
|
826
|
+
end
|
827
|
+
|
828
|
+
it "should ignore options where appropriate" do
|
829
|
+
t = nil
|
830
|
+
lambda { t = @c.parse_file!("#{@s}/krause/valid.tab", 'Krause', :start => '2010-05-08') }.should_not raise_error
|
831
|
+
t.start.should == '2008-02-01'
|
832
|
+
end
|
833
|
+
|
834
|
+
it "should raise exceptions for invalid files" do
|
835
|
+
lambda { @c.parse_file!("#{@s}/sp/notenoughfiles.zip", 'SwissPerfect', :start => '2010-05-08') }.should raise_error(/files/)
|
836
|
+
lambda { @c.parse_file!("#{@s}/krause/invalid.tab", 'Krause') }.should raise_error(/name/)
|
837
|
+
lambda { @c.parse_file!("#{@s}/fcsv/invalid.csv", 'ForeignCSV') }.should raise_error(/termination/)
|
838
|
+
end
|
839
|
+
|
840
|
+
it "should raise exceptions if the wrong type is used" do
|
841
|
+
lambda { @c.parse_file!("#{@s}/krause/valid.tab", 'ForeignCSV') }.should raise_error(/expected/)
|
842
|
+
lambda { @c.parse_file!("#{@s}/fcsv/valid.csv", 'SwissPerfect') }.should raise_error(/cannot/)
|
843
|
+
lambda { @c.parse_file!("#{@s}/sp/nccz.zip", 'Krause') }.should raise_error(/missing/)
|
844
|
+
end
|
845
|
+
|
846
|
+
it "should raise an exception if file does not exist" do
|
847
|
+
lambda { @c.parse_file!("#{@s}/nosuchfile.cvs", 'ForeignCSV') }.should raise_error(/no such file/i)
|
848
|
+
lambda { @c.parse_file!("#{@s}/nosuchfile.zip", 'SwissPerfect') }.should raise_error(/invalid/i)
|
849
|
+
lambda { @c.parse_file!("#{@s}/nosuchfile.tab", 'Krause') }.should raise_error(/no such file/i)
|
850
|
+
end
|
851
|
+
|
852
|
+
it "should raise an exception if an invalid type is used" do
|
853
|
+
lambda { @c.parse_file!("#{@s}/krause/valid.tab", 'NoSuchType') }.should raise_error(/invalid format/i)
|
854
|
+
end
|
855
|
+
end
|
802
856
|
end
|
803
857
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
8
|
+
- 13
|
9
|
+
version: 1.0.13
|
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: 2010-10-
|
17
|
+
date: 2010-10-16 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|