Sha256: ed3b57e6f97a0851376a09590b9cd44e6fe41e28d2fd0939c46f6b16d669570e

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

module Elastics
  class Tasks

    attr_reader :options

    def initialize(overrides={})
      options = Elastics::Utils.env2options *default_options.keys
      options[:index] = options[:index].split(',') if options[:index]
      @options = default_options.merge(options).merge(overrides)
    end

    def default_options
      @default_options ||= { :force          => false,
                             :index          => Conf.variables[:index],
                             :config_file    => Conf.config_file }
    end

    def create_indices
      indices.each do |index|
        delete_index(index) if options[:force]
        raise ExistingIndexError, "#{index.inspect} already exists. Please use FORCE=1 if you want to delete it first." \
              if exist?(index)
        create(index)
      end
    end

    def delete_indices
      indices.each { |index| delete_index(index) }
    end

    def config_hash
      @config_hash ||= ( hash = YAML.load(Utils.erb_process(config_path))
                         Utils.delete_allcaps_keys(hash) )
    end

  private

    def indices
      i = options[:index] || config_hash.keys
      i.is_a?(Array) ? i : [i]
    end

    def exist?(index)
      Elastics.exist?(:index => index)
    end

    def config_path
      @config_path ||= options[:config_file] || Conf.config_file
    end

    def delete_index(index)
      Elastics.delete_index(:index => index) if exist?(index)
    end

    def create(index)
      config_hash[index] = {} unless config_hash.has_key?(index)
      Elastics.POST "/#{index}", config_hash[index]
    end

  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
elastics-client-1.1.0 lib/elastics/tasks.rb
elastics-client-1.0.10 lib/elastics/tasks.rb
elastics-client-1.0.9 lib/elastics/tasks.rb
elastics-client-1.0.8 lib/elastics/tasks.rb
elastics-client-1.0.7 lib/elastics/tasks.rb
elastics-client-1.0.6 lib/elastics/tasks.rb
elastics-client-1.0.5 lib/elastics/tasks.rb
elastics-client-1.0.4 lib/elastics/tasks.rb