Sha256: aaa3c35661560d47bf924d62af5da0929a7db585b2ece9640b345856c6eee49c

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Picky

  #
  #
  class Category

    # Loads the index from cache.
    #
    def load
      Picky.logger.load self
      clear_realtime # THINK Should we really explicitly clear the realtime? Or should it just be loaded?
      exact.load
      partial.load
    end

    # Gets the weight for this token's text.
    #
    def weight token
      bundle = bundle_for token
      if range = token.range
        # TODO We might be able to return early?
        #
        @ranger.new(*range).inject(nil) do |sum, text|
          weight = bundle.weight text
          weight && (weight + (sum || 0)) || sum
        end
      else
        bundle.weight token.text
      end
    end

    # Gets the ids for this token's text.
    #
    def ids token
      bundle = bundle_for token
      if range = token.range
        # Adding all to an array, then flattening
        # is faster than using ary + ary.
        #
        @ranger.new(*range).inject([]) do |result, text|
          # It is 30% faster using the empty check
          # than just << [].
          #
          ids = bundle.ids text
          ids.empty? ? result : result << ids
        end.flatten
      else
        bundle.ids token.text
      end
    end

    # Returns the right index bundle for this token.
    #
    def bundle_for token
      token.partial? ? partial : exact
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
picky-4.17.1 lib/picky/category_indexed.rb
picky-4.17.0 lib/picky/category_indexed.rb