assemblymade/coderwall

View on GitHub
app/structs/percentile.rb

Summary

Maintainability
A
0 mins
Test Coverage
class Percentile
  class << self
    def for(number)
      ranges[number.to_i]
    end

    private
    def ranges
      @ranges ||= Rails.cache.fetch("percentile") do
        hash   = {}
        scores = Team.all.collect(&:score).compact.sort
        100.downto(1) do |percent|
          index                 = (scores.length * percent / 100).ceil - 1
          percentile            = scores.sort[index]
          hash[percentile.to_i] = percent
          hash
        end
        hash
      end
    end
  end
end