lib/mongoid/finders.rb in mongoid-1.2.8 vs lib/mongoid/finders.rb in mongoid-1.2.9
- old
+ new
@@ -1,8 +1,18 @@
# encoding: utf-8
module Mongoid #:nodoc:
module Finders #:nodoc:
+
+ # Delegate to the criteria methods that are natural for creating a new
+ # criteria.
+ [ :all_in, :any_in, :excludes, :limit, :max, :min,
+ :not_in, :only, :order_by, :skip, :sum, :where ].each do |name|
+ define_method(name) do |*args|
+ criteria.send(name, *args)
+ end
+ end
+
# Find +Documents+ given the conditions.
#
# Options:
#
# args: A +Hash+ with a conditions key and other options
@@ -98,40 +108,10 @@
# <tt>Person.last(:conditions => { :attribute => "value" })</tt>
def last(*args)
find(:last, *args)
end
- # Convenience method for returning the max value of a field.
- #
- # Options:
- #
- # field: The field to use when calculating the max.
- #
- # Example:
- #
- # <tt>Person.max(:age)</tt>
- #
- # Returns: <tt>Float</tt> max value.
- def max(field)
- criteria.max(field)
- end
-
- # Convenience method for returning the min value of a field.
- #
- # Options:
- #
- # field: The field to use when calculating the min.
- #
- # Example:
- #
- # <tt>Person.min(:age)</tt>
- #
- # Returns: <tt>Float</tt> min value.
- def min(field)
- criteria.min(field)
- end
-
# Find all documents in paginated fashion given the supplied arguments.
# If no parameters are passed just default to offset 0 and limit 20.
#
# Options:
#
@@ -143,59 +123,9 @@
# :per_page => 20)</tt>
#
# Returns paginated array of docs.
def paginate(params = {})
Criteria.translate(self, params).paginate
- end
-
- # Entry point for creating a new criteria from a Document. This will
- # instantiate a new +Criteria+ object with the supplied select criterion
- # already added to it.
- #
- # Options:
- #
- # args: A list of field names to retrict the returned fields to.
- #
- # Example:
- #
- # <tt>Person.only(:field1, :field2, :field3)</tt>
- #
- # Returns: <tt>Criteria</tt>
- def only(*args)
- criteria.only(*args)
- end
-
- # Convenience method for returning the sum of a specified field for all
- # documents in the database.
- #
- # Options:
- #
- # field: The field to use when calculating the sum.
- #
- # Example:
- #
- # <tt>Person.sum(:age)</tt>
- #
- # Returns: <tt>Float</tt> of the sum.
- def sum(field)
- criteria.sum(field)
- end
-
- # Entry point for creating a new criteria from a Document. This will
- # instantiate a new +Criteria+ object with the supplied select criterion
- # already added to it.
- #
- # Options:
- #
- # selector: A where criteria to initialize.
- #
- # Example:
- #
- # <tt>Person.where(:field1 => "Value")</tt>
- #
- # Returns: <tt>Criteria</tt>
- def where(selector = nil)
- criteria.where(selector)
end
protected
# Find the first object or create/initialize it.
def find_or(method, attrs = {})