Completed deprecation of old sort options, :offset, and Collection#clear
This commit is contained in:
parent
fcbdfdf8e4
commit
515594ebf3
|
@ -110,10 +110,6 @@ module Mongo
|
||||||
def find(selector={}, options={})
|
def find(selector={}, options={})
|
||||||
fields = options.delete(:fields)
|
fields = options.delete(:fields)
|
||||||
fields = ["_id"] if fields && fields.empty?
|
fields = ["_id"] if fields && fields.empty?
|
||||||
skip = options.delete(:offset) || nil
|
|
||||||
if !skip.nil?
|
|
||||||
warn "the :offset option to find is deprecated and will be removed. please use :skip instead"
|
|
||||||
end
|
|
||||||
skip = options.delete(:skip) || skip || 0
|
skip = options.delete(:skip) || skip || 0
|
||||||
limit = options.delete(:limit) || 0
|
limit = options.delete(:limit) || 0
|
||||||
sort = options.delete(:sort)
|
sort = options.delete(:sort)
|
||||||
|
@ -227,13 +223,6 @@ module Mongo
|
||||||
"db.#{@db.name}.remove(#{selector.inspect})")
|
"db.#{@db.name}.remove(#{selector.inspect})")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Remove all records.
|
|
||||||
# DEPRECATED: please use Collection#remove instead.
|
|
||||||
def clear
|
|
||||||
warn "Collection#clear is deprecated. Please use Collection#remove instead."
|
|
||||||
remove({})
|
|
||||||
end
|
|
||||||
|
|
||||||
# Update a single document in this collection.
|
# Update a single document in this collection.
|
||||||
#
|
#
|
||||||
# :selector :: a hash specifying elements which must be present for a document to be updated. Note:
|
# :selector :: a hash specifying elements which must be present for a document to be updated. Note:
|
||||||
|
|
|
@ -278,6 +278,7 @@ module Mongo
|
||||||
# Creates a new socket and tries to connect to master.
|
# Creates a new socket and tries to connect to master.
|
||||||
# If successful, sets @host and @port to master and returns the socket.
|
# If successful, sets @host and @port to master and returns the socket.
|
||||||
def connect_to_master
|
def connect_to_master
|
||||||
|
close
|
||||||
@host = @port = nil
|
@host = @port = nil
|
||||||
for node_pair in @nodes
|
for node_pair in @nodes
|
||||||
host, port = *node_pair
|
host, port = *node_pair
|
||||||
|
|
|
@ -67,7 +67,7 @@ module Mongo
|
||||||
# servers, next request will re-open on master server.
|
# servers, next request will re-open on master server.
|
||||||
if err == "not master"
|
if err == "not master"
|
||||||
raise ConnectionFailure, err
|
raise ConnectionFailure, err
|
||||||
@db.close
|
@connection.close
|
||||||
end
|
end
|
||||||
|
|
||||||
raise OperationFailure, err
|
raise OperationFailure, err
|
||||||
|
@ -301,7 +301,7 @@ module Mongo
|
||||||
close_cursor_if_query_complete
|
close_cursor_if_query_complete
|
||||||
end
|
end
|
||||||
|
|
||||||
# Run query first time we request an object from the wire
|
# Run query the first time we request an object from the wire
|
||||||
def send_query_if_needed
|
def send_query_if_needed
|
||||||
if @query_run
|
if @query_run
|
||||||
false
|
false
|
||||||
|
@ -349,14 +349,11 @@ module Mongo
|
||||||
|
|
||||||
def formatted_order_clause
|
def formatted_order_clause
|
||||||
case @order
|
case @order
|
||||||
when String then string_as_sort_parameters(@order)
|
when String, Symbol then string_as_sort_parameters(@order)
|
||||||
when Symbol then symbol_as_sort_parameters(@order)
|
|
||||||
when Array then array_as_sort_parameters(@order)
|
when Array then array_as_sort_parameters(@order)
|
||||||
when Hash # Should be an ordered hash, but this message doesn't care
|
|
||||||
warn_if_deprecated(@order)
|
|
||||||
@order
|
|
||||||
else
|
else
|
||||||
raise InvalidSortValueError, "Illegal order_by, '#{@order.class.name}'; must be String, Array, Hash, or OrderedHash"
|
raise InvalidSortValueError, "Illegal sort clause, '#{@order.class.name}'; must be of the form " +
|
||||||
|
"[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,17 +28,9 @@ module Mongo #:nodoc:
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
#
|
#
|
||||||
# *DEPRECATED
|
|
||||||
#
|
|
||||||
# <tt>array_as_sort_parameters(["field1", "field2"])</tt> =>
|
|
||||||
# <tt>{ "field1" => "1", "field2" => "1" }</tt>
|
|
||||||
#
|
|
||||||
# *New Syntax:
|
|
||||||
#
|
|
||||||
# <tt>array_as_sort_parameters([["field1", :asc], ["field2", :desc]])</tt> =>
|
# <tt>array_as_sort_parameters([["field1", :asc], ["field2", :desc]])</tt> =>
|
||||||
# <tt>{ "field1" => 1, "field2" => -1}</tt>
|
# <tt>{ "field1" => 1, "field2" => -1}</tt>
|
||||||
def array_as_sort_parameters(value)
|
def array_as_sort_parameters(value)
|
||||||
warn_if_deprecated(value)
|
|
||||||
order_by = OrderedHash.new
|
order_by = OrderedHash.new
|
||||||
value.each do |param|
|
value.each do |param|
|
||||||
if (param.class.name == "String")
|
if (param.class.name == "String")
|
||||||
|
@ -50,7 +42,7 @@ module Mongo #:nodoc:
|
||||||
order_by
|
order_by
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts the supplied +String+ to a +Hash+ to pass to mongo as
|
# Converts the supplied +String+ or +Symbol+ to a +Hash+ to pass to mongo as
|
||||||
# a sorting parameter with ascending order. If the +String+
|
# a sorting parameter with ascending order. If the +String+
|
||||||
# is empty then an empty +Hash+ will be returned.
|
# is empty then an empty +Hash+ will be returned.
|
||||||
#
|
#
|
||||||
|
@ -61,22 +53,8 @@ module Mongo #:nodoc:
|
||||||
# <tt>string_as_sort_parameters("field")</tt> => <tt>{ "field" => 1 }</tt>
|
# <tt>string_as_sort_parameters("field")</tt> => <tt>{ "field" => 1 }</tt>
|
||||||
# <tt>string_as_sort_parameters("")</tt> => <tt>{}</tt>
|
# <tt>string_as_sort_parameters("")</tt> => <tt>{}</tt>
|
||||||
def string_as_sort_parameters(value)
|
def string_as_sort_parameters(value)
|
||||||
warn_if_deprecated(value)
|
return {} if (str = value.to_s).empty?
|
||||||
return {} if value.empty?
|
{ str => 1 }
|
||||||
{ value => 1 }
|
|
||||||
end
|
|
||||||
|
|
||||||
# Converts the supplied +Symbol+ to a +Hash+ to pass to mongo as
|
|
||||||
# a sorting parameter with ascending order.
|
|
||||||
#
|
|
||||||
# Example:
|
|
||||||
#
|
|
||||||
# *DEPRECATED
|
|
||||||
#
|
|
||||||
# <tt>symbol_as_sort_parameters(:field)</tt> => <tt>{ "field" => 1 }</tt>
|
|
||||||
def symbol_as_sort_parameters(value)
|
|
||||||
warn_if_deprecated(value)
|
|
||||||
{ value.to_s => 1 }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts the +String+, +Symbol+, or +Integer+ to the
|
# Converts the +String+, +Symbol+, or +Integer+ to the
|
||||||
|
@ -96,15 +74,5 @@ module Mongo #:nodoc:
|
||||||
"#{self} was supplied as a sort direction when acceptable values are: " +
|
"#{self} was supplied as a sort direction when acceptable values are: " +
|
||||||
"Mongo::ASCENDING, 'ascending', 'asc', :ascending, :asc, 1, Mongo::DESCENDING, 'descending', 'desc', :descending, :desc, -1.")
|
"Mongo::ASCENDING, 'ascending', 'asc', :ascending, :asc, 1, Mongo::DESCENDING, 'descending', 'desc', :descending, :desc, -1.")
|
||||||
end
|
end
|
||||||
|
|
||||||
# This is the method to call when the client needs to be warned of
|
|
||||||
# deprecation in the way sorting parameters are supplied.
|
|
||||||
def warn_if_deprecated(value)
|
|
||||||
unless value.is_a?(Array) && value.first.is_a?(Array)
|
|
||||||
warn("Specifying sort order as #{value.inspect} has been deprecated in favor of a new syntax: \n" +
|
|
||||||
" :sort => [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -290,9 +290,6 @@ class TestCollection < Test::Unit::TestCase
|
||||||
@@test.save(:foo => i)
|
@@test.save(:foo => i)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO remove test for deprecated :offset option
|
|
||||||
assert_equal 5, @@test.find({}, :offset => 5).next_object()["foo"]
|
|
||||||
|
|
||||||
assert_equal 5, @@test.find({}, :skip => 5).next_object()["foo"]
|
assert_equal 5, @@test.find({}, :skip => 5).next_object()["foo"]
|
||||||
assert_equal nil, @@test.find({}, :skip => 10).next_object()
|
assert_equal nil, @@test.find({}, :skip => 10).next_object()
|
||||||
|
|
||||||
|
@ -387,11 +384,6 @@ class TestCollection < Test::Unit::TestCase
|
||||||
assert_equal 0, @collection.find.count
|
assert_equal 0, @collection.find.count
|
||||||
end
|
end
|
||||||
|
|
||||||
should "remove all records if deprecated clear is used" do
|
|
||||||
@collection.clear
|
|
||||||
assert_equal 0, @collection.find.count
|
|
||||||
end
|
|
||||||
|
|
||||||
should "remove only matching records" do
|
should "remove only matching records" do
|
||||||
@collection.remove({:name => "Jones"})
|
@collection.remove({:name => "Jones"})
|
||||||
assert_equal 1, @collection.size
|
assert_equal 1, @collection.size
|
||||||
|
|
|
@ -28,7 +28,7 @@ class ConversionsTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_symbol_as_sort_parameters
|
def test_symbol_as_sort_parameters
|
||||||
params = symbol_as_sort_parameters(:field)
|
params = string_as_sort_parameters(:field)
|
||||||
assert_equal({ "field" => 1 }, params)
|
assert_equal({ "field" => 1 }, params)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -167,7 +167,7 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
assert_equal 1, docs[3]['a']
|
assert_equal 1, docs[3]['a']
|
||||||
|
|
||||||
# Sorting using array of names; assumes ascending order.
|
# Sorting using array of names; assumes ascending order.
|
||||||
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).to_a
|
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
|
||||||
assert_equal 4, docs.size
|
assert_equal 4, docs.size
|
||||||
assert_equal 1, docs[0]['a']
|
assert_equal 1, docs[0]['a']
|
||||||
assert_equal 2, docs[1]['a']
|
assert_equal 2, docs[1]['a']
|
||||||
|
@ -197,22 +197,9 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
# order of the keys won't be guaranteed thus your sort won't make sense.
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
||||||
oh = OrderedHash.new
|
oh = OrderedHash.new
|
||||||
oh['a'] = -1
|
oh['a'] = -1
|
||||||
|
assert_raise InvalidSortValueError do
|
||||||
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
||||||
assert_equal 4, docs.size
|
end
|
||||||
assert_equal 4, docs[0]['a']
|
|
||||||
assert_equal 3, docs[1]['a']
|
|
||||||
assert_equal 2, docs[2]['a']
|
|
||||||
assert_equal 1, docs[3]['a']
|
|
||||||
|
|
||||||
oh = OrderedHash.new
|
|
||||||
oh['b'] = -1
|
|
||||||
oh['a'] = 1
|
|
||||||
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
|
||||||
assert_equal 4, docs.size
|
|
||||||
assert_equal 1, docs[0]['a']
|
|
||||||
assert_equal 3, docs[1]['a']
|
|
||||||
assert_equal 2, docs[2]['a']
|
|
||||||
assert_equal 4, docs[3]['a']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_find_limits
|
def test_find_limits
|
||||||
|
|
Loading…
Reference in New Issue