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

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 57 lines (46 sloc) 1.285 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
module Picky

  class Categories

    attr_reader :categories, :category_hash

    delegate :each,
             :first,
             :map,
             :to => :categories

    each_delegate :cache,
                  :dump,
                  :empty,
                  :index,
                  :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

    # Add the given category to the list of categories.
    #
    def << category
      categories << category
      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_s
      "#{self.class}(#{categories.join(', ')})"
    end

  end

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