From a698415fa50053970f4c6a116a0f358db4e31e90 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Mon, 28 Dec 2009 13:43:20 -0500 Subject: [PATCH] minor: fixed sort api to allow a single key / direction pair to be specified as an array --- lib/mongo/util/conversions.rb | 16 +++++++++++---- test/test_cursor.rb | 37 +++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/lib/mongo/util/conversions.rb b/lib/mongo/util/conversions.rb index 971d7cb..8e807db 100644 --- a/lib/mongo/util/conversions.rb +++ b/lib/mongo/util/conversions.rb @@ -32,11 +32,19 @@ module Mongo #:nodoc: # { "field1" => 1, "field2" => -1} def array_as_sort_parameters(value) order_by = OrderedHash.new - value.each do |param| - if (param.class.name == "String") - order_by[param] = 1 + if value.first.is_a? Array + value.each do |param| + if (param.class.name == "String") + order_by[param] = 1 + else + order_by[param[0]] = sort_value(param[1]) unless param[1].nil? + end + end + else + if order_by.size == 1 + order_by[value.first] = 1 else - order_by[param[0]] = sort_value(param[1]) unless param[1].nil? + order_by[value.first] = sort_value(value[1]) end end order_by diff --git a/test/test_cursor.rb b/test/test_cursor.rb index 198622e..e57c961 100644 --- a/test/test_cursor.rb +++ b/test/test_cursor.rb @@ -67,27 +67,30 @@ class CursorTest < Test::Unit::TestCase def test_sort @@coll.remove - 5.times{|x| @@coll.insert({"a" => x}) } + 5.times{|x| @@coll.insert({"age" => x}) } - assert_kind_of Cursor, @@coll.find().sort(:a, 1) + assert_kind_of Cursor, @@coll.find().sort(:age, 1) - assert_equal 0, @@coll.find().sort(:a, 1).next_document["a"] - assert_equal 4, @@coll.find().sort(:a, -1).next_document["a"] - assert_equal 0, @@coll.find().sort([["a", :asc]]).next_document["a"] + assert_equal 0, @@coll.find().sort(:age, 1).next_document["age"] + assert_equal 4, @@coll.find().sort(:age, -1).next_document["age"] + assert_equal 0, @@coll.find().sort([["age", :asc]]).next_document["age"] - assert_kind_of Cursor, @@coll.find().sort([[:a, -1], [:b, 1]]) + assert_kind_of Cursor, @@coll.find().sort([[:age, -1], [:b, 1]]) - assert_equal 4, @@coll.find().sort(:a, 1).sort(:a, -1).next_document["a"] - assert_equal 0, @@coll.find().sort(:a, -1).sort(:a, 1).next_document["a"] + assert_equal 4, @@coll.find().sort(:age, 1).sort(:age, -1).next_document["age"] + assert_equal 0, @@coll.find().sort(:age, -1).sort(:age, 1).next_document["age"] + + assert_equal 4, @@coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"] + assert_equal 0, @@coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"] cursor = @@coll.find() cursor.next_document assert_raise InvalidOperation do - cursor.sort(["a"]) + cursor.sort(["age"]) end assert_raise InvalidSortValueError do - @@coll.find().sort(:a, 25).next_document + @@coll.find().sort(:age, 25).next_document end assert_raise InvalidSortValueError do @@ -95,6 +98,20 @@ class CursorTest < Test::Unit::TestCase end end + def test_sort_date + @@coll.remove + 5.times{|x| @@coll.insert({"created_at" => Time.utc(2000 + x)}) } + + assert_equal 2000, @@coll.find().sort(:created_at, :asc).next_document["created_at"].year + assert_equal 2004, @@coll.find().sort(:created_at, :desc).next_document["created_at"].year + + assert_equal 2000, @@coll.find().sort([:created_at, :asc]).next_document["created_at"].year + assert_equal 2004, @@coll.find().sort([:created_at, :desc]).next_document["created_at"].year + + assert_equal 2000, @@coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year + assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year + end + def test_limit @@coll.remove