2009-06-04 20:43:14 +02:00
|
|
|
module CouchRest
|
2010-06-20 22:01:11 +02:00
|
|
|
module Model
|
2009-06-04 20:43:14 +02:00
|
|
|
module Collection
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2009-06-19 18:00:52 +02:00
|
|
|
|
2009-06-19 18:17:40 +02:00
|
|
|
# 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.
|
2009-06-19 18:00:52 +02:00
|
|
|
#
|
|
|
|
# This method is handy for objects that do not use the view_by method
|
|
|
|
# to declare their views.
|
2009-06-19 18:17:40 +02:00
|
|
|
def provides_collection(collection_name, design_doc, view_name, view_options)
|
2009-06-04 20:43:14 +02:00
|
|
|
class_eval <<-END, __FILE__, __LINE__ + 1
|
|
|
|
def self.find_all_#{collection_name}(options = {})
|
2009-06-19 18:17:40 +02:00
|
|
|
view_options = #{view_options.inspect} || {}
|
2010-04-06 00:22:56 +02:00
|
|
|
CollectionProxy.new(options[:database] || database, "#{design_doc}", "#{view_name}", view_options.merge(options), Kernel.const_get('#{self}'))
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2009-06-19 18:17:40 +02:00
|
|
|
# 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.
|
2009-06-04 20:43:14 +02:00
|
|
|
def paginate(options)
|
|
|
|
proxy = create_collection_proxy(options)
|
|
|
|
proxy.paginate(options)
|
|
|
|
end
|
|
|
|
|
2009-06-19 18:17:40 +02:00
|
|
|
# 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.
|
2009-06-04 20:43:14 +02:00
|
|
|
def paginated_each(options, &block)
|
2010-03-11 18:17:15 +01:00
|
|
|
search = options.delete(:search)
|
|
|
|
unless search == true
|
|
|
|
proxy = create_collection_proxy(options)
|
|
|
|
else
|
|
|
|
proxy = create_search_collection_proxy(options)
|
|
|
|
end
|
2009-06-04 20:43:14 +02:00
|
|
|
proxy.paginated_each(options, &block)
|
|
|
|
end
|
|
|
|
|
2009-06-19 18:17:40 +02:00
|
|
|
# Create a CollectionProxy for the specified view and options.
|
|
|
|
# CollectionProxy behaves just like an Array, but offers support for
|
|
|
|
# pagination.
|
2009-06-18 18:28:57 +02:00
|
|
|
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
|
|
|
|
|
2009-06-04 20:43:14 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def create_collection_proxy(options)
|
2009-06-19 18:00:52 +02:00
|
|
|
design_doc, view_name, view_options = parse_view_options(options)
|
2010-04-06 00:22:56 +02:00
|
|
|
CollectionProxy.new(options[:database] || database, design_doc, view_name, view_options, self)
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
|
2010-03-11 18:17:15 +01:00
|
|
|
def create_search_collection_proxy(options)
|
|
|
|
design_doc, search_name, search_options = parse_search_options(options)
|
2010-04-06 00:22:56 +02:00
|
|
|
CollectionProxy.new(options[:database] || database, design_doc, search_name, search_options, self, :search)
|
2010-03-11 18:17:15 +01:00
|
|
|
end
|
|
|
|
|
2009-06-04 20:43:14 +02:00
|
|
|
def parse_view_options(options)
|
2009-06-18 18:28:57 +02:00
|
|
|
design_doc = options.delete(:design_doc)
|
|
|
|
raise ArgumentError, 'design_doc is required' if design_doc.nil?
|
2009-06-04 20:43:14 +02:00
|
|
|
|
|
|
|
view_name = options.delete(:view_name)
|
|
|
|
raise ArgumentError, 'view_name is required' if view_name.nil?
|
|
|
|
|
2009-06-19 18:00:52 +02:00
|
|
|
default_view_options = (design_doc.class == Design &&
|
|
|
|
design_doc['views'][view_name.to_s] &&
|
|
|
|
design_doc['views'][view_name.to_s]["couchrest-defaults"]) || {}
|
|
|
|
view_options = default_view_options.merge(options)
|
2010-12-02 01:53:45 +01:00
|
|
|
view_options.delete(:database)
|
2009-06-04 20:43:14 +02:00
|
|
|
|
2009-06-19 18:00:52 +02:00
|
|
|
[design_doc, view_name, view_options]
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
2010-03-11 18:17:15 +01:00
|
|
|
|
|
|
|
def parse_search_options(options)
|
|
|
|
design_doc = options.delete(:design_doc)
|
|
|
|
raise ArgumentError, 'design_doc is required' if design_doc.nil?
|
|
|
|
|
|
|
|
search_name = options.delete(:view_name)
|
|
|
|
raise ArgumentError, 'search_name is required' if search_name.nil?
|
|
|
|
|
|
|
|
search_options = options.clone
|
2010-12-02 01:53:45 +01:00
|
|
|
search_options.delete(:database)
|
|
|
|
|
2010-03-11 18:17:15 +01:00
|
|
|
[design_doc, search_name, search_options]
|
|
|
|
end
|
|
|
|
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
class CollectionProxy
|
|
|
|
alias_method :proxy_respond_to?, :respond_to?
|
|
|
|
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
|
|
|
|
|
2009-06-19 18:00:52 +02:00
|
|
|
DEFAULT_PAGE = 1
|
|
|
|
DEFAULT_PER_PAGE = 30
|
2009-07-21 00:52:14 +02:00
|
|
|
|
2009-06-19 18:17:40 +02:00
|
|
|
# Create a new CollectionProxy to represent the specified view. If a
|
|
|
|
# container class is specified, the proxy will create an object of the
|
|
|
|
# given type for each row that comes back from the view. If no
|
|
|
|
# container class is specified, the raw results are returned.
|
|
|
|
#
|
|
|
|
# The CollectionProxy provides support for paginating over a collection
|
|
|
|
# via the paginate, and paginated_each methods.
|
2010-03-11 18:17:15 +01:00
|
|
|
def initialize(database, design_doc, view_name, view_options = {}, container_class = nil, query_type = :view)
|
2009-06-18 18:28:57 +02:00
|
|
|
raise ArgumentError, "database is a required parameter" if database.nil?
|
|
|
|
|
2009-06-04 20:43:14 +02:00
|
|
|
@database = database
|
2009-06-18 18:28:57 +02:00
|
|
|
@container_class = container_class
|
2010-03-11 18:17:15 +01:00
|
|
|
@query_type = query_type
|
2009-06-18 18:28:57 +02:00
|
|
|
|
2009-06-19 18:00:52 +02:00
|
|
|
strip_pagination_options(view_options)
|
|
|
|
@view_options = view_options
|
|
|
|
|
2009-06-18 18:28:57 +02:00
|
|
|
if design_doc.class == Design
|
|
|
|
@view_name = "#{design_doc.name}/#{view_name}"
|
|
|
|
else
|
|
|
|
@view_name = "#{design_doc}/#{view_name}"
|
|
|
|
end
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
|
2009-06-19 18:17:40 +02:00
|
|
|
# See Collection.paginate
|
2009-06-04 20:43:14 +02:00
|
|
|
def paginate(options = {})
|
|
|
|
page, per_page = parse_options(options)
|
2010-03-11 18:17:15 +01:00
|
|
|
results = @database.send(@query_type, @view_name, pagination_options(page, per_page))
|
2009-06-22 15:39:28 +02:00
|
|
|
remember_where_we_left_off(results, page)
|
2010-03-11 18:17:15 +01:00
|
|
|
instances = convert_to_container_array(results)
|
|
|
|
|
|
|
|
begin
|
|
|
|
if Kernel.const_get('WillPaginate')
|
|
|
|
total_rows = results['total_rows'].to_i
|
|
|
|
paginated = WillPaginate::Collection.create(page, per_page, total_rows) do |pager|
|
|
|
|
pager.replace(instances)
|
|
|
|
end
|
|
|
|
return paginated
|
|
|
|
end
|
|
|
|
rescue NameError
|
|
|
|
# When not using will_paginate, not much we could do about this. :x
|
|
|
|
end
|
|
|
|
return instances
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
|
2009-06-19 18:17:40 +02:00
|
|
|
# See Collection.paginated_each
|
2009-06-04 20:43:14 +02:00
|
|
|
def paginated_each(options = {}, &block)
|
|
|
|
page, per_page = parse_options(options)
|
|
|
|
|
|
|
|
begin
|
|
|
|
collection = paginate({:page => page, :per_page => per_page})
|
|
|
|
collection.each(&block)
|
|
|
|
page += 1
|
|
|
|
end until collection.size < per_page
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(*args)
|
|
|
|
proxy_respond_to?(*args) || (load_target && @target.respond_to?(*args))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Explicitly proxy === because the instance method removal above
|
|
|
|
# doesn't catch it.
|
|
|
|
def ===(other)
|
|
|
|
load_target
|
|
|
|
other === @target
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def method_missing(method, *args)
|
|
|
|
if load_target
|
|
|
|
if block_given?
|
|
|
|
@target.send(method, *args) { |*block_args| yield(*block_args) }
|
|
|
|
else
|
|
|
|
@target.send(method, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_target
|
|
|
|
unless loaded?
|
2010-03-11 18:17:15 +01:00
|
|
|
@view_options.merge!({:include_docs => true}) if @query_type == :search
|
|
|
|
results = @database.send(@query_type, @view_name, @view_options)
|
2009-06-18 18:28:57 +02:00
|
|
|
@target = convert_to_container_array(results)
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
@loaded = true
|
|
|
|
@target
|
|
|
|
end
|
|
|
|
|
|
|
|
def loaded?
|
|
|
|
@loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
reset
|
|
|
|
load_target
|
|
|
|
self unless @target.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
@loaded = false
|
|
|
|
@target = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
load_target
|
|
|
|
@target.inspect
|
|
|
|
end
|
|
|
|
|
2009-08-03 21:19:07 +02:00
|
|
|
def convert_to_container_array(results)
|
2009-06-18 18:28:57 +02:00
|
|
|
if @container_class.nil?
|
|
|
|
results
|
|
|
|
else
|
2009-12-04 08:58:07 +01:00
|
|
|
results['rows'].collect { |row| @container_class.create_from_database(row['doc']) } unless results['rows'].nil?
|
2009-06-18 18:28:57 +02:00
|
|
|
end
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def pagination_options(page, per_page)
|
2009-06-22 15:39:28 +02:00
|
|
|
view_options = @view_options.clone
|
2010-03-11 18:17:15 +01:00
|
|
|
if @query_type == :view && @last_key && @last_docid && @last_page == page - 1
|
2009-08-19 19:26:25 +02:00
|
|
|
key = view_options.delete(:key)
|
|
|
|
end_key = view_options[:endkey] || key
|
|
|
|
options = { :startkey => @last_key, :endkey => end_key, :startkey_docid => @last_docid, :limit => per_page, :skip => 1 }
|
2009-06-22 15:39:28 +02:00
|
|
|
else
|
|
|
|
options = { :limit => per_page, :skip => per_page * (page - 1) }
|
|
|
|
end
|
|
|
|
view_options.merge(options)
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_options(options)
|
2009-06-19 18:00:52 +02:00
|
|
|
page = options.delete(:page) || DEFAULT_PAGE
|
|
|
|
per_page = options.delete(:per_page) || DEFAULT_PER_PAGE
|
2009-06-18 18:28:57 +02:00
|
|
|
[page.to_i, per_page.to_i]
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
2009-06-19 18:00:52 +02:00
|
|
|
|
|
|
|
def strip_pagination_options(options)
|
|
|
|
parse_options(options)
|
|
|
|
end
|
2009-06-22 15:39:28 +02:00
|
|
|
|
|
|
|
def remember_where_we_left_off(results, page)
|
|
|
|
last_row = results['rows'].last
|
2009-07-13 11:58:48 +02:00
|
|
|
if last_row
|
|
|
|
@last_key = last_row['key']
|
|
|
|
@last_docid = last_row['id']
|
|
|
|
end
|
2009-06-22 15:39:28 +02:00
|
|
|
@last_page = page
|
|
|
|
end
|
2009-06-04 20:43:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2009-07-13 11:58:48 +02:00
|
|
|
end
|