Permalink
Browse files

Added Collection#fields and made Query#fields more forgiving/powerful.

  • Loading branch information...
1 parent 5d47f22 commit 15c622f1204a7852089864d38c837737911ef963 @jnunemaker jnunemaker committed May 3, 2010
Showing with 35 additions and 15 deletions.
  1. +1 −1 lib/plucky/collection.rb
  2. +17 −11 lib/plucky/query.rb
  3. +6 −0 test/test_collection.rb
  4. +11 −3 test/test_query.rb
View
@@ -15,7 +15,7 @@ def initialize(collection)
@collection = collection
end
- query_delegator :sort, :filter, :skip, :limit
+ query_delegator :fields, :filter, :limit, :skip, :sort
def all(options={})
query.update(options)
View
@@ -39,8 +39,8 @@ def limit(count=0)
self
end
- def fields(fields)
- @options[:fields] = normalized_fields(fields)
+ def fields(*args)
+ @options[:fields] = normalized_fields(args)
self
end
@@ -83,7 +83,7 @@ def normalize_options
sort @options[:sort] || @options.delete(:order)
skip @options[:skip] || @options.delete(:offset)
limit @options[:limit]
- fields normalized_fields(@options[:fields] || @options.delete(:select))
+ fields @options[:fields] || @options.delete(:select)
end
def normalized_key(field)
@@ -109,7 +109,7 @@ def normalized_value(criteria, key, value)
def normalized_sort(sort)
return if sort.nil?
- return if sort.respond_to?(:compact) && sort.compact.empty? # all nil array
+ return if sort.respond_to?(:compact) && sort.compact.empty?
sort = sort[0] if sort.size == 1
@@ -135,13 +135,19 @@ def normalized_sort(sort)
end
def normalized_fields(fields)
- return if fields.nil? || fields.empty?
- return fields if fields.is_a?(Hash)
-
- if fields.respond_to?(:flatten, :compact)
- fields.flatten.compact
- else
- fields.split(',').map { |field| field.strip }
+ 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
+ when String
+ fields.split(',').map { |field| field.strip }
+ when Symbol
+ [fields]
+ else
+ fields
end
end
View
@@ -98,5 +98,11 @@ 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
end
end
View
@@ -173,14 +173,22 @@ class QueryTest < Test::Unit::TestCase
should "normalize fields" do
Query.new.fields('foo, bar').options[:fields].should == %w(foo bar)
end
+
+ should "work with symbol" do
+ Query.new.fields(:foo).options[:fields].should == [:foo]
+ end
+
+ should "work with array of symbols" do
+ Query.new.fields(:foo, :bar).options[:fields].should == [:foo, :bar]
+ end
end
context "#limit" do
should "set limit option" do
Query.new.limit(5).options[:limit].should == 5
end
- should "override existing limit" do
+ should "overwrite existing limit" do
Query.new(:limit => 5).limit(15).options[:limit].should == 15
end
end
@@ -344,11 +352,11 @@ class QueryTest < Test::Unit::TestCase
end
should "should work with array" do
- Query.new({:fields => %w(a b)}).options[:fields].should == %w(a b)
+ Query.new(:fields => %w(a b)).options[:fields].should == %w(a b)
end
should "convert comma separated list to array" do
- Query.new({:fields => 'a, b'}).options[:fields].should == %w(a b)
+ Query.new(:fields => 'a, b').options[:fields].should == %w(a b)
end
should "also work as select" do

0 comments on commit 15c622f

Please sign in to comment.