Class: Ranker::Rankings

Inherits:
Array
  • Object
show all
Defined in:
lib/ranker/rankings.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy) ⇒ Rankings

Returns a new instance of Rankings.



7
8
9
10
# File 'lib/ranker/rankings.rb', line 7

def initialize(strategy)
  @strategy = strategy
  @scores = []
end

Instance Attribute Details

#scoresObject (readonly)

Returns the value of attribute scores.



5
6
7
# File 'lib/ranker/rankings.rb', line 5

def scores
  @scores
end

#strategyObject (readonly)

Returns the value of attribute strategy.



5
6
7
# File 'lib/ranker/rankings.rb', line 5

def strategy
  @strategy
end

Instance Method Details

#create_ranking(rank, score, rankables) ⇒ Object

Methods:



60
61
62
63
64
65
# File 'lib/ranker/rankings.rb', line 60

def create_ranking(rank, score, rankables)
  scores.concat(Array.new(rankables.count, score))
  ranking = Ranking.new(self, self.count, rank, score, rankables)
  self << ranking
  ranking
end

#errorsObject

Properties:



15
16
17
# File 'lib/ranker/rankings.rb', line 15

def errors
  @errors ||= {}
end

#meanObject



19
20
21
# File 'lib/ranker/rankings.rb', line 19

def mean
  @mean ||= total.to_f / num_scores
end

#num_scoresObject



32
33
34
# File 'lib/ranker/rankings.rb', line 32

def num_scores
  scores.size
end

#standard_deviationObject



23
24
25
26
27
28
29
30
# File 'lib/ranker/rankings.rb', line 23

def standard_deviation
  @standard_deviation ||= if variance.nan?
    # For ruby-1.8.7 compatibility
    variance
  else
    Math.sqrt(variance)
  end
end

#totalObject

Raises:



40
41
42
43
44
# File 'lib/ranker/rankings.rb', line 40

def total
  raise Error.new(errors) unless valid?

  @total ||= scores.reduce(:+)
end

#total_differenceObject



46
47
48
49
50
# File 'lib/ranker/rankings.rb', line 46

def total_difference
  @total_difference ||= scores.reduce(0) { |sum, score|
    sum + (score - mean) ** 2
  }
end

#valid?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/ranker/rankings.rb', line 52

def valid?
  validate
  errors.empty?
end

#varianceObject



36
37
38
# File 'lib/ranker/rankings.rb', line 36

def variance
  @variance ||= total_difference.to_f / num_scores
end