Sha256: 36fe158fd920363ae391e6e543d7fed83a22316012c701629d32af0334330261

Contents?: true

Size: 2 KB

Versions: 21

Compression:

Stored size: 2 KB

Contents

module Picky

  module Query

    # Calculates boosts for combinations.
    #
    # Example:
    #   Someone searches for peter fish.
    #   Picky might match this to categories as follows:
    #     [:name, :food]
    #   and
    #     [:name, :surname]
    #
    # This class is concerned with calculating boosts
    # for the category combinations.
    #
    # Implement either
    #   #boost_for(combinations)
    # or
    #   #boost_for_categories(category_names) # Subclass this class for this.
    #
    # And return a boost (float).
    #
    class Boosts

      attr_reader :boosts

      delegate :empty?,
               :to => :boosts

      # Needs a Hash of
      #   [:category_name1, :category_name2] => +3
      # (some positive or negative weight)
      #
      def initialize boosts = {}
        @boosts = boosts
      end

      # API.
      #
      # Get the boost for an array of category names.
      #
      # Example:
      #   [:name, :height, :color] returns +3, but
      #   [:name, :height, :street] returns -1.
      #
      # Note: Use Array#clustered_uniq_fast to make
      #       [:a, :a, :b, :a] => [:a, :b, :a]
      #
      def boost_for_categories names
        @boosts[names.clustered_uniq_fast] || 0
      end

      # API.
      #
      # Calculates a score for the combinations.
      # Implement #weight_for(category_names) if you don't need the
      # actual combinations, just the category names.
      #
      # Note: Cache this if more complicated weighings become necessary.
      # Note: Maybe make combinations comparable to Symbols?
      #
      def boost_for combinations
        boost_for_categories combinations.map { |combination| combination.category_name }
      end

      # A Weights instance is == to another if
      # the weights are the same.
      #
      def == other
        @boosts == other.boosts
      end

      # Prints out a nice representation of the
      # configured weights.
      #
      def to_s
        "#{self.class}(#{@boosts})"
      end

    end
  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
picky-4.10.0 lib/picky/query/boosts.rb
picky-4.9.0 lib/picky/query/boosts.rb
picky-4.8.1 lib/picky/query/boosts.rb
picky-4.8.0 lib/picky/query/boosts.rb
picky-4.7.0 lib/picky/query/boosts.rb
picky-4.6.6 lib/picky/query/boosts.rb
picky-4.6.5 lib/picky/query/boosts.rb
picky-4.6.4 lib/picky/query/boosts.rb
picky-4.6.3 lib/picky/query/boosts.rb
picky-4.6.2 lib/picky/query/boosts.rb
picky-4.6.1 lib/picky/query/boosts.rb
picky-4.6.0 lib/picky/query/boosts.rb
picky-4.5.12 lib/picky/query/boosts.rb
picky-4.5.11 lib/picky/query/boosts.rb
picky-4.5.10 lib/picky/query/boosts.rb
picky-4.5.9 lib/picky/query/boosts.rb
picky-4.5.8 lib/picky/query/boosts.rb
picky-4.5.7 lib/picky/query/boosts.rb
picky-4.5.6 lib/picky/query/boosts.rb
picky-4.5.5 lib/picky/query/boosts.rb