Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added sort and filter to collection.

  • Loading branch information...
commit 3dd81780be83d7f1bce00482eedd1e7ec8982475 1 parent 587da3b
@jnunemaker jnunemaker authored
Showing with 38 additions and 9 deletions.
  1. +13 −0 lib/plucky/collection.rb
  2. +25 −9 test/test_collection.rb
View
13 lib/plucky/collection.rb
@@ -1,9 +1,22 @@
module Plucky
class Collection
+ def self.query_delegator(*methods)
+ methods.each do |method|
+ class_eval <<-EOC
+ def #{method}(*args)
+ query.#{method}(*args)
+ self
+ end
+ EOC
+ end
+ end
+
def initialize(collection)
@collection = collection
end
+ query_delegator :sort, :filter
+
def all(options={})
query.update(options)
[].tap do |docs|
View
34 test/test_collection.rb
@@ -5,29 +5,32 @@ class CollectionTest < Test::Unit::TestCase
include Plucky
setup do
- @john = oh(['_id', 'john'], ['age', 28], ['name', 'John'])
- @steve = oh(['_id', 'steve'], ['age', 29], ['name', 'Steve'])
+ @chris = oh(['_id', 'chris'], ['age', 26], ['name', 'Chris'])
+ @john = oh(['_id', 'john'], ['age', 28], ['name', 'John'])
+ @steve = oh(['_id', 'steve'], ['age', 29], ['name', 'Steve'])
@collection = Collection.new(DB['users'])
@collection.insert(:_id => 'john', :name => 'John', :age => 28)
@collection.insert(:_id => 'steve', :name => 'Steve', :age => 29)
+ @collection.insert(:_id => 'chris', :name => 'Chris', :age => 26)
end
context "#all" do
should "work with no arguments" do
docs = @collection.all
- docs.size.should == 2
+ docs.size.should == 3
docs.should include(@john)
docs.should include(@steve)
+ docs.should include(@chris)
end
should "work with and normalize criteria" do
- docs = @collection.all(:id.ne => 'john')
+ docs = @collection.all(:id.in => ['steve'])
docs.should == [@steve]
end
should "work with and normalize options" do
docs = @collection.all(:order => :name.asc)
- docs.should == [@john, @steve]
+ docs.should == [@chris, @john, @steve]
end
end
@@ -47,28 +50,41 @@ class CollectionTest < Test::Unit::TestCase
end
should "work with and normalize options" do
- @collection.last(:age.lte => 29, :order => :name.desc).should == @john
+ @collection.last(:age.lte => 26, :order => :name.desc).should == @chris
end
end
context "#count" do
should "work with no arguments" do
- @collection.count.should == 2
+ @collection.count.should == 3
end
should "work with and normalize criteria" do
- @collection.count(:age.lte => 28).should == 1
+ @collection.count(:age.lte => 28).should == 2
end
end
context "#delete" do
should "work with no arguments" do
- lambda { @collection.delete }.should change { @collection.count }.by(2)
+ lambda { @collection.delete }.should change { @collection.count }.by(3)
end
should "work with and normalize criteria" do
lambda { @collection.delete(:age.lte => 28) }.should change { @collection.count }
end
end
+
+ context "#sort" do
+ should "change the sort order of the documents" 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
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.