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
|
2011-06-08 18:22:35 +02:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
module ClassMethods
|
2011-06-08 18:22:35 +02:00
|
|
|
|
2010-09-17 23:00:55 +02:00
|
|
|
# Load all documents that have the model_type_key's field equal to the
|
2009-01-30 03:25:45 +01:00
|
|
|
# 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
|
|
|
|
2010-09-17 23:00:55 +02:00
|
|
|
# Returns the number of documents that have the model_type_key's field
|
2009-05-24 06:44:44 +02:00
|
|
|
# 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
|
|
|
|
|
2010-09-17 23:00:55 +02:00
|
|
|
# Load the first document that have the model_type_key's field equal to
|
2009-01-30 03:25:45 +01:00
|
|
|
# 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
|
|
|
|
|
2010-12-31 21:59:57 +01:00
|
|
|
# Load the last document that have the model_type_key's field equal to
|
|
|
|
# the name of the current class.
|
|
|
|
# It's similar to method first, just adds :descending => true
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# Object:: The last 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 last(opts = {})
|
|
|
|
first(opts.merge!(:descending => true))
|
|
|
|
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
|
2011-06-08 18:22:35 +02:00
|
|
|
|
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)
|
2011-05-23 19:02:08 +02:00
|
|
|
raise CouchRest::Model::DocumentNotFound if id.blank?
|
|
|
|
|
2009-03-27 12:27:37 +01:00
|
|
|
doc = db.get id
|
2011-02-09 21:21:03 +01:00
|
|
|
build_from_database(doc)
|
2011-05-23 19:02:08 +02:00
|
|
|
rescue RestClient::ResourceNotFound
|
|
|
|
raise CouchRest::Model::DocumentNotFound
|
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
|