sportdb-formats 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f298701705843da74864b5b202122696b0c315f
4
- data.tar.gz: 776194a60adc13e2397ff8d63ec640cb519f6022
3
+ metadata.gz: 10455b8b9b5c3220b15a503f299a5ca50bcf22a7
4
+ data.tar.gz: 4abaa2dd8badbc94de069ae3fffe0268021ab6c1
5
5
  SHA512:
6
- metadata.gz: 8d77c3ecceedd959a21d129eebb5c456aaa203c885813a97520920bd8273859723f3c74091bfe1ecfcc6e5f678b33db9c4a70fd8afcfb6c48e11e4420ed307c3
7
- data.tar.gz: f36282877eea8646e123a0ba9b5bde25e55dc428a50c69bfc3884c9e2b437fc3e95f635a85b44fa95a47013d3027bc21ff1c8de7bf9ba35b0aae4cbdaf3ec3ed
6
+ metadata.gz: 4f456deb7833358bbf748a50c37da56cf546736f345e7c57db88ac14aab1ff8ebd01a1c2c0c7f52cc58be3d1da666663009fb481a04bee1059f7e10913a8eafe
7
+ data.tar.gz: 30a07f60a25daf252a5c0cc6cc0862640790dc6cef93defb28f3c48425e1ee20d3525f9c6b3037559c7abbe13d638caba3d7135e53b99c7065e2546c413c7a65
data/Manifest.txt CHANGED
@@ -5,8 +5,11 @@ Rakefile
5
5
  lib/sportdb/formats.rb
6
6
  lib/sportdb/formats/datafile.rb
7
7
  lib/sportdb/formats/outline_reader.rb
8
+ lib/sportdb/formats/season_utils.rb
8
9
  lib/sportdb/formats/version.rb
9
10
  test/helper.rb
10
11
  test/test_csv_reader.rb
11
12
  test/test_datafile.rb
13
+ test/test_datafile_match.rb
12
14
  test/test_outline_reader.rb
15
+ test/test_season_utils.rb
@@ -23,7 +23,7 @@ end
23
23
  require 'sportdb/formats/version' # let version always go first
24
24
  require 'sportdb/formats/outline_reader'
25
25
  require 'sportdb/formats/datafile'
26
-
26
+ require 'sportdb/formats/season_utils'
27
27
 
28
28
 
