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.
-
#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.
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, {:reduce => false}.merge(opts), &block) 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 24 25 |
# File 'lib/couchrest/mixins/document_queries.rb', line 21 def count(opts = {}, &block) result = all({:reduce => true}.merge(opts), &block)['rows'] return 0 if result.empty? result.first['value'] 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#viewoptions for more info.
38 39 40 41 |
# File 'lib/couchrest/mixins/document_queries.rb', line 38 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
44 45 46 47 |
# File 'lib/couchrest/mixins/document_queries.rb', line 44 def get(id, db = database) doc = db.get id new(doc) end |