Skip to content

Commit f41b48a

Browse files
author
Mike Dirolf
committed
minor: tweaks and some more test cases
1 parent 1b98a19 commit f41b48a

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

lib/mongo/cursor.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def count
8484
# This method overrides any sort order specified in the Collection#find
8585
# method, and only the last sort applied has an effect
8686
def sort(order)
87-
raise InvalidOperation, "can't call Cursor#sort on a used cursor" if @query_run
87+
check_modifiable
8888
@query.order_by = order
8989
self
9090
end
@@ -100,7 +100,7 @@ def limit(number_to_return)
100100
raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer
101101

102102
@query.number_to_return = number_to_return
103-
return self
103+
self
104104
end
105105

106106
# Skips the first +number_to_skip+ results of this cursor.
@@ -114,7 +114,7 @@ def skip(number_to_skip)
114114
raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer
115115

116116
@query.number_to_skip = number_to_skip
117-
return self
117+
self
118118
end
119119

120120
# Iterate over each document in this cursor, yielding it to the given

test/test_cursor.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,27 @@ def test_count
5757

5858
assert_equal 0, @@db['acollectionthatdoesn'].count()
5959
end
60-
60+
6161
def test_sort
6262
@@coll.clear
63-
5.times{|x| @@coll.insert({"a" => x, "b" => 5-x}) }
64-
65-
assert_kind_of Cursor, @@coll.find().sort({:a=>1})
66-
63+
5.times{|x| @@coll.insert({"a" => x}) }
64+
65+
assert_kind_of Cursor, @@coll.find().sort({:a => 1})
66+
6767
assert_equal 0, @@coll.find().sort({:a => 1}).next_object["a"]
6868
assert_equal 4, @@coll.find().sort({:a => -1}).next_object["a"]
69-
70-
assert_equal 1, @@coll.find().sort({:a => -1, :b => 1}).next_object["b"]
71-
assert_equal 5, @@coll.find().sort({:a => 1, :b => -1}).next_object["b"]
72-
69+
assert_equal 0, @@coll.find().sort(["a"]).next_object["a"]
70+
71+
assert_kind_of Cursor, @@coll.find().sort({:a => -1, :b => 1})
72+
7373
assert_equal 4, @@coll.find().sort({:a => 1}).sort({:a => -1}).next_object["a"]
7474
assert_equal 0, @@coll.find().sort({:a => -1}).sort({:a => 1}).next_object["a"]
75-
75+
76+
cursor = @@coll.find()
77+
cursor.next_object()
78+
assert_raise InvalidOperation do
79+
cursor.sort(["a"])
80+
end
7681
end
7782

7883
def test_limit

0 commit comments

Comments
 (0)