Permalink
Browse files

Beefed up sort a bit and added Collection#reverse.

  • Loading branch information...
1 parent 15c622f commit 48b5464403f92eb8984900e923d851c4704e7f73 @jnunemaker jnunemaker committed May 3, 2010
Showing with 20 additions and 12 deletions.
  1. +1 −1 lib/plucky/collection.rb
  2. +8 −6 lib/plucky/query.rb
  3. +10 −4 test/test_collection.rb
  4. +1 −1 test/test_query.rb
View
@@ -15,7 +15,7 @@ def initialize(collection)
@collection = collection
end
- query_delegator :fields, :filter, :limit, :skip, :sort
+ query_delegator :fields, :filter, :limit, :reverse, :skip, :sort
def all(options={})
query.update(options)
View
@@ -117,18 +117,20 @@ def normalized_sort(sort)
when Array
sort.map do |s|
case s
- when SymbolOperator
- normalized_order(s.field, s.operator)
- when Array
- s
- else
- [s.to_s, 1]
+ when SymbolOperator
+ normalized_order(s.field, s.operator)
+ when Array
+ s
+ else
+ [s.to_s, 1]
end
end
when SymbolOperator
[normalized_order(sort.field, sort.operator)]
when String
sort.split(',').map { |str| normalized_order(*str.strip.split(' ')) }
+ when Symbol
+ [[sort, 1]]
else
sort
end
View
@@ -73,20 +73,20 @@ class CollectionTest < Test::Unit::TestCase
lambda { @collection.delete(:age.lte => 28) }.should change { @collection.count }
end
end
-
+
context "#sort" do
should "work" do
@collection.sort(:age).all.should == [@chris, @john, @steve]
@collection.sort(:age.desc).all.should == [@steve, @john, @chris]
end
end
-
+
context "#filter" do
should "work" do
@collection.filter(:age.lt => 29).filter(:name => 'Chris').all.should == [@chris]
end
end
-
+
context "#skip" do
should "work" do
@collection.skip(2).all(:order => :age).should == [@steve]
@@ -98,11 +98,17 @@ class CollectionTest < Test::Unit::TestCase
@collection.limit(2).all(:order => :age).should == [@chris, @john]
end
end
-
+
context "#fields" do
should "work" do
@collection.fields(:name).first(:id => 'john').keys.should == ['_id', 'name']
end
end
+
+ context "#reverse" do
+ should "work" do
+ @collection.sort(:age).reverse.all.should == [@steve, @john, @chris]
+ end
+ end
end
end
View
@@ -222,7 +222,7 @@ class QueryTest < Test::Unit::TestCase
end
should "work with just a symbol" do
- Query.new.sort(:foo).options[:sort].should == :foo
+ Query.new.sort(:foo).options[:sort].should == [[:foo, 1]]
end
should "work with multiple symbols" do

0 comments on commit 48b5464

Please sign in to comment.