Class: SportDb::Standings

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Standings

Returns a new instance of Standings.



39
40
41
42
43
44
45
46
47
48
# File 'lib/sportdb/standings.rb', line 39

def initialize( 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
end

Instance Method Details

#to_aObject



68
69
70
71
72
73
74
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
# File 'lib/sportdb/standings.rb', line 68

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sportdb/standings.rb', line 51

def update( match_or_matches )
  puts " [debug] update match_or_matches.class.name: #{match_or_matches.class.name}"

  ## convenience - update all matches at once
  if match_or_matches.is_a?( Array ) ||
     match_or_matches.is_a?( ActiveRecord::Associations::CollectionProxy )
    matches = match_or_matches
    matches.each_with_index do |match,i| # note: index(i) starts w/ zero (0)
      update_match( match )
    end
  else
    match = match_or_matches
    update_match( match )
  end
  self  # note: return self to allow chaining
end