Sha256: 3b252dc9b3cd2b0b8dcdc666b4ad272f49e44de5afbb52ac874e6fff1687f3dc

Contents?: true

Size: 1.9 KB

Versions: 6

Compression:

Stored size: 1.9 KB

Contents

module Picky

  class Categories

    attr_reader :categories, :category_hash

    forward :each,
            :first,
            :map,
            :map!,
            :include?,
            :to => :categories

    each_forward :cache,
                 :dump,
                 :empty,
                 :inject,
                 :reindex,
                 :reset_backend,
                 :to => :categories

    # A list of indexed categories.
    #
    def initialize options = {}
      clear_categories
    end

    # Clears both the array of categories and the hash of categories.
    #
    def clear_categories
      @categories    = []
      @category_hash = {}
    end
    
    # Updates the qualifier ("qualifier:searchterm") mapping.
    #
    # Example:
    #   You dynamically add a new category to an index.
    #   To add the qualifiers to a search, you call this
    #   method.
    #
    def qualifier_mapper
      @qualifier_mapper ||= QualifierMapper.new self
    end
    
    # Resets the qualifier mapper used.
    #
    def reset_qualifier_mapper
      @qualifier_mapper = nil
    end

    # Add the given category to the list of categories.
    #
    def << category
      reset_qualifier_mapper # TODO Have an add method on QualifierMapper?
      categories << category unless categories.include? category # This is wrong, and needs to be handled in index.rb
      category_hash[category.name] = category
    end

    # Find a given category in the categories.
    #
    def [] category_name
      category_name = category_name.intern
      category_hash[category_name] || raise_not_found(category_name)
    end
    def raise_not_found category_name
      raise %Q{Index category "#{category_name}" not found. Possible categories: "#{categories.map(&:name).join('", "')}".}
    end
    
    def to_stats
      map(&:name).join(', ')
    end

    def to_s
      "#{self.class}(#{categories.join(', ')})"
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
picky-4.12.13 lib/picky/categories.rb
picky-4.12.12 lib/picky/categories.rb
picky-4.12.11 lib/picky/categories.rb
picky-4.12.10 lib/picky/categories.rb
picky-4.12.8 lib/picky/categories.rb
picky-4.12.7 lib/picky/categories.rb