Module: CouchRest::Mixins::Collection::ClassMethods

Defined in:
lib/couchrest/mixins/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.



61
62
63
64
# File 'lib/couchrest/mixins/collection.rb', line 61

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.



42
43
44
45
# File 'lib/couchrest/mixins/collection.rb', line 42

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.



53
54
55
56
# File 'lib/couchrest/mixins/collection.rb', line 53

def paginated_each(options, &block)
  proxy = create_collection_proxy(options)
  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.



28
29
30
31
32
33
34
35
# File 'lib/couchrest/mixins/collection.rb', line 28

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(@database, "#{design_doc}", "#{view_name}", view_options.merge(options), Kernel.const_get('#{self}'))
    end
  END
end