Skip to content

Commit d7684b7

Browse files
committed
Move duplicate-key method to Minidoc
We're going to remove the Indexes module.
1 parent 6e21a08 commit d7684b7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: lib/minidoc.rb

+14
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ def self.transaction(isolation = "mvcc")
103103
end
104104
end
105105

106+
# Rescue a duplicate key exception in the given block. Returns the result of
107+
# the block, or +false+ if the exception was raised.
108+
def self.rescue_duplicate_key_errors
109+
yield
110+
rescue Minidoc::DuplicateKey
111+
false
112+
rescue Mongo::OperationFailure => ex
113+
if Minidoc::DuplicateKey.duplicate_key_exception(ex)
114+
false
115+
else
116+
raise
117+
end
118+
end
119+
106120
def self.tokumx?
107121
@server_info ||= connection.server_info
108122
@server_info.key?("tokumxVersion")

Diff for: test/duplicate_key_test.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require File.expand_path('../helper', __FILE__)
2+
3+
class DuplicateKeyTest < Minidoc::TestCase
4+
def test_rescue_duplicate_key_errors_result
5+
user = User.create!
6+
result = Minidoc.rescue_duplicate_key_errors do
7+
User.create!(name: "two")
8+
end
9+
10+
assert_equal "two", result.name
11+
end
12+
13+
def test_rescue_duplicate_key_errors_false
14+
user = User.create!
15+
result = Minidoc.rescue_duplicate_key_errors do
16+
User.create!(_id: user.id, name: "two")
17+
end
18+
19+
assert_equal false, result
20+
end
21+
end

0 commit comments

Comments
 (0)