Permalink
Browse files

Renamed Query#update to options. Added Collection#where for adding $w…

…here clause.
  • Loading branch information...
1 parent 48b5464 commit 7fd3d1fd2b29eb60b2841ebeebc977b9fa668bf8 @jnunemaker jnunemaker committed May 3, 2010
Showing with 31 additions and 27 deletions.
  1. +6 −6 lib/plucky/collection.rb
  2. +11 −10 lib/plucky/query.rb
  3. +6 −0 test/test_collection.rb
  4. +8 −11 test/test_query.rb
View
12 lib/plucky/collection.rb
@@ -15,32 +15,32 @@ def initialize(collection)
@collection = collection
end
- query_delegator :fields, :filter, :limit, :reverse, :skip, :sort
+ query_delegator :fields, :filter, :limit, :reverse, :skip, :sort, :where
def all(options={})
- query.update(options)
+ query.options(options)
[].tap do |docs|
find(query.criteria, query.options).each { |doc| docs << doc }
end
end
def first(options={})
- query.update(options)
+ query.options(options)
find_one(query.criteria, query.options)
end
def last(options={})
- query.update(options).reverse
+ query.options(options).reverse
find_one(query.criteria, query.options)
end
def delete(options={})
- query.update(options)
+ query.options(options)
remove(query.criteria)
end
def count(options={})
- query.update(options)
+ query.options(options)
find(query.criteria, query.options).count
end
View
21 lib/plucky/query.rb
@@ -7,20 +7,21 @@ class Query
:fields, :skip, :limit, :sort, :hint, :snapshot, :batch_size, :timeout # Ruby Driver
]
- attr_reader :criteria, :options
+ attr_reader :criteria
- def initialize(options={})
+ def initialize(opts={})
@options, @criteria, = {}, {}
- update(options)
+ options(opts)
end
- def update(options={})
- separate_criteria_and_options(options)
+ def options(opts=nil)
+ return @options if opts.nil?
+ separate_criteria_and_options(opts || {})
self
end
def filter(hash={})
- @criteria.update(CriteriaMerger.merge(criteria, normalized_criteria(hash)))
+ @criteria.update(CriteriaMerger.merge(@criteria, normalized_criteria(hash)))
self
end
@@ -63,7 +64,7 @@ def []=(key, value)
end
def merge(other)
- clone.update(other.options).filter(other.criteria)
+ clone.options(other.options).filter(other.criteria)
end
private
@@ -140,7 +141,7 @@ def normalized_fields(fields)
return if fields.nil?
fields = fields[0] if fields.size == 1
return if fields.respond_to?(:empty?) && fields.empty?
-
+
case fields
when Array
fields.flatten.compact
@@ -167,8 +168,8 @@ def modifier?(key)
key.to_s =~ /^\$/
end
- def separate_criteria_and_options(options={})
- options.each_pair do |key, value|
+ def separate_criteria_and_options(opts={})
+ opts.each_pair do |key, value|
key = key.respond_to?(:to_sym) ? key.to_sym : key
if OptionKeys.include?(key)
View
6 test/test_collection.rb
@@ -110,5 +110,11 @@ class CollectionTest < Test::Unit::TestCase
@collection.sort(:age).reverse.all.should == [@steve, @john, @chris]
end
end
+
+ context "#where" do
+ should "work" do
+ @collection.where('this.name == "John"').all.should == [@john]
+ end
+ end
end
end
View
19 test/test_query.rb
@@ -141,6 +141,12 @@ class QueryTest < Test::Unit::TestCase
end
end
+ context "#options" do
+ should "normalize and update options" do
+ Query.new.options(:order => :age.desc).options[:sort].should == [['age', -1]]
+ end
+ end
+
context "#filter" do
should "update criteria" do
Query.new(:moo => 'cow').filter(:foo => 'bar').criteria.should == {:foo => 'bar', :moo => 'cow'}
@@ -155,8 +161,8 @@ class QueryTest < Test::Unit::TestCase
context "#where" do
should "update criteria with $where statement" do
- Query.new.where('this.writer_id = 1 || this.editor_id = 1').criteria.should == {
- '$where' => 'this.writer_id = 1 || this.editor_id = 1'
+ Query.new.where('this.writer_id == 1 || this.editor_id == 1').criteria.should == {
+ '$where' => 'this.writer_id == 1 || this.editor_id == 1'
}
end
end
@@ -203,15 +209,6 @@ class QueryTest < Test::Unit::TestCase
end
end
- context "#update" do
- should "split and update criteria and options" do
- query = Query.new(:foo => 'bar')
- query.update(:bar => 'baz', :skip => 5)
- query.criteria.should == {:foo => 'bar', :bar => 'baz'}
- query.options[:skip].should == 5
- end
- end
-
context "#sort" do
should "work with symbol operators" do
Query.new.sort(:foo.asc, :bar.desc).options[:sort].should == [['foo', 1], ['bar', -1]]

0 comments on commit 7fd3d1f

Please sign in to comment.