Skip to content
This repository
tag: v3.0.0.pre4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 128 lines (110 sloc) 3.868 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
module Picky

  class Category

    attr_reader :name

    # Mandatory params:
    # * name: Category name to use as identifier and file names.
    # * index: Index to which this category is attached to.
    #
    # Options:
    # * partial: Partial::None.new, Partial::Substring.new(from:start_char, to:up_to_char) (defaults from:-3, to:-1)
    # * similarity: Similarity::None.new (default), Similarity::DoubleMetaphone.new(amount_of_similarly_linked_words)
    # * from: The source category identifier to take the data from.
    #
    # Advanced Options:
    # * source: Use if the category should use a different source.
    # * weights: Query::Weights.new( [:category1, :category2] => +2, ... )
    # * tokenizer: Use a subclass of Tokenizers::Base that implements #tokens_for and #empty_tokens.
    # * key_format: What this category's keys are formatted with (default is :to_i)
    #
    def initialize name, index, options = {}
      @name = name
      @index = index

      # Indexing.
      #
      @source = options[:source]
      @from = options[:from]
      @tokenizer = options[:tokenizer]
      @key_format = options[:key_format]
      @qualifiers = extract_qualifiers_from options

      weights = options[:weights] || Generators::Weights::Default
      partial = options[:partial] || Generators::Partial::Default
      similarity = options[:similarity] || Generators::Similarity::Default

      @indexing_exact = index.indexing_bundle_class.new :exact, self, weights, Generators::Partial::None.new, similarity, options
      @indexing_partial = index.indexing_bundle_class.new :partial, self, weights, partial, Generators::Similarity::None.new, options

      # Indexed.
      #
      @indexed_exact = index.indexed_bundle_class.new :exact, self, similarity
      if partial.use_exact_for_partial?
        @indexed_partial = @indexed_exact
      else
        @indexed_partial = index.indexed_bundle_class.new :partial, self, similarity
      end

      # @exact = exact_lambda.call(@exact, @partial) if exact_lambda = options[:exact_lambda]
      # @partial = partial_lambda.call(@exact, @partial) if partial_lambda = options[:partial_lambda]
    end

    # Indexes and reloads the category.
    #
    def reindex
      index
      reload
    end

    # Index name.
    #
    def index_name
      @index.name
    end

    # Returns the qualifiers if set or
    # just the name if not.
    #
    def qualifiers
      @qualifiers || [name]
    end
    # Extract qualifiers from the options.
    #
    def extract_qualifiers_from options
      options[:qualifiers] || options[:qualifier] && [options[:qualifier]]
    end

    # The category itself just yields itself.
    #
    def each_category
      yield self
    end

    # Path and partial filename of the prepared index on this category.
    #
    def prepared_index_path
      @prepared_index_path ||= "#{index_directory}/prepared_#{name}_index"
    end
    # Get an opened index file.
    #
    # Note: If you don't use it with the block, do not forget to close it.
    #
    def prepared_index_file &block
      @prepared_index_file ||= Backend::File::Text.new prepared_index_path
      @prepared_index_file.open &block
    end
    # Creates the index directory including all necessary paths above it.
    #
    # Note: Interface method called by any indexers.
    #
    def prepare_index_directory
      FileUtils.mkdir_p index_directory
    end

    # The index directory for this category.
    #
    # TODO Push down into files?
    #
    def index_directory
      @index_directory ||= "#{PICKY_ROOT}/index/#{PICKY_ENVIRONMENT}/#{@index.name}"
    end

    # Identifier for technical output.
    #
    def identifier
      "#{@index.identifier}:#{name}"
    end

    #
    #
    def to_s
      "#{self.class}(#{identifier})"
    end

  end

end
Something went wrong with that request. Please try again.