class ElasticRecord::Index::Deferred::DeferredConnection

Constants

READ_METHODS

Attributes

bulk_actions[RW]
deferred_actions[RW]
index[RW]
writes_made[RW]

Public Class Methods

new(index) click to toggle source
# File lib/elastic_record/index/deferred.rb, line 16
def initialize(index)
  @index = index
  @bulk_actions = nil
  reset!
end

Public Instance Methods

reset!() click to toggle source
# File lib/elastic_record/index/deferred.rb, line 22
def reset!
  if writes_made
    begin
      index.disable_deferring!
      index.refresh
      index.delete_by_query query: {match_all: {}}
    ensure
      index.enable_deferring!
    end
  end
  self.deferred_actions = []
  self.writes_made = false
end

Private Instance Methods

flush_deferred_actions!() click to toggle source
# File lib/elastic_record/index/deferred.rb, line 53
def flush_deferred_actions!
  deferred_actions.each do |queued_action|
    self.writes_made = true
    queued_action.run(index.real_connection)
  end
  deferred_actions.clear
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/elastic_record/index/deferred.rb, line 38
def method_missing(method, *args, &block)
  super unless index.real_connection.respond_to?(method)

  if READ_METHODS.include?(method)
    flush_deferred_actions!
    if method == :json_get && args.first =~ /^\/(.*)\/_m?search/
      index.real_connection.json_post("/#{$1.partition('/').first}/_refresh")
    end

    index.real_connection.send(method, *args, &block)
  else
    deferred_actions << DeferredAction.new(method, args, block)
  end
end