Sha256: 3aab8f5d126b1ccab41e54c9428c3e3f8c607162ba1c310eddeb8aa33cf4b5a5

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module TireAsyncIndex
  module Workers

    # Worker for updating ElasticSearch index
    class UpdateIndex

      # Update or delete ElasticSearch index based on the action_type parameter.
      #
      # Parameters:
      #   action_type - Determine index direction. (allowed value - Update or Delete)
      #   class_name - Model class name
      #   id - Document id
      #

      def process(action_type, class_name, id)
        klass = find_class_const(class_name)

        case action_type.to_sym
          when :update
            object = klass.send(get_finder_method(klass), id)

            if object.present? && object.respond_to?(:tire)
              object.tire.update_index
            end
          when :delete

            klass.new.tap do |inst|
              inst.tire.index.remove(inst.tire.document_type, { _id: id })
            end
        end
      end

      def find_class_const(class_name)
        if defined?(RUBY_VERSION) && RUBY_VERSION.match(/^2\./)
          Kernel.const_get(class_name)
        else
          class_name.split('::').reduce(Object) do |mod, const_name|
            mod.const_get(const_name)
          end
        end
      end

      def get_finder_method(klass)
        klass.respond_to?(:tire_async_finder) ? :tire_async_finder : :find
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tire_async_index-0.3.0 lib/tire_async_index/workers/update_index.rb