Sha256: 5c604245a12adc019ca163bb799e3fbc39923e986a99a5c5cd15660c45c822ab

Contents?: true

Size: 1.61 KB

Versions: 30

Compression:

Stored size: 1.61 KB

Contents

module Picky

  # Holds all indexes and provides operations
  # for extracting and working on them.
  #
  # Delegates a number of operations to the
  # indexes.
  #
  class Indexes

    attr_reader :indexes,
                :index_mapping

    delegate :size,
             :each,
             :to => :indexes

    each_delegate :reindex,
                  :each_category,
                  :to => :indexes

    def initialize
      clear_indexes
    end

    # Return the Indexes instance.
    #
    def self.instance
      @instance ||= new
    end
    def self.identifier
      name
    end

    instance_delegate :clear,
                      :register,
                      :reindex,
                      :[],
                      :to_s,
                      :size,
                      :each,
                      :each_category

    # Clears the indexes and the mapping.
    #
    def clear_indexes
      @indexes       = []
      @index_mapping = {}
    end

    # Registers an index with the indexes.
    #
    def register index
      self.indexes << index
      self.index_mapping[index.name] = index
    end
    def self.register index
      self.instance.register index
    end

    # Extracts an index, given its identifier.
    #
    def [] identifier
      index_name = identifier.to_sym
      index_mapping[index_name] || raise_not_found(index_name)
    end

    # Raises a not found for the index.
    #
    def raise_not_found index_name
      raise %Q{Index "#{index_name}" not found. Possible indexes: "#{indexes.map(&:name).join('", "')}".}
    end

    #
    #
    def to_s
      indexes.indented_to_s
    end

  end

end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
picky-3.4.3 lib/picky/indexes.rb
picky-3.4.2 lib/picky/indexes.rb
picky-3.4.1 lib/picky/indexes.rb
picky-3.4.0 lib/picky/indexes.rb
picky-3.3.3 lib/picky/indexes.rb
picky-3.3.2 lib/picky/indexes.rb
picky-3.3.1 lib/picky/indexes.rb
picky-3.3.0 lib/picky/indexes.rb
picky-3.2.0 lib/picky/indexes.rb
picky-3.1.13 lib/picky/indexes.rb
picky-3.1.12 lib/picky/indexes.rb
picky-3.1.11 lib/picky/indexes.rb
picky-3.1.10 lib/picky/indexes.rb
picky-3.1.9 lib/picky/indexes.rb
picky-3.1.8 lib/picky/indexes.rb
picky-3.1.7 lib/picky/indexes.rb
picky-3.1.6 lib/picky/indexes.rb
picky-3.1.5 lib/picky/indexes.rb
picky-3.1.4 lib/picky/indexes.rb
picky-3.1.3 lib/picky/indexes.rb