Sha256: cb4894173e83f8262a1dc26c97db8e2b845723e942228a7fb36a23d94a72d2d2

Contents?: true

Size: 1.97 KB

Versions: 17

Compression:

Stored size: 1.97 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

      forward :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 to make
      #       [:a, :a, :b, :a] => [:a, :b, :a]
      #
      def boost_for_categories names
        @boosts[names.clustered_uniq] || 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

17 entries across 17 versions & 1 rubygems

Version Path
picky-4.16.0 lib/picky/query/boosts.rb
picky-4.15.1 lib/picky/query/boosts.rb
picky-4.15.0 lib/picky/query/boosts.rb
picky-4.14.0 lib/picky/query/boosts.rb
picky-4.13.1 lib/picky/query/boosts.rb
picky-4.13.0 lib/picky/query/boosts.rb
picky-4.12.13 lib/picky/query/boosts.rb
picky-4.12.12 lib/picky/query/boosts.rb
picky-4.12.11 lib/picky/query/boosts.rb
picky-4.12.10 lib/picky/query/boosts.rb
picky-4.12.8 lib/picky/query/boosts.rb
picky-4.12.7 lib/picky/query/boosts.rb
picky-4.12.6 lib/picky/query/boosts.rb
picky-4.12.5 lib/picky/query/boosts.rb
picky-4.12.4 lib/picky/query/boosts.rb
picky-4.12.3 lib/picky/query/boosts.rb
picky-4.12.2 lib/picky/query/boosts.rb