Sha256: 0e773503bd3f63122c22d232c4c286f2dd523c4b58950274b8197ca5bcf52da5

Contents?: true

Size: 1.37 KB

Versions: 24

Compression:

Stored size: 1.37 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
      token.select_bundle exact, partial
    end

  end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
picky-4.25.3 lib/picky/category_indexed.rb
picky-4.25.2 lib/picky/category_indexed.rb
picky-4.25.1 lib/picky/category_indexed.rb
picky-4.25.0 lib/picky/category_indexed.rb
picky-4.24.0 lib/picky/category_indexed.rb
picky-4.23.2 lib/picky/category_indexed.rb
picky-4.23.1 lib/picky/category_indexed.rb
picky-4.23.0 lib/picky/category_indexed.rb
picky-4.22.0 lib/picky/category_indexed.rb
picky-4.21.2 lib/picky/category_indexed.rb
picky-4.21.1 lib/picky/category_indexed.rb
picky-4.21.0 lib/picky/category_indexed.rb
picky-4.20.2 lib/picky/category_indexed.rb
picky-4.20.1 lib/picky/category_indexed.rb
picky-4.20.0 lib/picky/category_indexed.rb
picky-4.19.7 lib/picky/category_indexed.rb
picky-4.19.6 lib/picky/category_indexed.rb
picky-4.19.5 lib/picky/category_indexed.rb
picky-4.19.4 lib/picky/category_indexed.rb
picky-4.19.3 lib/picky/category_indexed.rb