Module: Elastictastic::OptimisticLocking::ClassMethods

Defined in:
lib/elastictastic/optimistic_locking.rb

Instance Method Summary collapse

Instance Method Details

#create_or_update(id, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/elastictastic/optimistic_locking.rb', line 9

def create_or_update(id, &block)
  scope = current_scope
  new.tap do |instance|
    instance.id = id
    yield instance
  end.create do |e|
    case e
    when nil # chill
    when Elastictastic::ServerError::DocumentAlreadyExistsEngineException,
      Elastictastic::ServerError::DocumentAlreadyExistsException # 0.19+
      scope.update(id, &block)
    else
      raise e
    end
  end
rescue Elastictastic::CancelSave
  # Do Nothing
end

#update(id, &block) ⇒ Object



28
29
30
31
# File 'lib/elastictastic/optimistic_locking.rb', line 28

def update(id, &block)
  instance = scoped({}).find_one(id, :preference => '_primary_first')
  instance.try_update(current_scope, &block) if instance
end

#update_each(&block) ⇒ Object



33
34
35
# File 'lib/elastictastic/optimistic_locking.rb', line 33

def update_each(&block)
  all.each { |instance| instance.try_update(current_scope, &block) }
end