class PopconResult include Comparable attr_reader :recent, :old, :vote, :unknown def initialize(vote, old, recent, unknown) @vote = vote @old = old @recent = recent @unknown = unknown end def installed @recent + @old + @vote + @unknown end def users @recent + @old + @vote end def usage(total = 1) t = Float(@vote + @recent) / Float(@recent + @old + @vote) t.nan? ? 0.0 : t * total end def <=>(item) self.usage <=> item.usage end end class Popcon include Enumerable attr_accessor :total def initialize(resultsfile, totalpackages = 11000) @data = {} @total = totalpackages File.open(resultsfile) { |f| f.each_line { |line| parts = line.split(/\s+/) @data[parts[0]] = PopconResult.new(parts[1].to_i, parts[2].to_i, parts[3].to_i, parts[4].to_i) } } end def sort @data.sort { |a,b| a[1] <=> b[1] } end def method_missing(f, *args, &func) if @data.respond_to? f @data.__send__(f, *args, &func) else super end end end