Module: Minidoc::Indexes::ClassMethods

Defined in:
lib/minidoc/indexes.rb

Constant Summary collapse

MONGO_DUPLICATE_KEY_ERROR_CODE =
11000

Instance Method Summary collapse

Instance Method Details

#ensure_index(key_or_keys, options = {}) ⇒ Object



9
10
11
12
13
# File 'lib/minidoc/indexes.rb', line 9

def ensure_index(key_or_keys, options = {})
  indexes = Array(key_or_keys).map { |key| { key => 1 } }.reduce(:merge)

  collection.ensure_index(indexes, options)
end

#rescue_duplicate_key_errorsObject

Rescue a Mongo::OperationFailure exception which originated from duplicate key error (error code 11000) in the given block.

Returns the status of the operation in the given block, or false if the exception was raised.



20
21
22
23
24
25
26
27
28
29
# File 'lib/minidoc/indexes.rb', line 20

def rescue_duplicate_key_errors
  yield
rescue Mongo::OperationFailure => exception
  if exception.respond_to?(:error_code) &&
    exception.error_code == MONGO_DUPLICATE_KEY_ERROR_CODE
    return false
  else
    raise
  end
end