Sha256: 590461b20e578daf6244ae61c53f701f268f5ebeab371740843a2f4787e8529a

Contents?: true

Size: 1.74 KB

Versions: 28

Compression:

Stored size: 1.74 KB

Contents

module Picky

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

    attr_reader :indexes,
                :index_mapping

    forward :size, :each, :map, :to => :indexes
    each_forward :reindex, :to => :indexes
    instance_forward :clear,
                     :clear_indexes,
                     :register,
                     :reindex,
                     :[],
                     :to_s,
                     :size,
                     :each,
                     :each_category

    def initialize *indexes
      clear_indexes
      indexes.each { |index| register index }
    end

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

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

    # Registers an index with the indexes.
    #
    def register index
      # TODO Do not store duplicate indexes.
      #
      # self.indexes.delete_if { |existing| existing.name == index.name }
      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.intern
      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

28 entries across 28 versions & 1 rubygems

Version Path
picky-4.27.0 lib/picky/indexes.rb
picky-4.26.2 lib/picky/indexes.rb
picky-4.26.1 lib/picky/indexes.rb
picky-4.26.0 lib/picky/indexes.rb
picky-4.25.3 lib/picky/indexes.rb
picky-4.25.2 lib/picky/indexes.rb
picky-4.25.1 lib/picky/indexes.rb
picky-4.25.0 lib/picky/indexes.rb
picky-4.24.0 lib/picky/indexes.rb
picky-4.23.2 lib/picky/indexes.rb
picky-4.23.1 lib/picky/indexes.rb
picky-4.23.0 lib/picky/indexes.rb
picky-4.22.0 lib/picky/indexes.rb
picky-4.21.2 lib/picky/indexes.rb
picky-4.21.1 lib/picky/indexes.rb
picky-4.21.0 lib/picky/indexes.rb
picky-4.20.2 lib/picky/indexes.rb
picky-4.20.1 lib/picky/indexes.rb
picky-4.20.0 lib/picky/indexes.rb
picky-4.19.7 lib/picky/indexes.rb