Sha256: 4dba87c8b698632cbee63d60223d13b5ad2721f09fb4bcfdff2083d26551e65a

Contents?: true

Size: 1.23 KB

Versions: 51

Compression:

Stored size: 1.23 KB

Contents

module Picky

  class Categories

    attr_reader :categories, :category_hash

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

    each_delegate :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

    # 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

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
picky-4.5.4 lib/picky/categories.rb
picky-4.5.3 lib/picky/categories.rb
picky-4.5.2 lib/picky/categories.rb
picky-4.5.1 lib/picky/categories.rb
picky-4.5.0 lib/picky/categories.rb
picky-4.4.2 lib/picky/categories.rb
picky-4.4.1 lib/picky/categories.rb
picky-4.4.0 lib/picky/categories.rb
picky-4.3.2 lib/picky/categories.rb
picky-4.3.1 lib/picky/categories.rb
picky-4.3.0 lib/picky/categories.rb
picky-4.2.4 lib/picky/categories.rb
picky-4.2.3 lib/picky/categories.rb
picky-4.2.2 lib/picky/categories.rb
picky-4.2.1 lib/picky/categories.rb
picky-4.2.0 lib/picky/categories.rb
picky-4.1.0 lib/picky/categories.rb
picky-4.0.9 lib/picky/categories.rb
picky-4.0.8 lib/picky/categories.rb
picky-4.0.7 lib/picky/categories.rb