Class: Sports::Matchlist::StatLine

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/structs/structs/matchlist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatLine

Returns a new instance of StatLine.



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sportdb/structs/structs/matchlist.rb', line 148

def initialize
   @matches    = 0
   @goals      = 0

   @start_date = nil
   @end_date   = nil

   @team_usage = Hash.new(0)

   @match_counts = nil
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



71
72
73
# File 'lib/sportdb/structs/structs/matchlist.rb', line 71

def end_date
  @end_date
end

#goalsObject (readonly)

Returns the value of attribute goals.



71
72
73
# File 'lib/sportdb/structs/structs/matchlist.rb', line 71

def goals
  @goals
end

#matchesObject (readonly)

Returns the value of attribute matches.



71
72
73
# File 'lib/sportdb/structs/structs/matchlist.rb', line 71

def matches
  @matches
end

#roundsObject (readonly)

Returns the value of attribute rounds.



71
72
73
# File 'lib/sportdb/structs/structs/matchlist.rb', line 71

def rounds
  @rounds
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



71
72
73
# File 'lib/sportdb/structs/structs/matchlist.rb', line 71

def start_date
  @start_date
end

#team_usageObject (readonly)

Returns the value of attribute team_usage.



71
72
73
# File 'lib/sportdb/structs/structs/matchlist.rb', line 71

def team_usage
  @team_usage
end

Instance Method Details

#build_match_countsObject

use/rename to matches_played - why? why not?



119
120
121
122
123
124
125
126
127
128
# File 'lib/sportdb/structs/structs/matchlist.rb', line 119

def build_match_counts   ## use/rename to matches_played - why? why not?
  counts = Hash.new(0)
  team_usage.values.each do |count|
    counts[count] += 1
  end

  ## sort (descending) highest usage value first (in returned array)
  ##  e.g. [[32,8],[31,2]]  ## 32 matches by 8 teams, 31 matches by 2 teams etc.
  counts.sort_by {|count, usage| -count }
end

#dates_strObject



84
85
86
87
88
89
90
91
# File 'lib/sportdb/structs/structs/matchlist.rb', line 84

def dates_str
  ## note: start_date/end_date might be optional/missing
  if has_dates?
    "#{start_date.strftime( '%a %d %b %Y' )} - #{end_date.strftime( '%a %d %b %Y' )}"
  else
    "??? - ???"
  end
end

#daysObject



93
# File 'lib/sportdb/structs/structs/matchlist.rb', line 93

def days() end_date.jd - start_date.jd; end

#end_date?Boolean

Returns:

  • (Boolean)


81
# File 'lib/sportdb/structs/structs/matchlist.rb', line 81

def end_date?()   @end_date.nil? == false; end

#has_dates?Boolean

Returns:

  • (Boolean)


83
# File 'lib/sportdb/structs/structs/matchlist.rb', line 83

def has_dates?()  @start_date && @end_date; end

#match_countsObject



130
131
132
133
134
# File 'lib/sportdb/structs/structs/matchlist.rb', line 130

def match_counts
  # match counts / nos played per team
  @match_counts ||= build_match_counts
  @match_counts
end

#match_counts_strObject



136
137
138
139
140
141
142
143
144
# File 'lib/sportdb/structs/structs/matchlist.rb', line 136

def match_counts_str
  ## pretty print / formatted match_counts
  buf = String.new('')
  match_counts.each_with_index do |rec,i|
    buf << ' '  if i > 0   ## add (space) separator
    buf << "#{rec[0]}×#{rec[1]}"
  end
  buf
end

#rounds?Boolean

todo: add has_rounds? alias for rounds? too

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sportdb/structs/structs/matchlist.rb', line 102

def rounds?
   ## return true if all match_played in team_usage are the same
   ##  e.g. assumes league with matchday rounds
   if @has_rounds.nil?    ## check/todo: if undefined attribute is nil by default??
      ## check/calc rounds
      ##  note: values => matches_played by team
      if match_counts.size == 1
        @rounds = match_counts[0][0]
      else
        @rounds = nil
      end
      @has_rounds = @rounds ? true : false
   end
   @has_rounds
end

#start_date?Boolean

Returns:

  • (Boolean)


80
# File 'lib/sportdb/structs/structs/matchlist.rb', line 80

def start_date?() @start_date.nil? == false; end

#teamsObject

(auto-)sort here always - why? why not?



78
# File 'lib/sportdb/structs/structs/matchlist.rb', line 78

def teams() @team_usage.keys.sort; end

#update(match) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/sportdb/structs/structs/matchlist.rb', line 161

def update( match )
   @matches += 1    ## match counter

   if match.score1 && match.score2
     @goals += match.score1
     @goals += match.score2

     ## todo: add after extra time? if knock out (k.o.) - why? why not?
     ##   make it a flag/opt?
   end

   @team_usage[ match.team1 ] += 1
   @team_usage[ match.team2 ] += 1

   if match.date
     ## return / store date as string as is - why? why not?
     date = Date.strptime( match.date, '%Y-%m-%d' )

     @start_date = date  if @start_date.nil? || date < @start_date
     @end_date   = date  if @end_date.nil?   || date > @end_date
   end
end