Skip to content
Browse files

some nasty args parsing in Collection#find

  • Loading branch information...
1 parent ef9f277 commit f0c4e79c4e8f009f0acd590087c7b3152f7fcc2d @tmm1 committed
Showing with 33 additions and 9 deletions.
  1. +8 −0 lib/mongo.rb
  2. +23 −7 lib/mongo/collection.rb
  3. +2 −2 lib/mongo/symbol.rb
View
8 lib/mongo.rb
@@ -138,6 +138,14 @@ def Mongo namespace, client = nil
end
end
+ should 'find ranges' do
+ @numbers.find(:num > 2, :num < 4) do |res|
+ res.first[:num].should == 3
+ res.size.should == 1
+ done
+ end
+ end
+
end
__END__
View
30 lib/mongo/collection.rb
@@ -13,21 +13,37 @@ def initialize ns, client = nil
end
# find {:a => 1}, :b < 2, :a.asc, :b.desc, :limit => 10, :skip => 5
- def find obj, *args, &blk
+ def find *args, &blk
opts = Hash.new(0)
opts.update(args.pop) if args.size > 1 and args.last.keys.find{|k| [:limit, :skip].include? k }
+
+ query = args.inject({}) do |h, arg|
+ unless arg.has_key? :orderby
+ if arg.is_a? Hash and arg.values.first.is_a? Hash and arg.values.first.keys.first.to_s =~ /^\$/
+ arg.each do |k,v|
+ (h[k] ||= {}).update v
+ end
+ else
+ h.update arg
+ end
+ end
+
+ h
+ end
+
+ order = args.reject do |arg| not arg.has_key? :orderby end
# to sort: { query : { ... } , orderby : { ... } }
- obj = {
- :query => obj,
- :orderby => args.inject({}){ |h,a| h.update(a) }
- } if args.any?
-
+ query = {
+ :query => query,
+ :orderby => order.inject({}){ |h,a| h.update(a[:orderby]); h }
+ } if order.any?
+
@client.send 2004, :int, reserved = 0,
:cstring, @ns,
:int, opts[:skip],
:int, opts[:limit],
- :bson, obj,
+ :bson, query,
&blk
end
View
4 lib/mongo/symbol.rb
@@ -17,11 +17,11 @@ def in values
end
def asc
- { self => 1 }
+ { :orderby => { self => 1 } }
end
def desc
- { self => -1 }
+ { :orderby => { self => -1 } }
end
end

0 comments on commit f0c4e79

Please sign in to comment.
Something went wrong with that request. Please try again.