Module: CouchRest::Mixins::DocumentQueries::ClassMethods
- Defined in:
- lib/couchrest/mixins/document_queries.rb
Instance Method Summary collapse
-
#all(opts = {}, &block) ⇒ Object
Load all documents that have the “couchrest-type” field equal to the name of the current class.
-
#bulk_get(ids, db = database) ⇒ Object
Load several documents from the database by ids No exceptions will be raised if the document isn’t found.
-
#count(opts = {}, &block) ⇒ Object
Returns the number of documents that have the “couchrest-type” field equal to the name of the current class.
-
#first(opts = {}) ⇒ Object
Load the first document that have the “couchrest-type” field equal to the name of the current class.
-
#get(id, db = database) ⇒ Object
Load a document from the database by id No exceptions will be raised if the document isn’t found.
-
#get!(id, db = database) ⇒ Object
Load a document from the database by id An exception will be raised if the document isn’t found.
Instance Method Details
#all(opts = {}, &block) ⇒ Object
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.
14 15 16 |
# File 'lib/couchrest/mixins/document_queries.rb', line 14 def all(opts = {}, &block) view(:all, opts, &block) end |
#bulk_get(ids, db = database) ⇒ Object
Load several documents from the database by ids No exceptions will be raised if the document isn’t found
Returns
- [Objects]
-
if the document was found
documents that are not found are nil or
- Nil
-
if there was an error
Parameters
- ids<Array> of <String, Integer>
-
Document IDs
- db<Database>
-
optional option to pass a custom database to use
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/couchrest/mixins/document_queries.rb', line 74 def bulk_get(ids, db = database) begin rows = db.bulk_load(ids)['rows'] docs = rows.map{ |row| row['doc'] } rescue nil else docs.map{|doc| new(doc) if doc } end end |
#count(opts = {}, &block) ⇒ Object
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
21 22 23 |
# File 'lib/couchrest/mixins/document_queries.rb', line 21 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 “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
CouchRest::Database#view
options for more info.
36 37 38 39 |
# File 'lib/couchrest/mixins/document_queries.rb', line 36 def first(opts = {}) first_instance = self.all(opts.merge!(:limit => 1)) first_instance.empty? ? nil : first_instance.first end |
#get(id, db = database) ⇒ Object
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
52 53 54 55 56 57 58 59 60 |
# File 'lib/couchrest/mixins/document_queries.rb', line 52 def get(id, db = database) begin doc = db.get id rescue nil else new(doc) end end |
#get!(id, db = database) ⇒ Object
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
95 96 97 98 |
# File 'lib/couchrest/mixins/document_queries.rb', line 95 def get!(id, db = database) doc = db.get id new(doc) end |