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.



57
58
59
60
# 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) ⇒ 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.



33
34
35
36
# File 'lib/couchrest/mixins/collection.rb', line 33

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.



44
45
46
47
48
49
50
51
52
# 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) ⇒ 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.



19
20
21
22
23
24
25
26
# File 'lib/couchrest/mixins/collection.rb', line 19

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