diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index f99087c..2074aee 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -538,7 +538,7 @@ module Mongo result = @db.command(group_command) - if result["ok"] == 1 + if Mongo::Support.ok?(result) result["retval"] else raise OperationFailure, "group command failed: #{result['errmsg']}" diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 6236392..41bcade 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -423,7 +423,8 @@ module Mongo # If we're connected to master, set the @host and @port result = self['admin'].command({:ismaster => 1}, :check_response => false, :sock => socket) - if result['ok'] == 1 && ((is_master = result['ismaster'] == 1) || @slave_ok) + if Mongo::Support.ok?(result) && + ((is_master = result['ismaster'] == 1) || @slave_ok) @host, @port = host, port apply_saved_authentication end diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index a0284c6..41d5962 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -103,7 +103,7 @@ module Mongo "query", @selector, "fields", @fields] response = @db.command(command) - return response['n'].to_i if response['ok'] == 1 + return response['n'].to_i if Mongo::Support.ok?(response) return 0 if response['errmsg'] == "ns missing" raise OperationFailure, "Count failed: #{response['errmsg']}" end diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 0cdc006..e9aea98 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -217,8 +217,7 @@ module Mongo oh = BSON::OrderedHash.new oh[:create] = name doc = command(oh.merge(options || {})) - ok = doc['ok'] - return Collection.new(self, name, @pk_factory) if ok.kind_of?(Numeric) && (ok.to_i == 1 || ok.to_i == 0) + return Collection.new(self, name, @pk_factory) if ok?(doc) raise MongoDBError, "Error creating collection: #{doc.inspect}" end @@ -427,8 +426,7 @@ module Mongo # # @return [Boolean] def ok?(doc) - ok = doc['ok'] - ok.kind_of?(Numeric) && ok.to_i == 1 + Mongo::Support.ok?(doc) end # Send a command to the database. diff --git a/lib/mongo/util/support.rb b/lib/mongo/util/support.rb index 7ce51a0..d8fa769 100644 --- a/lib/mongo/util/support.rb +++ b/lib/mongo/util/support.rb @@ -68,5 +68,15 @@ module Mongo "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]" end end + + # Determine if a database command has succeeded by + # checking the document response. + # + # @param [Hash] doc + # + # @return [Boolean] true if the 'ok' key is either 1 or *true*. + def ok?(doc) + doc['ok'] == 1.0 || doc['ok'] == true + end end end diff --git a/test/connection_test.rb b/test/connection_test.rb index 238faf0..3816616 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -21,7 +21,7 @@ class TestConnection < Test::Unit::TestCase def test_server_info server_info = @mongo.server_info assert server_info.keys.include?("version") - assert_equal 1.0, server_info["ok"] + assert Mongo::Support.ok?(server_info) end def test_server_version @@ -71,7 +71,7 @@ class TestConnection < Test::Unit::TestCase end result = @mongo.copy_database('old', 'new', 'localhost', 'bob', 'secret') - assert result['ok'].to_i == 1 + assert Mongo::Support.ok?(result) @mongo.drop_database('old') @mongo.drop_database('new') diff --git a/test/db_test.rb b/test/db_test.rb index 7667d1b..1258c30 100644 --- a/test/db_test.rb +++ b/test/db_test.rb @@ -158,7 +158,7 @@ class DBTest < Test::Unit::TestCase end result = @@db.command({:non_command => 1}, :check_response => false) - assert_equal 0, result['ok'].to_i + assert !Mongo::Support.ok?(result) end def test_error diff --git a/test/unit/db_test.rb b/test/unit/db_test.rb index b262589..5a69deb 100644 --- a/test/unit/db_test.rb +++ b/test/unit/db_test.rb @@ -62,7 +62,7 @@ class DBTest < Test::Unit::TestCase should "raise an error if collection creation fails" do @db.expects(:collection_names).returns([]) - @db.expects(:command).returns({}) + @db.expects(:command).returns({'ok' => 0}) assert_raise Mongo::MongoDBError do @db.create_collection("foo") end