Class: WeightedListRank::Strategy

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

Instance Method Summary collapse

Instance Method Details

#calculate_score(list, item) ⇒ Numeric

Calculates the score for an item within a list. Must be implemented by subclass.

Parameters:

  • list (List)

    the list containing the item.

  • item (Item)

    the item to calculate the score for.

Returns:

  • (Numeric)

    the calculated score for the item.

Raises:

  • (NotImplementedError)


7
8
9
# File 'lib/weighted_list_rank/strategy.rb', line 7

def calculate_score(list, item)
  raise NotImplementedError
end

#calculate_scores(list) ⇒ Array<Numeric>

Calculates the scores for every item in a list, returned in the same order as list.items.

This is the entry point RankingContext uses. The default implementation simply delegates to #calculate_score for each item, so strategies that implement only the single-item API continue to work unchanged.

Strategies whose scoring depends on values that are constant across the list (a total, a count, a normalizing factor) should override this and compute those values once, rather than recomputing them for every item.

Parameters:

  • list (List)

    the list whose items should be scored.

Returns:

  • (Array<Numeric>)

    scores positionally matching list.items.



24
25
26
# File 'lib/weighted_list_rank/strategy.rb', line 24

def calculate_scores(list)
  list.items.map { |item| calculate_score(list, item) }
end