Permalink
Browse files

Removed Collection. It just complicates things. Added #find, #find_on…

…e, #all, #first, #last, #remove, and #count to Query. Changed options(...) to update(...) so now options is just a reader for @options. Reordered the tests a bit.
  • Loading branch information...
1 parent 4af9837 commit 5fd3553bcbb3be088d6cfcd465c9db4f709fc201 @jnunemaker jnunemaker committed May 7, 2010
Showing with 479 additions and 514 deletions.
  1. +0 −60 lib/plucky/collection.rb
  2. +37 −8 lib/plucky/query.rb
  3. +0 −128 test/test_collection.rb
  4. +442 −318 test/test_query.rb
View
@@ -1,60 +0,0 @@
-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 :fields, :limit, :reverse, :skip, :sort, :where, :[]=
-
- def [](key)
- query[key]
- end
-
- def all(options={})
- query.options(options)
- [].tap do |docs|
- find(query.criteria, query.options).each { |doc| docs << doc }
- end
- end
-
- def first(options={})
- query.options(options)
- find_one(query.criteria, query.options)
- end
-
- def last(options={})
- query.options(options).reverse
- find_one(query.criteria, query.options)
- end
-
- def delete(options={})
- query.options(options)
- remove(query.criteria)
- end
-
- def count(options={})
- query.options(options)
- find(query.criteria, query.options).count
- end
-
- private
- def method_missing(method, *args, &block)
- @collection.send(method, *args, &block)
- end
-
- def query
- @query ||= Query.new
- end
- end
-end
View
@@ -7,16 +7,45 @@ class Query
:fields, :skip, :limit, :sort, :hint, :snapshot, :batch_size, :timeout # Ruby Driver
]
- attr_reader :criteria
+ attr_reader :criteria, :options, :collection
- def initialize(opts={})
- @options, @criteria, = {}, {}
- options(opts)
+ def initialize(collection, opts={})
+ @collection, @options, @criteria, = collection, {}, {}
+ update(opts)
end
- def options(opts=nil)
- return @options if opts.nil?
- separate_criteria_and_options(opts || {})
+ def find(opts={})
+ update(opts).collection.find(criteria, options)
+ end
+
+ def find_one(opts={})
+ update(opts).collection.find_one(criteria, options)
+ end
+
+ def all(opts={})
+ [].tap do |docs|
+ update(opts).find(criteria.merge(options)).each { |doc| docs << doc }
+ end
+ end
+
+ def first(opts={})
+ update(opts).find_one(criteria.merge(options))
+ end
+
+ def last(opts={})
+ update(opts).reverse.find_one(criteria.merge(options))
+ end
+
+ def remove(opts={})
+ update(opts).collection.remove(criteria)
+ end
+
+ def count(opts={})
+ update(opts).find(criteria.merge(options)).count
+ end
+
+ def update(opts={})
+ separate_criteria_and_options(opts)
self
end
@@ -59,7 +88,7 @@ def []=(key, value)
end
def merge(other)
- clone.options(other.options).where(other.criteria)
+ clone.update(other.options).where(other.criteria)
end
private
View
@@ -1,128 +0,0 @@
-require 'helper'
-
-class CollectionTest < Test::Unit::TestCase
- context "Plucky::Collection" do
- include Plucky
-
- setup do
- @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', :age => 28, :name => 'John')
- @collection.insert(:_id => 'steve', :age => 29, :name => 'Steve')
- @collection.insert(:_id => 'chris', :age => 26, :name => 'Chris')
- end
-
- context "#all" do
- should "work with no arguments" do
- docs = @collection.all
- 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.in => ['steve'])
- docs.should == [@steve]
- end
-
- should "work with and normalize options" do
- docs = @collection.all(:order => :name.asc)
- docs.should == [@chris, @john, @steve]
- end
- end
-
- context "#first" do
- should "work with and normalize criteria" do
- @collection.first(:age.lt => 29).should == @john
- end
-
- should "work with and normalize options" do
- @collection.first(:age.lte => 29, :order => :name.desc).should == @steve
- end
- end
-
- context "#last" do
- should "work with and normalize criteria" do
- @collection.last(:age.lte => 29, :order => :name.asc).should == @steve
- end
-
- should "work with and normalize options" do
- @collection.last(:age.lte => 26, :order => :name.desc).should == @chris
- end
- end
-
- context "#count" do
- should "work with no arguments" do
- @collection.count.should == 3
- end
-
- should "work with and normalize criteria" do
- @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(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 "work" do
- @collection.sort(:age).all.should == [@chris, @john, @steve]
- @collection.sort(:age.desc).all.should == [@steve, @john, @chris]
- end
- end
-
- context "#where" do
- should "work" do
- @collection.where(:age.lt => 29).where(:name => 'Chris').all.should == [@chris]
- end
- end
-
- context "#skip" do
- should "work" do
- @collection.skip(2).all(:order => :age).should == [@steve]
- end
- end
-
- context "#limit" do
- should "work" do
- @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
-
- context "#[]" do
- should "work" do
- @collection.where(:name => 'John')
- @collection[:name].should == 'John'
- end
- end
-
- context "#[]=" do
- should "work" do
- @collection[:name] = 'John'
- @collection.all.should == [@john]
- end
- end
- end
-end
Oops, something went wrong.

0 comments on commit 5fd3553

Please sign in to comment.