Module: Elastics::AutoRefresh

Defined in:
lib/elastics/auto_refresh.rb

Constant Summary collapse

METHODS =
%w(put post patch delete)
SKIP_IDS =
%w(_refresh _search)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disable!(&block) ⇒ Object



29
30
31
# File 'lib/elastics/auto_refresh.rb', line 29

def disable!(&block)
  use(false, &block)
end

.enable!(&block) ⇒ Object



25
26
27
# File 'lib/elastics/auto_refresh.rb', line 25

def enable!(&block)
  use(true, &block)
end

.enabled=(value) ⇒ Object



11
12
13
14
15
# File 'lib/elastics/auto_refresh.rb', line 11

def enabled=(value)
  value = !!value
  Client.send(:include, self) if value && !Client.include?(self)
  Thread.current[:elastics_test_mode] = value
end

.enabled?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/elastics/auto_refresh.rb', line 7

def enabled?
  Thread.current[:elastics_test_mode]
end

.included(base) ⇒ Object



33
34
35
36
# File 'lib/elastics/auto_refresh.rb', line 33

def included(base)
  base.send :alias_method, :request_without_auto_refresh, :request
  base.send :alias_method, :request, :request_with_auto_refresh
end

.use(value) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/elastics/auto_refresh.rb', line 17

def use(value)
  old_value = enabled?
  self.enabled = value
  block_given? ? yield : value
ensure
  self.enabled = old_value if block_given?
end

Instance Method Details

#request_with_auto_refresh(params) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/elastics/auto_refresh.rb', line 39

def request_with_auto_refresh(params)
  request_without_auto_refresh(params).tap do
    next unless AutoRefresh.enabled?
    next if SKIP_IDS.include?(params[:id].to_s.downcase)
    next unless METHODS.include?(params[:method].to_s.downcase)
    refresh(params[:index])
  end
end