Module: CouchRest::Model::Collection::ClassMethods

Defined in:
lib/couchrest/model/collection.rb

Instance Method Summary collapse

Instance Method Details

#collection_proxy_for(design_doc, view_name, view_options = {}) ⇒ Object

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



63
64
65
66
# File 'lib/couchrest/model/collection.rb', line 63

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) ⇒ Object

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.



39
40
41
42
# File 'lib/couchrest/model/collection.rb', line 39

def paginate(options)
  proxy = create_collection_proxy(options)
  proxy.paginate(options)
end

#paginated_each(options, &block) ⇒ Object

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.



50
51
52
53
54
55
56
57
58
# File 'lib/couchrest/model/collection.rb', line 50

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) ⇒ Object

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.



25
26
27
28
29
30
31
32
# File 'lib/couchrest/model/collection.rb', line 25

def provides_collection(collection_name, design_doc, view_name, view_options)
  class_eval <<-END, __FILE__, __LINE__ + 1
    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
  END
end