require 'mongodoc/criteria'
module MongoDoc
module Finders
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
#
# Person.#{name}
def #{name}
criteria.#{name}
end
RUBY
end
# Find a +Document+ based on id (+String+ or +Mongo::ObjectID+)
#
# Person.find('1')
# Person.find(obj_id_1, obj_id_2)
def find(*args)
criteria.id(*args)
end
#
# Find all +Document+s in the collections
#
# Person.find_all
def find_all
criteria
end
# Find a +Document+ based on id (+String+ or +Mongo::ObjectID+)
# or conditions
#
# Person.find_one('1')
# Person.find_one(:where => {:age.gt > 25})
def find_one(conditions_or_id)
if Hash === conditions_or_id
Mongoid::Criteria.translate(self, conditions_or_id).one
else
Mongoid::Criteria.translate(self, conditions_or_id)
end
end
end
end