29
29
 
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ module SeasonHelper ## use Helpers why? why not?
5
+ def prev( season )
6
+ ## todo: add 1964-1965 format too!!!
7
+ if season =~ /^(\d{4})-(\d{2})$/ ## season format is 1964-65
8
+ fst = $1.to_i - 1
9
+ snd = (100 + $2.to_i - 1) % 100 ## note: add 100 to turn 00 => 99
10
+ "%4d-%02d" % [fst,snd]
11
+ elsif season =~ /^(\d{4})$/
12
+ fst = $1.to_i - 1
13
+ "%4d" % [fst]
14
+ else
15
+ puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
16
+ exit 1
17
+ end
18
+ end # method prev
19
+
20
+
21
+ def key( basename )
22
+ if basename =~ /^(\d{4})[\-\/](\d{4})$/ ## e.g. 2011-2012 or 2011/2012 => 2011/12
23
+ "%4d/%02d" % [$1.to_i, $2.to_i % 100]
24
+ elsif basename =~ /^(\d{4})[\-\/](\d{2})$/ ## e.g. 2011-12 or 2011/12 => 2011/12
25
+ "#{$1}/#{$2}"
26
+ elsif basename =~ /^(\d{4})$/
27
+ $1
28
+ else
29
+ puts "*** !!!! wrong season format >>#{basename}<<; exit; sorry"
30
+ exit 1
31
+ end
32
+ end # method key
33
+
34
+
35
+ def directory( season, format: nil )
36
+ ## todo: find better names for formats - why? why not?:
37
+ ## long | archive | decade(?) => 1980s/1988-89, 2010s/2017-18, ...
38
+ ## short | std(?) => 1988-89, 2017-18, ...
39
+
40
+ ## convert season name to "standard" season name for directory
41
+
42
+ ## todo/fix: move to parse / validate season (for (re)use)!!!! - why? why not?
43
+ if season =~ /^(\d{4})[\-\/](\d{4})$/ ## e.g. 2011-2012 or 2011/2012 => 2011-12
44
+ years = [$1.to_i, $2.to_i]
45
+ elsif season =~ /^(\d{4})[\-\/](\d{2})$/ ## e.g. 2011-12 or 2011/12 => 2011-12
46
+ years = [$1.to_i, $1.to_i+1]
47
+ ## note: check that season end year is (always) season start year + 1
48
+ if ($1.to_i+1) % 100 != $2.to_i
49
+ puts "*** !!!! wrong season format >>#{season}<<; season end year MUST (always) equal season start year + 1; exit; sorry"
50
+ exit 1
51
+ end
52
+ elsif season =~ /^(\d{4})$/
53
+ years = [$1.to_i]
54
+ else
55
+ puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
56
+ exit 1
57
+ end
58
+
59
+
60
+ if ['l', 'long', 'archive' ].include?( format.to_s ) ## note: allow passing in of symbol to e.g. 'long' or :long
61
+ if years.size == 2
62
+ "%3d0s/%4d-%02d" % [years[0] / 10, years[0], years[1] % 100] ## e.g. 2000s/2001-02
63
+ else ## assume size 1 (single year season)
64
+ "%3d0s/%4d" % [years[0] / 10, years[0]]
65
+ end
66
+ else ## default 'short' format / fallback
67
+ if years.size == 2
68
+ "%4d-%02d" % [years[0], years[1] % 100] ## e.g. 2001-02
69
+ else ## assume size 1 (single year season)
70
+ "%4d" % years[0]
71
+ end
72
+ end
73
+ end # method directory
74
+
75
+
76
+
77
+ def start_year( season ) ## get start year
78
+ ## convert season name to "standard" season name for directory
79
+
80
+ ## todo/check: just return year from first for chars - keep it simple - why? why not?
81
+ if season =~ /^(\d{4})[\-\/](\d{4})$/ ## e.g. 2011-2010 or 2011/2011 => 2011-10
82
+ $1
83
+ elsif season =~ /^(\d{4})[\-\/](\d{2})$/
84
+ $1
85
+ elsif season =~ /^(\d{4})$/
86
+ $1
87
+ else
88
+ puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
89
+ exit 1
90
+ end
91
+ end
92
+
93
+ def end_year( season ) ## get end year
94
+ ## convert season name to "standard" season name for directory
95
+ if season =~ /^(\d{4})[\-\/](\d{4})$/ ## e.g. 2011-2010 or 2011/2011 => 2011-10
96
+ $2
97
+ elsif season =~ /^(\d{4})[\-\/](\d{2})$/
98
+ ## note: assume second year is always +1
99
+ ## todo/fix: add assert/check - why? why not?
100
+ ## eg. 1999-00 => 2000 or 1899-00 => 1900
101
+ ($1.to_i+1).to_s
102
+ elsif season =~ /^(\d{4})$/
103
+ $1
104
+ else
105
+ puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
106
+ exit 1
107
+ end
108
+ end
109
+ end # module SeasonHelper
110
+
111
+
112
+ module SeasonUtils
113
+ extend SeasonHelper
114
+ ## lets you use SeasonHelper as "globals" eg.
115
+ ## SeasonUtils.prev( season ) etc.
116
+ end # SeasonUtils
@@ -6,7 +6,7 @@ module Formats
6
6
 
7
7
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
8
8
  MINOR = 1
9
- PATCH = 0
9
+ PATCH = 1
10
10
  VERSION = [MAJOR,MINOR,PATCH].join('.')
11
11
 
