Class: Chewy::Strategy

Inherits:
Object show all
Defined in:
lib/chewy/strategy.rb,
lib/chewy/strategy/base.rb,
lib/chewy/strategy/atomic.rb,
lib/chewy/strategy/bypass.rb,
lib/chewy/strategy/resque.rb,
lib/chewy/strategy/urgent.rb,
lib/chewy/strategy/sidekiq.rb,
lib/chewy/strategy/shoryuken.rb,
lib/chewy/strategy/active_job.rb

Overview

This class represents strategies stack with :base Strategy on top of it. This causes raising exceptions on every index update attempt, so other strategy must be choosen.

User.first.save # Raises UndefinedUpdateStrategy exception

Chewy.strategy(:atomic) do User.last.save # Save user according to the :atomic strategy rules end

Defined Under Namespace

Classes: ActiveJob, Atomic, Base, Bypass, Resque, Shoryuken, Sidekiq, Urgent

Instance Method Summary collapse

Constructor Details

#initializeStrategy

Returns a new instance of Strategy.


47
48
49
# File 'lib/chewy/strategy.rb', line 47

def initialize
  @stack = [resolve(Chewy.root_strategy).new]
end

Instance Method Details

#currentObject


51
52
53
# File 'lib/chewy/strategy.rb', line 51

def current
  @stack.last
end

#popObject


61
62
63
64
65
66
# File 'lib/chewy/strategy.rb', line 61

def pop
  raise "Can't pop root strategy" if @stack.one?
  result = @stack.pop.tap(&:leave)
  debug "[#{@stack.size}] -> #{result.name}, now #{current.name}" if @stack.size > 1
  result
end

#push(name) ⇒ Object


55
56
57
58
59
# File 'lib/chewy/strategy.rb', line 55

def push(name)
  result = @stack.push resolve(name).new
  debug "[#{@stack.size - 1}] <- #{current.name}" if @stack.size > 2
  result
end

#wrap(name) ⇒ Object


68
69
70
71
72
73
# File 'lib/chewy/strategy.rb', line 68

def wrap(name)
  stack = push(name)
  yield
ensure
  pop if stack
end