2009-01-30 03:25:45 +01:00
|
|
|
module CouchRest
|
|
|
|
module Mixins
|
|
|
|
module DocumentQueries
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
# Load all documents that have the "couchrest-type" field equal to the
|
|
|
|
# name of the current class. Take the standard set of
|
|
|
|
# CouchRest::Database#view options.
|
|
|
|
def all(opts = {}, &block)
|
2009-05-24 06:44:44 +02:00
|
|
|
view(:all, {:reduce => false}.merge(opts), &block)
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
2009-05-24 06:44:44 +02:00
|
|
|
|
|
|
|
# Returns the number of documents that have the "couchrest-type" field
|
|
|
|
# equal to the name of the current class. Takes the standard set of
|
|
|
|
# CouchRest::Database#view options
|
|
|
|
def count(opts = {}, &block)
|
|
|
|
result = all({:reduce => true}.merge(opts), &block)['rows']
|
|
|
|
return 0 if result.empty?
|
|
|
|
result.first['value']
|
|
|
|
end
|
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
# Load the first document that have the "couchrest-type" field equal to
|
|
|
|
# the name of the current class.
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# Object:: The first object instance available
|
|
|
|
# or
|
|
|
|
# Nil:: if no instances available
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# opts<Hash>::
|
|
|
|
# View options, see <tt>CouchRest::Database#view</tt> options for more info.
|
|
|
|
def first(opts = {})
|
|
|
|
first_instance = self.all(opts.merge!(:limit => 1))
|
|
|
|
first_instance.empty? ? nil : first_instance.first
|
|
|
|
end
|
|
|
|
|
2009-01-30 03:45:01 +01:00
|
|
|
# Load a document from the database by id
|
2009-03-27 12:27:37 +01:00
|
|
|
def get(id, db = database)
|
|
|
|
doc = db.get id
|
2009-01-30 03:45:01 +01:00
|
|
|
new(doc)
|
|
|
|
end
|
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|