Class: Sports::Standings

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

Defined Under Namespace

Classes: StandingsLine

Instance Method Summary collapse

Constructor Details

#initialize(match_or_matches = nil, opts = {}) ⇒ Standings

Returns a new instance of Standings.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sportdb/structs/structs/standings.rb', line 40

def initialize( match_or_matches=nil, opts={} )
  ## fix:
  # passing in e.g. pts for win (3? 2? etc.)
  # default to 3 for now

  ## lets you pass in 2 as an alterantive, for example
  @pts_won = opts[:pts_won] || 3

  @lines = {}   # StandingsLines cached by team name/key

  ## add init and update all-in-one convenience shortcut
  update( match_or_matches )  if match_or_matches
end

Instance Method Details

#build(source: nil) ⇒ Object

fix: move build to StandingsPart/Report !!!!



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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/standings.rb', line 114

def build( source: nil )   ## build / pretty print standings table in string buffer
  ## keep pretty printer in struct - why? why not?


  ## add standings table in markdown to buffer (buf)

  ## todo: use different styles/formats (simple/ etc  ???)

## simple table (only totals - no home/away)
##       standings.to_a.each do |l|
##         buf << '%2d. '  % l.rank
##         buf << '%-28s  ' % l.team
##         buf << '%2d '     % l.played
##         buf << '%3d '     % l.won
##         buf << '%3d '     % l.drawn
##         buf << '%3d '     % l.lost
##         buf << '%3d:%-3d ' % [l.goals_for,l.goals_against]
##         buf << '%3d'       % l.pts
##         buf << "\n"
##       end

  buf = ''
  buf << "\n"
  buf << "```\n"
  buf << "                                        - Home -          - Away -            - Total -\n"
  buf << "                                 Pld   W  D  L   F:A     W  D  L   F:A      F:A   +/-  Pts\n"

  to_a.each do |l|
    buf << '%2d. '  % l.rank
    buf << '%-28s  ' % l.team
    buf << '%2d  '     % l.played

    buf << '%2d '      % l.home_won
    buf << '%2d '      % l.home_drawn
    buf << '%2d '      % l.home_lost
    buf << '%3d:%-3d  ' % [l.home_goals_for,l.home_goals_against]

    buf << '%2d '       % l.away_won
    buf << '%2d '       % l.away_drawn
    buf << '%2d '       % l.away_lost
    buf << '%3d:%-3d  ' % [l.away_goals_for,l.away_goals_against]

    buf << '%3d:%-3d ' % [l.goals_for,l.goals_against]

    goals_diff = l.goals_for-l.goals_against
    if goals_diff > 0
      buf << '%3s  '  %  "+#{goals_diff}"
    elsif goals_diff < 0
      buf << '%3s  '  %  "#{goals_diff}"
    else ## assume 0
      buf << '     '
    end

    buf << '%3d'       % l.pts
    buf << "\n"
  end

  buf << "```\n"
  buf << "\n"

  ## optinal: add data source if known / present
  ##   assume (relative) markdown link for now in README.md
  if source
    buf << "(Source: [`#{source}`](#{source}))\n"
    buf << "\n"
  end

  buf
end

#each(&block) ⇒ Object

note: add a convenience shortcut

to_a  will sort and add rank (1,2,3) to standing lines


73
# File 'lib/sportdb/structs/structs/standings.rb', line 73

def each( &block ) to_a.each( &block ); end

#to_aObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sportdb/structs/structs/standings.rb', line 75

def to_a
  ## return lines; sort and add rank
  ## note: will update rank!!!! (side effect)

  #############################
  ### calc ranking position (rank)
  ## fix/allow same rank e.g. all 1 or more than one team 3rd etc.

  # build array from hash
  ary = []
  @lines.each do |k,v|
    ary << v
  end

  ary.sort! do |l,r|
    ## note: reverse order (thus, change l,r to r,l)
    value = r.pts <=> l.pts
    if value == 0 # same pts try goal diff
      value = (r.goals_for-r.goals_against) <=> (l.goals_for-l.goals_against)
      if value == 0 # same goal diff too; try assume more goals better for now
        value = r.goals_for <=> l.goals_for
      end
    end
    value
  end

  ## update rank using ordered array
  ary.each_with_index do |line,i|
    line.rank = i+1 ## add ranking (e.g. 1,2,3 etc.) - note: i starts w/ zero (0)
  end

  ary
end

#update(match_or_matches) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sportdb/structs/structs/standings.rb', line 55

def update( match_or_matches )
  ## convenience - update all matches at once
  ## todo/check:  check for ActiveRecord_Associations_CollectionProxy and than use to_a (to "force" array) - why? why not?
  matches = if match_or_matches.is_a?(Array)
              match_or_matches
            else
              [match_or_matches]
            end

  matches.each_with_index do |match,i| # note: index(i) starts w/ zero (0)
    update_match( match )
  end
  self  # note: return self to allow chaining
end