Class: League

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(games_array) ⇒ League

Returns a new instance of League.

[View source]

6
7
8
9
# File 'lib/league.rb', line 6

def initialize(games_array)
  @games = games_array
  @score_hash = {}
end

Instance Attribute Details

#gamesObject

Returns the value of attribute games.


4
5
6
# File 'lib/league.rb', line 4

def games
  @games
end

#score_hashObject

Returns the value of attribute score_hash.


4
5
6
# File 'lib/league.rb', line 4

def score_hash
  @score_hash
end

Instance Method Details

#adjust_score(adjustment_data) ⇒ Object

[View source]

25
26
27
28
29
30
31
32
33
# File 'lib/league.rb', line 25

def adjust_score(adjustment_data)
  adjustment_data.each do |key, value|
    if @score_hash.key?(key)
      @score_hash[key] += value
    else
      @score_hash[key] = value
    end
  end
end

#resultsObject

[View source]

11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/league.rb', line 11

def results
  @games.each do |game|
    adjust_score(game.adjustment)
  end

  result_array = @score_hash.sort_by { |k, v|  k }
  result_array = result_array.reverse
  result_array = result_array.sort_by { |k, v|  v }
  result_array = result_array.reverse
  result_array.each_with_index.map do |item, index|
    "#{index + 1}. #{item[0]}, #{item[1]} #{item[1] > 1 || item[1] == 0 ? 'pts' : 'pt'}"
  end
end