module CouchRest::Mixins::Collection::ClassMethods

Public Instance Methods

collection_proxy_for(design_doc, view_name, view_options = {}) click to toggle source

Create a CollectionProxy for the specified view and options. CollectionProxy behaves just like an Array, but offers support for pagination.

# File lib/couchrest/mixins/collection.rb, line 57
def collection_proxy_for(design_doc, view_name, view_options = {})
  options = view_options.merge(:design_doc => design_doc, :view_name => view_name)
  create_collection_proxy(options)
end
paginate(options) click to toggle source

Fetch a group of objects from CouchDB. Options can include:

:page - Specifies the page to load (starting at 1)
:per_page - Specifies the number of objects to load per page

Defaults are used if these options are not specified.

# File lib/couchrest/mixins/collection.rb, line 33
def paginate(options)
  proxy = create_collection_proxy(options)
  proxy.paginate(options)
end
paginated_each(options, &block) click to toggle source

Iterate over the objects in a collection, fetching them from CouchDB in groups. Options can include:

:page - Specifies the page to load
:per_page - Specifies the number of objects to load per page

Defaults are used if these options are not specified.

# File lib/couchrest/mixins/collection.rb, line 44
def paginated_each(options, &block)
  search = options.delete(:search)
  unless search == true
    proxy = create_collection_proxy(options)
  else
    proxy = create_search_collection_proxy(options)
  end
  proxy.paginated_each(options, &block)
end
provides_collection(collection_name, design_doc, view_name, view_options) click to toggle source

Creates a new class method, find_all_<collection_name>, that will execute the view specified with the design_doc and view_name parameters, along with the specified view_options. This method will return the results of the view as an Array of objects which are instances of the class.

This method is handy for objects that do not use the view_by method to declare their views.

# File lib/couchrest/mixins/collection.rb, line 19
        def provides_collection(collection_name, design_doc, view_name, view_options)
          class_eval "            def self.find_all_#{collection_name}(options = {})
              view_options = #{view_options.inspect} || {}
              CollectionProxy.new(options[:database] || database, "#{design_doc}", "#{view_name}", view_options.merge(options), Kernel.const_get('#{self}'))
            end
", __FILE__, __LINE__ + 1
        end