lib/mongodoc/finders.rb in mongodoc-0.2.2 vs lib/mongodoc/finders.rb in mongodoc-0.2.4

- old
+ new

@@ -1,29 +1,49 @@ +require 'mongodoc/criteria' + module MongoDoc module Finders - [:all, :count, :first, :last].each do |name| + def self.extended(base) + base.extend(Criteria) unless base === Criteria + end + + %w(count first last).each do |name| module_eval <<-RUBY + # #{name.humanize} for this +Document+ class + # + # <tt>Person.#{name}</tt> def #{name} - Criteria.new(self).#{name} + criteria.#{name} end RUBY end - def criteria - Criteria.new(self) - end - + # Find a +Document+ based on id (+String+ or +Mongo::ObjectID+) + # + # <tt>Person.find('1')</tt> + # <tt>Person.find(obj_id_1, obj_id_2)</tt> def find(*args) - query = args.extract_options! - which = args.first - Criteria.translate(self, query).send(which) + criteria.id(*args) end + # + # Find all +Document+s in the collections + # + # <tt>Person.find_all</tt> + def find_all + criteria + end + # Find a +Document+ based on id (+String+ or +Mongo::ObjectID+) + # or conditions + # + # <tt>Person.find_one('1')</tt> + # <tt>Person.find_one(:where => {:age.gt > 25})</tt> def find_one(conditions_or_id) if Hash === conditions_or_id - Criteria.translate(self, conditions_or_id).one + Mongoid::Criteria.translate(self, conditions_or_id).one else - Criteria.translate(self, conditions_or_id) + Mongoid::Criteria.translate(self, conditions_or_id) end end + end end