class Aperitifilm::Ranking

Constants

PositionAlreadyExists
PositionNotFound

Public Class Methods

new() click to toggle source
# File lib/aperitifilm/ranking.rb, line 8
def initialize
  @positions = {}
end

Public Instance Methods

add_position(item, score) click to toggle source
# File lib/aperitifilm/ranking.rb, line 40
def add_position(item, score)
  if @positions[item.id]
    raise PositionAlreadyExists, "Position with an item with ID '#{item.id}' already exists."
  else
    @positions[item.id] = RankingPosition.new(item, score)
  end
end
find_position(id) { |id| ... } click to toggle source
# File lib/aperitifilm/ranking.rb, line 30
def find_position(id)
  @positions.fetch(id) do
    if block_given?
      yield id
    else
      raise PositionNotFound, "Couldn't find a position with ID '#{id}'."
    end
  end
end
positions(limit = @positions.size) { |position| ... } click to toggle source
# File lib/aperitifilm/ranking.rb, line 12
def positions(limit = @positions.size)
  return enum_for(:positions, limit) unless block_given?

  limit = Integer(limit)

  @positions.values.sort.take(limit).each do |position|
    yield position
  end
end
positions_with_ordinal(limit) { |position, index + 1| ... } click to toggle source
# File lib/aperitifilm/ranking.rb, line 22
def positions_with_ordinal(limit)
  return enum_for(:positions_with_ordinal) unless block_given?

  positions(limit).each_with_index do |position, index|
    yield position, index + 1
  end
end