Module: CouchRest::Model::DocumentQueries::ClassMethods
- Defined in:
- lib/couchrest/model/document_queries.rb
Instance Method Summary collapse
-
#all(opts = {}, &block) ⇒ Object
Load all documents that have the model_type_key’s field equal to the name of the current class.
-
#count(opts = {}, &block) ⇒ Object
Returns the number of documents that have the model_type_key’s field equal to the name of the current class.
-
#first(opts = {}) ⇒ Object
Load the first document that have the model_type_key’s field equal to the name of the current class.
-
#get(id, db = database) ⇒ Object
(also: #find)
Load a document from the database by id No exceptions will be raised if the document isn’t found.
-
#get!(id, db = database) ⇒ Object
(also: #find!)
Load a document from the database by id An exception will be raised if the document isn’t found.
-
#last(opts = {}) ⇒ Object
Load the last document that have the model_type_key’s field equal to the name of the current class.
Instance Method Details
#all(opts = {}, &block) ⇒ Object
Load all documents that have the model_type_key’s field equal to the name of the current class. Take the standard set of CouchRest::Database#view options.
11 12 13 |
# File 'lib/couchrest/model/document_queries.rb', line 11 def all(opts = {}, &block) view(:all, opts, &block) end |
#count(opts = {}, &block) ⇒ Object
Returns the number of documents that have the model_type_key’s field equal to the name of the current class. Takes the standard set of CouchRest::Database#view options
18 19 20 |
# File 'lib/couchrest/model/document_queries.rb', line 18 def count(opts = {}, &block) all({:raw => true, :limit => 0}.merge(opts), &block)['total_rows'] end |
#first(opts = {}) ⇒ Object
Load the first document that have the model_type_key’s 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
CouchRest::Database#view
options for more info.
33 34 35 36 |
# File 'lib/couchrest/model/document_queries.rb', line 33 def first(opts = {}) first_instance = self.all(opts.merge!(:limit => 1)) first_instance.empty? ? nil : first_instance.first end |
#get(id, db = database) ⇒ Object Also known as: find
Load a document from the database by id 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
65 66 67 68 69 70 71 |
# File 'lib/couchrest/model/document_queries.rb', line 65 def get(id, db = database) begin get!(id, db) rescue nil end end |
#get!(id, db = database) ⇒ Object Also known as: find!
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
85 86 87 88 89 90 91 92 |
# File 'lib/couchrest/model/document_queries.rb', line 85 def get!(id, db = database) raise CouchRest::Model::DocumentNotFound if id.blank? doc = db.get id build_from_database(doc) rescue RestClient::ResourceNotFound raise CouchRest::Model::DocumentNotFound end |
#last(opts = {}) ⇒ Object
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
CouchRest::Database#view
options for more info.
50 51 52 |
# File 'lib/couchrest/model/document_queries.rb', line 50 def last(opts = {}) first(opts.merge!(:descending => true)) end |