2009-01-30 03:25:45 +01:00
|
|
|
module CouchRest
|
2010-06-20 22:01:11 +02:00
|
|
|
module Model
|
2009-01-30 03:25:45 +01:00
|
|
|
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-07-25 07:31:36 +02:00
|
|
|
view(:all, 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)
|
2009-07-17 05:38:15 +02:00
|
|
|
all({:raw => true, :limit => 0}.merge(opts), &block)['total_rows']
|
2009-05-24 06:44:44 +02:00
|
|
|
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-07-23 01:05:55 +02:00
|
|
|
# No exceptions will be raised if the document isn't found
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# Object:: if the document was found
|
|
|
|
# or
|
|
|
|
# Nil::
|
|
|
|
#
|
|
|
|
# === Parameters
|
|
|
|
# id<String, Integer>:: Document ID
|
|
|
|
# db<Database>:: optional option to pass a custom database to use
|
2009-03-27 12:27:37 +01:00
|
|
|
def get(id, db = database)
|
2009-07-23 01:05:55 +02:00
|
|
|
begin
|
2009-12-04 08:58:07 +01:00
|
|
|
get!(id, db)
|
2009-07-23 01:05:55 +02:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2010-06-07 13:56:39 +02:00
|
|
|
alias :find :get
|
2009-07-23 01:05:55 +02:00
|
|
|
|
|
|
|
# Load a document from the database by id
|
|
|
|
# An exception will be raised if the document isn't found
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# Object:: if the document was found
|
|
|
|
# or
|
|
|
|
# Exception
|
|
|
|
#
|
|
|
|
# === Parameters
|
|
|
|
# id<String, Integer>:: Document ID
|
|
|
|
# db<Database>:: optional option to pass a custom database to use
|
|
|
|
def get!(id, db = database)
|
2009-03-27 12:27:37 +01:00
|
|
|
doc = db.get id
|
2009-12-04 08:58:07 +01:00
|
|
|
create_from_database(doc)
|
2009-01-30 03:45:01 +01:00
|
|
|
end
|
2010-06-07 13:56:39 +02:00
|
|
|
alias :find! :get!
|
2009-01-30 03:45:01 +01:00
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2009-12-04 08:58:07 +01:00
|
|
|
end
|