Sha256: 91e9e641fe81281b88d38cf1af96e5644bfdde5b02f1fa9f11f5920e97189e33

Contents?: true

Size: 1.19 KB

Versions: 112

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8
#
module Picky

  class Results

    # This index combines an exact and partial index.
    # It serves to order the results such that exact hits are found first.
    #
    module ExactFirst

      # Installs the exact first on the given category
      # or on the categories of the index, if an index is given.
      #
      # THINK Can we unextend in the case it is an index?
      #
      def self.extended index_or_category
        if index_or_category.respond_to? :categories
          extend_each_of index_or_category.categories
          index_or_category
        end
      end
      def self.extend_each_of categories
        categories.categories.each { |category| category.extend self }
      end

      # Overrides the original method.
      #
      def ids token
        text = token.text
        if token.partial?
          exact.ids(text) | partial.ids(text)
        else
          exact.ids text
        end
      end

      # Overrides the original method.
      #
      def weight token
        text = token.text
        if token.partial?
          [exact.weight(text), partial.weight(text)].compact.max
        else
          exact.weight text
        end
      end

    end

  end

end

Version data entries

112 entries across 112 versions & 1 rubygems

Version Path
picky-4.31.3 lib/picky/results/exact_first.rb
picky-4.31.2 lib/picky/results/exact_first.rb
picky-4.31.1 lib/picky/results/exact_first.rb
picky-4.31.0 lib/picky/results/exact_first.rb
picky-4.30.0 lib/picky/results/exact_first.rb
picky-4.29.0 lib/picky/results/exact_first.rb
picky-4.28.1 lib/picky/results/exact_first.rb
picky-4.27.1 lib/picky/results/exact_first.rb
picky-4.27.0 lib/picky/results/exact_first.rb
picky-4.26.2 lib/picky/results/exact_first.rb
picky-4.26.1 lib/picky/results/exact_first.rb
picky-4.26.0 lib/picky/results/exact_first.rb
picky-4.25.3 lib/picky/results/exact_first.rb
picky-4.25.2 lib/picky/results/exact_first.rb
picky-4.25.1 lib/picky/results/exact_first.rb
picky-4.25.0 lib/picky/results/exact_first.rb
picky-4.24.0 lib/picky/results/exact_first.rb
picky-4.23.2 lib/picky/results/exact_first.rb
picky-4.23.1 lib/picky/results/exact_first.rb
picky-4.23.0 lib/picky/results/exact_first.rb