From 4d7f06a6beacea3463ab190b6ba0c68d031a199a Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sun, 31 May 2009 22:16:01 -0400 Subject: [PATCH] Implemented that index fields can also be specified with symbols. --- lib/mongo/db.rb | 6 +++--- tests/test_db_api.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 8550dcf..d8a5519 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -460,10 +460,10 @@ module XGen # enforce a uniqueness constraint. def create_index(collection_name, field_or_spec, unique=false) field_h = OrderedHash.new - if field_or_spec.is_a? String - field_h[field_or_spec] = 1 + if field_or_spec.is_a?(String) || field_or_spec.is_a?(Symbol) + field_h[field_or_spec.to_s] = 1 else - field_or_spec.each { |f| field_h[f[0]] = f[1] } + field_or_spec.each { |f| field_h[f[0].to_s] = f[1] } end name = gen_index_name(field_h) sel = { diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 3507401..55cafd4 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -294,6 +294,20 @@ class DBAPITest < Test::Unit::TestCase ensure @@db.drop_index(@@coll.name, name) end + + def test_index_create_with_symbol + name = @@db.create_index(@@coll.name, :a) + list = @@db.index_information(@@coll.name) + assert_equal @@coll.index_information, list + assert_equal 2, list.length + + info = list[1] + assert_equal name, 'a_1' + assert_equal name, info[:name] + assert_equal 1, info[:keys]['a'] + ensure + @@db.drop_index(@@coll.name, name) + end def test_multiple_index_cols name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]]) @@ -308,6 +322,20 @@ class DBAPITest < Test::Unit::TestCase ensure @@db.drop_index(@@coll.name, name) end + + def test_multiple_index_cols_with_symbols + name = @@db.create_index(@@coll.name, [[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]]) + list = @@db.index_information(@@coll.name) + assert_equal 2, list.length + + info = list[1] + assert_equal name, 'a_-1_b_1_c_-1' + assert_equal name, info[:name] + keys = info[:keys].keys + assert_equal ['a', 'b', 'c'], keys.sort + ensure + @@db.drop_index(@@coll.name, name) + end def test_unique_index @@db.drop_collection("blah")