Sha256: 7a89cb262cc24890cdd00dca4350d0ffb15a1661816e7b01fce6b3e23b790bfc

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'mongo_doc/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
        #
        # <tt>Person.#{name}</tt>
        def #{name}
          criteria.#{name}
        end
      RUBY
    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)
      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
        Mongoid::Criteria.translate(self, conditions_or_id).one
      else
        Mongoid::Criteria.translate(self, conditions_or_id)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_doc-0.3.1 lib/mongo_doc/finders.rb
mongo_doc-0.3.0 lib/mongo_doc/finders.rb