Module: SeasonHelper

Included in:
SeasonUtils
Defined in:
lib/sportdb/config/season_utils.rb

Overview

use Helpers why? why not?

Instance Method Summary collapse

Instance Method Details

#directory(season, format: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sportdb/config/season_utils.rb', line 35

def directory( season, format: nil )
  ##  todo: find better names for formats - why? why not?:
  ##    long | archive | decade(?)   =>   1980s/1988-89, 2010s/2017-18, ...
  ##    short | std(?)   =>   1988-89, 2017-18, ...

  ## convert season name to "standard" season name for directory

  ## todo/fix: move to parse / validate season (for (re)use)!!!! - why? why not?
  if season =~ /^(\d{4})[\-\/](\d{4})$/   ## e.g. 2011-2012 or 2011/2012 => 2011-12
    years = [$1.to_i, $2.to_i]
  elsif season =~ /^(\d{4})[\-\/](\d{2})$/   ## e.g. 2011-12 or 2011/12  => 2011-12
    years = [$1.to_i, $1.to_i+1]
    ## note: check that season end year is (always) season start year + 1
    if ($1.to_i+1) % 100 != $2.to_i
      puts "*** !!!! wrong season format >>#{season}<<; season end year MUST (always) equal season start year + 1; exit; sorry"
      exit 1
    end
  elsif season =~ /^(\d{4})$/
    years = [$1.to_i]
  else
    puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
    exit 1
  end


  if ['l', 'long', 'archive' ].include?( format.to_s )   ## note: allow passing in of symbol to e.g. 'long' or :long
    if years.size == 2
      "%3d0s/%4d-%02d" % [years[0] / 10, years[0], years[1] % 100]   ## e.g. 2000s/2001-02
    else  ## assume size 1 (single year season)
      "%3d0s/%4d" % [years[0] / 10, years[0]]
    end
  else  ## default 'short' format / fallback
    if years.size == 2
      "%4d-%02d" % [years[0], years[1] % 100]   ## e.g. 2001-02
    else  ## assume size 1 (single year season)
      "%4d" % years[0]
    end
  end
end

#end_year(season) ⇒ Object

get end year



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sportdb/config/season_utils.rb', line 93

def end_year( season ) ## get end year
  ## convert season name to "standard" season name for directory
  if season =~ /^(\d{4})[\-\/](\d{4})$/   ## e.g. 2011-2010 or 2011/2011 => 2011-10
    $2
  elsif season =~ /^(\d{4})[\-\/](\d{2})$/
    ## note: assume second year is always +1
    ##    todo/fix: add assert/check - why? why not?
    ## eg. 1999-00 => 2000 or 1899-00 => 1900
    ($1.to_i+1).to_s
  elsif season =~ /^(\d{4})$/
    $1
  else
    puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
    exit 1
  end
end

#key(basename) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sportdb/config/season_utils.rb', line 21

def key( basename )
  if basename =~ /^(\d{4})[\-\/](\d{4})$/      ## e.g. 2011-2012 or 2011/2012 => 2011/12
    "%4d/%02d" % [$1.to_i, $2.to_i % 100]
  elsif basename =~ /^(\d{4})[\-\/](\d{2})$/   ## e.g. 2011-12 or 2011/12  => 2011/12
    "#{$1}/#{$2}"
  elsif basename =~ /^(\d{4})$/
    $1
  else
    puts "*** !!!! wrong season format >>#{basename}<<; exit; sorry"
    exit 1
  end
end

#prev(season) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sportdb/config/season_utils.rb', line 5

def prev( season )
  ## todo: add 1964-1965 format too!!!
  if season =~ /^(\d{4})-(\d{2})$/    ## season format is  1964-65
    fst = $1.to_i - 1
    snd = (100 + $2.to_i - 1) % 100    ## note: add 100 to turn 00 => 99
    "%4d-%02d" % [fst,snd]
  elsif season =~ /^(\d{4})$/
    fst = $1.to_i - 1
    "%4d" % [fst]
  else
    puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
    exit 1
  end
end

#start_year(season) ⇒ Object

get start year



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sportdb/config/season_utils.rb', line 77

def start_year( season ) ## get start year
  ## convert season name to "standard" season name for directory

  ## todo/check:  just return year from first for chars - keep it simple - why? why not?
  if season =~ /^(\d{4})[\-\/](\d{4})$/   ## e.g. 2011-2010 or 2011/2011 => 2011-10
    $1
  elsif season =~ /^(\d{4})[\-\/](\d{2})$/
    $1
  elsif season =~ /^(\d{4})$/
    $1
  else
    puts "*** !!!! wrong season format >>#{season}<<; exit; sorry"
    exit 1
  end
end