Use ordered has for orderby in QueryMessage. Accept a single string for the order_by value.

This commit is contained in:
Jim Menard 2009-01-07 10:09:19 -05:00
parent 26b88efd3b
commit 9446d50145

View File

@ -15,25 +15,20 @@ module XGen
write_int(query.number_to_skip) write_int(query.number_to_skip)
write_int(query.number_to_return) write_int(query.number_to_return)
sel = query.selector sel = query.selector
if query.order_by if query.order_by && query.order_by.length > 0
sel = OrderedHash.new sel = OrderedHash.new
sel['query'] = query.selector sel['query'] = query.selector
sel['orderby'] = case query.order_by sel['orderby'] = case query.order_by
when String
{query.order_by => 1}
when Array when Array
if query.order_by.empty? # Empty array of order_by values h = OrderedHash.new
[] query.order_by.each { |ob| h[ob] = 1 }
else h
case query.order_by[0]
when Hash # Array of hashes
query.order_by
else # ['a', 'b']
query.order_by.collect { |v| {v => 1} } # Assume ascending order for all values
end
end
when Hash # Should be an ordered hash, but this message doesn't care when Hash # Should be an ordered hash, but this message doesn't care
a = [] query.order_by
query.order_by.each { |k,v| a << {k => v }} else
a raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
end end
end end