Sha256: 551a3fda47df93414b8b6a81cae9b1af8cbfcf41d90be46dcad3361d2f665ac8

Contents?: true

Size: 1.96 KB

Versions: 11

Compression:

Stored size: 1.96 KB

Contents

# encoding: utf-8
#
module Internals
  
  #
  #
  module Query
  
    # This class primarily handles switching through similar token constellations.
    #
    class Tokens # :nodoc:all
    
      # Basically delegates to its internal tokens array.
      #
      self.delegate *[Enumerable.instance_methods, :slice!, :[], :uniq!, :last, :reject!, :length, :size, :empty?, :each, :exit, { :to => :@tokens }].flatten
    
      #
      #
      def initialize tokens = []
        @tokens = tokens
      end
    
      #
      #
      def tokenize_with tokenizer
        @tokens.each { |token| token.tokenize_with(tokenizer) }
      end
    
      # Generates an array in the form of
      # [
      #  [combination],                           # of token 1
      #  [combination, combination, combination], # of token 2
      #  [combination, combination]               # of token 3
      # ]
      #
      # TODO If we want token behaviour defined per Query, we can
      #      compact! here
      #
      def possible_combinations_in type
        @tokens.inject([]) do |combinations, token|
          combinations << token.possible_combinations_in(type)
        end
        # TODO compact! if ignore_unassigned_tokens
      end

      # Makes the last of the tokens partial.
      #
      def partialize_last
        @tokens.last.partial = true unless empty?
      end

      # Caps the tokens to the maximum.
      #
      def cap maximum
        @tokens.slice!(maximum..-1) if cap?(maximum)
      end
      def cap? maximum
        @tokens.size > maximum
      end
    
      # Rejects blank tokens.
      #
      def reject
        @tokens.reject! &:blank?
      end
    
      # Returns a solr query.
      #
      def to_solr_query
        @tokens.map(&:to_solr).join ' '
      end
    
      #
      #
      def originals
        @tokens.map(&:original)
      end
    
      # Just join the token original texts.
      #
      def to_s
        originals.join ' '
      end
    
    end
  
  end
  
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
picky-2.0.0 lib/picky/internals/query/tokens.rb
picky-2.0.0.pre3 lib/picky/internals/query/tokens.rb
picky-2.0.0.pre2 lib/picky/internals/query/tokens.rb
picky-2.0.0.pre1 lib/picky/internals/query/tokens.rb
picky-1.5.4 lib/picky/internals/query/tokens.rb
picky-1.5.3 lib/picky/internals/query/tokens.rb
picky-1.5.2 lib/picky/internals/query/tokens.rb
picky-1.5.1 lib/picky/internals/query/tokens.rb
picky-1.5.0 lib/picky/internals/query/tokens.rb
picky-1.4.3 lib/picky/internals/query/tokens.rb
picky-1.4.2 lib/picky/internals/query/tokens.rb