@@ -260,16 +260,16 @@ def find_one_and_delete
260
260
#
261
261
# @return [ Document ] The first document.
262
262
def first ( limit_or_opts = nil )
263
- limit = limit_or_opts unless limit_or_opts . is_a? ( Hash )
263
+ limit , opts = extract_limit_and_opts ( limit_or_opts )
264
264
if cached? && cache_loaded?
265
265
return limit ? documents . first ( limit ) : documents . first
266
266
end
267
267
try_numbered_cache ( :first , limit ) do
268
- if limit_or_opts . try ( : key?, :id_sort )
268
+ if opts . key? ( :id_sort )
269
269
Mongoid ::Warnings . warn_id_sort_deprecated
270
270
end
271
271
sorted_view = view
272
- if sort = view . sort || ( { _id : 1 } unless limit_or_opts . try ( :fetch , : id_sort) == :none )
272
+ if sort = view . sort || ( { _id : 1 } unless opts [ : id_sort] == :none )
273
273
sorted_view = view . sort ( sort )
274
274
end
275
275
if raw_docs = sorted_view . limit ( limit || 1 ) . to_a
@@ -376,12 +376,12 @@ def initialize(criteria)
376
376
#
377
377
# @return [ Document ] The last document.
378
378
def last ( limit_or_opts = nil )
379
- limit = limit_or_opts unless limit_or_opts . is_a? ( Hash )
379
+ limit , opts = extract_limit_and_opts ( limit_or_opts )
380
380
if cached? && cache_loaded?
381
381
return limit ? documents . last ( limit ) : documents . last
382
382
end
383
383
res = try_numbered_cache ( :last , limit ) do
384
- with_inverse_sorting ( limit_or_opts ) do
384
+ with_inverse_sorting ( opts ) do
385
385
if raw_docs = view . limit ( limit || 1 ) . to_a
386
386
process_raw_docs ( raw_docs , limit )
387
387
end
@@ -612,6 +612,23 @@ def try_numbered_cache(key, n, &block)
612
612
end
613
613
end
614
614
615
+ # Extract the limit and opts from the given argument, so that code
616
+ # can operate without having to worry about the current type and
617
+ # state of the argument.
618
+ #
619
+ # @param [ nil | Integer | Hash ] limit_or_opts The value to pull the
620
+ # limit and option hash from.
621
+ #
622
+ # @return [ Array<nil | Integer, Hash> ] A 2-array of the limit and the
623
+ # option hash.
624
+ def extract_limit_and_opts ( limit_or_opts )
625
+ case limit_or_opts
626
+ when nil , Integer then [ limit_or_opts , { } ]
627
+ when Hash then [ nil , limit_or_opts ]
628
+ else raise ArgumentError , "expected nil, Integer, or Hash"
629
+ end
630
+ end
631
+
615
632
# Update the documents for the provided method.
616
633
#
617
634
# @api private
@@ -676,10 +693,10 @@ def apply_option(name)
676
693
# @example Apply the inverse sorting params to the given block
677
694
# context.with_inverse_sorting
678
695
def with_inverse_sorting ( opts = { } )
679
- Mongoid ::Warnings . warn_id_sort_deprecated if opts . try ( : key?, :id_sort )
696
+ Mongoid ::Warnings . warn_id_sort_deprecated if opts . key? ( :id_sort )
680
697
681
698
begin
682
- if sort = criteria . options [ :sort ] || ( { _id : 1 } unless opts . try ( :fetch , : id_sort) == :none )
699
+ if sort = criteria . options [ :sort ] || ( { _id : 1 } unless opts [ : id_sort] == :none )
683
700
@view = view . sort ( Hash [ sort . map { |k , v | [ k , -1 *v ] } ] )
684
701
end
685
702
yield
0 commit comments