Sha256: 56beb033310308f3c2970cf41c373c6dc8307395760165cedd6520e55c7691f4

Contents?: true

Size: 1.32 KB

Versions: 16

Compression:

Stored size: 1.32 KB

Contents

# encoding: utf-8
#
module Picky

  module Wrappers

    module Category

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

        delegate :similar,
                 :identifier,
                 :name,
                 :to => :@exact
        delegate :index,
                 :category,
                 :weight,
                 :generate_partial_from,
                 :generate_caches_from_memory,
                 :generate_derived,
                 :dump,
                 :load,
                 :to => :@partial

        def initialize category
          @exact   = category.exact
          @partial = category.partial
        end

        def self.wrap index_or_category
          if index_or_category.respond_to? :categories
            wrap_each_of index_or_category.categories
            index_or_category
          else
            new index_or_category
          end
        end
        def self.wrap_each_of categories
          categories.categories.collect! { |category| new(category) }
        end

        def ids text
          @exact.ids(text) + @partial.ids(text)
        end

        def weight text
          [@exact.weight(text) || 0, @partial.weight(text) || 0].max
        end

      end

    end

  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
picky-3.6.4 lib/picky/wrappers/category/exact_first.rb
picky-3.6.3 lib/picky/wrappers/category/exact_first.rb
picky-3.6.2 lib/picky/wrappers/category/exact_first.rb
picky-3.6.1 lib/picky/wrappers/category/exact_first.rb
picky-3.6.0 lib/picky/wrappers/category/exact_first.rb
picky-3.5.4 lib/picky/wrappers/category/exact_first.rb
picky-3.5.3 lib/picky/wrappers/category/exact_first.rb
picky-3.5.2 lib/picky/wrappers/category/exact_first.rb
picky-3.5.1 lib/picky/wrappers/category/exact_first.rb
picky-3.5.0 lib/picky/wrappers/category/exact_first.rb
picky-3.4.3 lib/picky/wrappers/category/exact_first.rb
picky-3.4.2 lib/picky/wrappers/category/exact_first.rb
picky-3.4.1 lib/picky/wrappers/category/exact_first.rb
picky-3.4.0 lib/picky/wrappers/category/exact_first.rb
picky-3.3.3 lib/picky/wrappers/category/exact_first.rb
picky-3.3.2 lib/picky/wrappers/category/exact_first.rb