Sha256: 2de9644c418cb6d2cf48b7f1a6d8c3ca2320cdcda928d303d716eb10f51d4432

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

module Elasticity
  class IndexConfig
    ATTRS = [:index_base_name, :document_type, :mapping, :strategy, :subclasses].freeze
    VALIDATABLE_ATTRS = [:index_base_name, :document_type, :strategy].freeze

    attr_accessor *ATTRS

    def initialize(elasticity_config, default_document_type)
      @document_type = default_document_type
      @elasticity_config = elasticity_config
      yield(self)
      validate!
    end

    def segment(name)
      new_config = self.dup
      new_config.index_base_name = "#{index_base_name}_#{name.underscore}"
      new_config
    end

    def client
      @elasticity_config.client
    end

    def definition
      return @definition if defined?(@definition)
      @definition = { settings: @elasticity_config.settings, mappings: { @document_type => @mapping || {} } }
      subclasses.each do |doc_type, subclass|
        @definition[:mappings][doc_type] = subclass.constantize.mapping
      end if subclasses.present?
      @definition
    end

    def document_types
      @document_types ||= definition[:mappings].collect { |doc_type, mapping| doc_type }
    end

    def fq_index_base_name
      return @fq_index_base_name if defined?(@fq_index_base_name)

      if namespace = @elasticity_config.namespace
        @fq_index_base_name = "#{namespace}_#{@index_base_name}"
      else
        @fq_index_base_name = @index_base_name
      end

      @fq_index_base_name
    end

    def strategy
      @strategy ||= Strategies::SingleIndex
    end

    private

    def validate!
      VALIDATABLE_ATTRS.each do |attr|
        raise "#{attr} is not set" if public_send(attr).nil?
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
es-elasticity-0.7.1 lib/elasticity/index_config.rb
es-elasticity-0.7.0 lib/elasticity/index_config.rb
es-elasticity-0.6.5 lib/elasticity/index_config.rb
es-elasticity-0.6.4 lib/elasticity/index_config.rb
es-elasticity-0.6.3 lib/elasticity/index_config.rb
es-elasticity-0.6.2 lib/elasticity/index_config.rb
es-elasticity-0.6.1 lib/elasticity/index_config.rb
es-elasticity-0.6.0 lib/elasticity/index_config.rb