Class: Season

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/season-formats/season.rb

Constant Summary collapse

YYYY_YYYY_RE =

todo: add unicode - too - why? why not? see wikipedia pages, for example

%r{^      ## e.g. 2011-2012 or 2011/2012
  (\d{4})
    [/-]
  (\d{4})
 $
}x
YYYY_YY_RE =
%r{^       ## e.g. 2011-12 or 2011/12
  (\d{4})
    [/-]
  (\d{2})
 $
}x
YYYY_Y_RE =
%r{^       ## e.g. 2011-2 or 2011/2
  (\d{4})
    [/-]
  (\d{1})
 $
}x
YYYY_RE =
%r{^       ## e.g. 2011
  (\d{4})
 $
}x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Season

change args to years - why? why not?



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/season-formats/season.rb', line 87

def initialize( *args )   ## change args to years - why? why not?
  if args.size == 1 && args[0].is_a?( Integer )
    @start_year = args[0]
    @end_year   = args[0]
  elsif args.size == 2 && args[0].is_a?( Integer ) &&
                          args[1].is_a?( Integer )
    @start_year = args[0]
    @end_year   = args[1]
    end_year_exp = @start_year+1
    raise ArgumentError, "[Season] invalid year in season >>#{to_s}<<; expected #{end_year_exp} but got #{@end_year}"   if end_year_exp != @end_year
  else
    pp args
    raise ArgumentError, "[Season] expected season start year (integer) with opt. end year"
  end
end

Instance Attribute Details

#end_yearObject (readonly)

Returns the value of attribute end_year.



84
85
86
# File 'lib/season-formats/season.rb', line 84

def end_year
  @end_year
end

#start_yearObject (readonly)

Returns the value of attribute start_year.



84
85
86
# File 'lib/season-formats/season.rb', line 84

def start_year
  @start_year
end

Class Method Details

._parse(str) ⇒ Object

“internal” parse helper



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/season-formats/season.rb', line 43

def self._parse( str )   ## "internal" parse helper
  if str =~ YYYY_YYYY_RE    ## e.g. 2011/2012
    [$1.to_i, $2.to_i]
  elsif str =~ YYYY_YY_RE   ## e.g. 2011/12
    fst = $1.to_i
    snd = $2.to_i
    snd_exp = '%02d' % [(fst+1) % 100]    ## double check: e.g 00 == 00, 01==01 etc.
    raise ArgumentError, "[Season.parse] invalid year in season >>#{str}<<; expected #{snd_exp} but got #{$2}"   if snd_exp != $2
    [fst, fst+1]
  elsif str =~ YYYY_Y_RE    ## e.g. 2011/2
    fst = $1.to_i
    snd = $2.to_i
    snd_exp = '%d' % [(fst+1) % 10]      ## double check: e.g 0 == 0, 1==1 etc.
    raise ArgumentError, "[Season.parse] invalid year in season >>#{str}<<; expected #{snd_exp} but got #{$2}"   if snd_exp != $2
    [fst, fst+1]
  elsif str =~ YYYY_RE      ## e.g. 2011
    [$1.to_i]
  else
    raise ArgumentError, "[Season.parse] unkown season format >>#{str}<<; sorry cannot parse"
  end
end

.convert(*args) ⇒ Object

note: used by Kernel method Season()



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/season-formats/season.rb', line 66

def self.convert( *args )  ## note: used by Kernel method Season()
  if args.size == 1 && args[0].is_a?( Season )
    args[0]  # pass through / along as is 1:1
  elsif args.size == 1 && args[0].is_a?( String )
    parse( args[0] )
  elsif args.size == 1 && args[0].is_a?( Integer ) && args[0] > 9999
    ## note: allow convenience "hack" such as:
    # 202021   or 2020_21   => '2020/21' or
    # 2020_1   or 2020_1    => '2020/21' or
    # 20202021 or 2020_2021 => '2020/21'
    str = args[0].to_s
    parse( "#{str[0..3]}/#{str[4..-1]}" )
  else ## assume all integer args e.g.  2020 or 2020, 2021 and such
    new( *args ) ## try conversion with new
  end
end

.parse(str) ⇒ Object



39
40
41
# File 'lib/season-formats/season.rb', line 39

def self.parse( str )
  new( *_parse( str ))
end

Instance Method Details

#<=>(other) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/season-formats/season.rb', line 134

def <=>(other)
  ## todo/fix/fix:  check if other is_a?( Season )!!!
  ##  what to return if other type/class ??
  ## note: check special edge case - year season and other e.g.
  ##    2010 <=> 2010/2011

  res = @start_year <=> other.start_year
  res = @end_year   <=> other.end_year     if res == 0
  res
end

#academic_year?Boolean Also known as: academic?

Returns:

  • (Boolean)


110
# File 'lib/season-formats/season.rb', line 110

def academic_year?() !calenar_year?; end

#calendar_year?Boolean Also known as: calendar?, year?

single-year season e.g. 2011 if start_year is end_year - todo - find a better name?

Returns:

  • (Boolean)


106
# File 'lib/season-formats/season.rb', line 106

def calendar_year?()  @start_year == @end_year; end

#keyObject Also known as: to_key, name, title, inspect



162
# File 'lib/season-formats/season.rb', line 162

def key()  to_s( :short ); end

#nextObject Also known as: succ



123
124
125
126
127
128
129
# File 'lib/season-formats/season.rb', line 123

def next
  if year?
    Season.new( @start_year+1 )
  else
    Season.new( @start_year+1, @end_year+1 )
  end
end

#prevObject



115
116
117
118
119
120
121
# File 'lib/season-formats/season.rb', line 115

def prev
  if year?
    Season.new( @start_year-1 )
  else
    Season.new( @start_year-1, @end_year-1 )
  end
end

#start_dateObject

more convenience helper - move to sportdb or such - remove - why - why not???



192
193
194
195
196
197
198
# File 'lib/season-formats/season.rb', line 192

def start_date   ## generate "generic / syntetic start date" - keep helper - why? why not?
  if year?
    Date.new( start_year, 1, 1 )
  else
    Date.new( start_year 1, 7 )
  end
end

#to_formatted_s(format = :default, sep: '/') ⇒ Object Also known as: to_s



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/season-formats/season.rb', line 146

def to_formatted_s( format=:default, sep: '/' )
  if year?
    '%d' % @start_year
  else
    case format
    when :default, :short, :s   ## e.g. 1999/00 or 2019/20
      "%d#{sep}%02d" % [@start_year, @end_year % 100]
    when :long, :l              ## e.g. 1999/2000 or 2019/2020
      "%d#{sep}%d" % [@start_year, @end_year]
    else
      raise ArgumentError, "[Season.to_s] unsupported format >#{format}<"
    end
  end
end

#to_path(format = :default) ⇒ Object Also known as: directory, path



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/season-formats/season.rb', line 171

def to_path( format=:default )
  case format
  when :default, :short, :s    ## e.g. 1999-00 or 2019-20
    to_s( :short, sep: '-' )
  when :long, :l                ## e.g. 1999-2000 or 2019-2000
    to_s( :long, sep: '-' )
  when :archive, :decade, :d    ## e.g. 1990s/1999-00 or 2010s/2019-20
    "%3d0s/%s" % [@start_year / 10, to_s( :short, sep: '-' )]
  when :century, :c             ## e.g. 1900s/1990-00 or 2000s/2019-20
    "%2d00s/%s" % [@start_year / 100, to_s( :short, sep: '-' )]
  else
    raise ArgumentError, "[Season.to_path] unsupported format >#{format}<"
  end
end