12
12
  def self.version
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_datafile_match.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+ class TestDatafileMatch < MiniTest::Test
11
+
12
+ def match_clubs( txt ) Datafile::CLUBS_REGEX.match( txt ); end
13
+ def match_clubs_wiki( txt ) Datafile::CLUBS_WIKI_REGEX.match( txt ); end
14
+ def match_leagues( txt ) Datafile::LEAGUES_REGEX.match( txt ); end
15
+ def match_conf( txt ) Datafile::CONF_REGEX.match( txt ); end
16
+
17
+
18
+ CLUBS_TXT = [ ## with country code
19
+ 'de.clubs.txt',
20
+ 'deutschland/de.clubs.txt',
21
+ ## without country code
22
+ 'europe/de-deutschland/clubs.txt',
23
+ 'de-deutschland/clubs.txt',
24
+ 'deutschland/clubs.txt',
25
+ 'clubs.txt' ]
26
+
27
+ CLUBS_WIKI_TXT = [ ## with country code
28
+ 'de.clubs.wiki.txt',
29
+ 'deutschland/de.clubs.wiki.txt',
30
+ ## without country code
31
+ 'europe/de-deutschland/clubs.wiki.txt',
32
+ 'de-deutschland/clubs.wiki.txt',
33
+ 'deutschland/clubs.wiki.txt',
34
+ 'clubs.wiki.txt' ]
35
+
36
+ LEAGUES_TXT = [ 'europe/england/leagues.txt',
37
+ 'england/leagues.txt',
38
+ 'leagues.txt' ]
39
+
40
+ CONF_TXT = [ 'austria/archives/2000s/2001-02/.conf.txt',
41
+ 'austria/2019-20/.conf.txt',
42
+ '.conf.txt' ]
43
+
44
+
45
+ def test_match_clubs
46
+ CLUBS_TXT.each { |txt| assert match_clubs( txt ) }
47
+
48
+ CLUBS_WIKI_TXT.each { |txt| assert !match_clubs( txt ) }
49
+ end
50
+
51
+ def test_match_clubs_wiki
52
+ CLUBS_WIKI_TXT.each { |txt| assert match_clubs_wiki( txt ) }
53
+
54
+ CLUBS_TXT.each { |txt| assert !match_clubs_wiki( txt ) }
55
+ end
56
+
57
+ def test_match_leagues
58
+ LEAGUES_TXT.each { |txt| assert match_leagues( txt ) }
59
+ end
60
+
61
+ def test_match_conf
62
+ CONF_TXT.each { |txt| assert match_conf( txt ) }
63
+ end
64
+
65
+ end # class TestFindDatafileMatch
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_season_utils.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+ class TestSeasonUtils < MiniTest::Test
11
+
12
+ def test_directory
13
+ assert_equal '2010-11', SeasonUtils.directory( '2010-11' )
14
+ assert_equal '2010-11', SeasonUtils.directory( '2010-2011' )
15
+ assert_equal '2010-11', SeasonUtils.directory( '2010/11' )
16
+ assert_equal '2010-11', SeasonUtils.directory( '2010/2011' )
17
+ assert_equal '2010', SeasonUtils.directory( '2010' )
18
+
19
+ assert_equal '2010s/2010-11', SeasonUtils.directory( '2010-11', format: 'long' )
20
+ assert_equal '2010s/2010-11', SeasonUtils.directory( '2010-2011', format: 'long' )
21
+ assert_equal '2010s/2010', SeasonUtils.directory( '2010', format: 'long' )
22
+
23
+ assert_equal '1999-00', SeasonUtils.directory( '1999-00' )
24
+ assert_equal '1999-00', SeasonUtils.directory( '1999-2000' )
25
+ assert_equal '1990s/1999-00', SeasonUtils.directory( '1999-00', format: 'long' )
26
+ assert_equal '1990s/1999-00', SeasonUtils.directory( '1999-2000', format: 'long' )
27
+ end # method test_diretory
28
+
29
+ end # class TestSeasonlUtils
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sportdb-formats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
@@ -82,11 +82,14 @@ files:
82
82
  - lib/sportdb/formats.rb
83
83
  - lib/sportdb/formats/datafile.rb
84
84
  - lib/sportdb/formats/outline_reader.rb
85
+ - lib/sportdb/formats/season_utils.rb
85
86
  - lib/sportdb/formats/version.rb
86
87
  - test/helper.rb
87
88
  - test/test_csv_reader.rb
88
89
  - test/test_datafile.rb
90
+ - test/test_datafile_match.rb
89
91
  - test/test_outline_reader.rb
92
+ - test/test_season_utils.rb
90
93
  homepage: https://github.com/sportdb/sport.db
91
94
  licenses:
92
95
  - Public Domain