Skip to content

Commit

Permalink
Renamed Query#update to options. Added Collection#where for adding $w…
Browse files Browse the repository at this point in the history
…here clause.
  • Loading branch information
jnunemaker committed May 3, 2010
1 parent 48b5464 commit 7fd3d1f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
12 changes: 6 additions & 6 deletions lib/plucky/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 11 additions & 10 deletions lib/plucky/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/test_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 8 additions & 11 deletions test/test_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -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
Expand Down Expand Up @@ -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]]
Expand Down

0 comments on commit 7fd3d1f

Please sign in to comment.