| Class | CouchRest::Model::Designs::View |
| In: |
lib/couchrest/model/designs/view.rb
|
| Parent: | Object |
A proxy class that allows view queries to be created using chained method calls. After each call a new instance of the method is created based on the original in a similar fashion to ruby‘s Sequel library, or Rails 3‘s Arel.
CouchDB views have inherent limitations, so joins and filters as used in a normal relational database are not possible.
| design_doc | [RW] | |
| model | [RW] | |
| name | [RW] | |
| owner | [RW] | |
| query | [RW] | |
| result | [RW] |
Simplified view definition. A new view will be added to the provided design document using the name and options.
If the view name starts with "by_" and +:by+ is not provided in the options, the new view‘s map method will be interpreted and generated automatically. For example:
View.define(Meeting, design, "by_date_and_name")
Will create a view that searches by the date and name properties. Explicity setting the attributes to use is possible using the +:by+ option. For example:
View.define(Meeting, design, "by_date_and_name", :by => [:date, :firstname, :lastname])
The view name is the same, but three keys would be used in the subsecuent index.
By default, a check is made on each of the view‘s keys to ensure they do not contain a nil value (‘null’ in javascript). This is probably what you want in most cases but sometimes in can be useful to create an index where nil is permited. Set the :allow_nil option to true to remove this check.
Conversely, keys are not checked to see if they are empty or blank. If you‘d like to enable this, set the :allow_blank option to false. The default is true, empty strings are permited in the indexes.
Accept requests as if the view was an array. Used for backwards compatibity with older queries:
Model.all(:raw => true, :limit => 0)['total_rows']
In this example, the raw option will be ignored, and the total rows will still be accessible.
Fetch all the documents the view can access. If the view has not already been prepared for including documents in the query, it will be added automatically and reset any previously cached results.
Perform a count operation based on the current view. If the view can be reduced, the reduce will be performed and return the first value. This is okay for most simple queries, but may provide unexpected results if your reduce method does not calculate the total number of documents in a result set.
Trying to use this method with the group option will raise an error.
If no reduce function is defined, a query will be performed to return the total number of rows, this is the equivalant of:
view.limit(0).total_rows
Specify the database the view should use. If not defined, an attempt will be made to load its value from the model.
The results should be provided in descending order. If the startkey or endkey query options have already been seen set, calling this method will automatically swap the options around. If you don‘t want this, simply set descending before any other option.
Descending is false by default, and this method cannot be undone once used, it has no inverse option.
Provide all the documents from the view. If the view has not been prepared with the include_docs option, each document will be loaded individually.
Check to see if the array of documents is empty. This will perform the query and return all documents ready to use, if you don‘t want to load anything, use +total_rows+ or +count+ instead.
The result set should end at the position of the provided document. The value may be provided as an object that responds to the +id+ call or a string.
No yet implemented. Eventually this will provide a raw hash of the information CouchDB holds about the view.
Keys is a special CouchDB option that will cause the view request to be POSTed including an array of keys. Only documents with the matching keys will be returned. This is much faster than sending multiple requests for a set non-consecutive documents.
If no values are provided, this method will act as a wrapper around the rows result set, providing an array of keys.
Based on the really simple support for scoped pagination in the the Kaminari gem, we provide compatible methods here to perform the same actions you‘d expect.
Return any cached values to their nil state so that any queries requested later will have a fresh set of data.
Skip the number of entries in the index specified by value. This would be the equivilent of an offset in SQL.
The CouchDB documentation states that the skip option should not be used with large data sets as it is inefficient. Use the startkey_doc method instead to skip ranges efficiently.
The result set should start from the position of the provided document. The value may be provided as an object that responds to the +id+ call or a string.
Wrapper for the total_rows value provided by the query. As per the CouchDB API, this may be nil if groups are used.