Sha256: c2fc25bbba1f226694a768a638e42d568a96913782457aa944bdf850d04410bd

Contents?: true

Size: 1.67 KB

Versions: 23

Compression:

Stored size: 1.67 KB

Contents

module MongoModel
  class Scope
    module FinderMethods
      def find(*ids, &block)
        if block_given?
          to_a.find(&block)
        else
          ids.flatten!

          case ids.size
          when 0
            raise ArgumentError, "At least one id must be specified"
          when 1
            id = ids.first
            where(:id => id).first || raise(DocumentNotFound, "Couldn't find document with id: #{id}")
          else
            ids = ids.map { |id| Reference.cast(id) }
            docs = where(:id.in => ids).to_a
            raise DocumentNotFound if docs.size != ids.size
            docs.sort_by { |doc| ids.index(doc.id) }
          end
        end
      end
      
      def first(count=nil)
        if loaded?
          count ? to_a.first(count) : to_a.first
        else
          count ? limit(count).to_a : limit(1).to_a[0]
        end
      end
      
      def last(count=nil)
        if loaded?
          count ? to_a.last(count) : to_a.last
        else
          count ? reverse_order.limit(count).to_a : reverse_order.limit(1).to_a[0]
        end
      end
      
      def all
        to_a
      end
      
      def exists?(id)
        where(:id => id).any?
      end
      
      def apply_finder_options(options={})
        result = clone
        
        result = result.where(options[:conditions]) if options[:conditions]
        result = result.order(options[:order])      if options[:order]
        result = result.select(options[:select])    if options[:select]
        result = result.limit(options[:limit])      if options[:limit]
        result = result.offset(options[:offset])    if options[:offset]
        
        result
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
mongomodel-0.5.5 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.5.4 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.5.3 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.5.2 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.5.1 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.5.0 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.9 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.8 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.7 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.6 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.5 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.4 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.3 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.2 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.1 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.4.0 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.3.6 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.3.5 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.3.4 lib/mongomodel/support/scope/finder_methods.rb
mongomodel-0.3.3 lib/mongomodel/support/scope/finder_methods.rb