Better integration with couchrest views. More tests, doc, and some cleanup still needed.

This commit is contained in:
John Wood 2009-06-18 11:28:57 -05:00 committed by Matt Aimonetti
parent cf76466795
commit 5963f1d4f8
3 changed files with 72 additions and 25 deletions

View file

@ -27,23 +27,30 @@ module CouchRest
proxy.paginated_each(options, &block) proxy.paginated_each(options, &block)
end end
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
private private
def create_collection_proxy(options) def create_collection_proxy(options)
view_name, view_options = parse_view_options(options) view_name, view_options, design_doc = parse_view_options(options)
CollectionProxy.new(@database, view_name, view_options, self) CollectionProxy.new(@database, design_doc, view_name, view_options, self)
end end
def parse_view_options(options) def parse_view_options(options)
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys design_doc = options.delete(:design_doc)
options = options.symbolize_keys raise ArgumentError, 'design_doc is required' if design_doc.nil?
view_name = options.delete(:view_name) view_name = options.delete(:view_name)
raise ArgumentError, 'view_name is required' if view_name.nil? raise ArgumentError, 'view_name is required' if view_name.nil?
view_options = options.delete(:view_options) || {} view_options = options.delete(:view_options) || {}
default_view_options = (design_doc['views'][view_name.to_s] && design_doc['views'][view_name.to_s]["couchrest-defaults"]) || {}
view_options = default_view_options.merge(view_options).merge(options)
[view_name, view_options] [view_name, view_options, design_doc]
end end
end end
@ -51,17 +58,24 @@ module CouchRest
alias_method :proxy_respond_to?, :respond_to? alias_method :proxy_respond_to?, :respond_to?
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ } instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
def initialize(database, view_name, view_options = {}, container_class = nil) def initialize(database, design_doc, view_name, view_options = {}, container_class = nil)
@container_class = container_class raise ArgumentError, "database is a required parameter" if database.nil?
@database = database @database = database
@view_name = view_name
@view_options = view_options @view_options = view_options
@container_class = container_class
if design_doc.class == Design
@view_name = "#{design_doc.name}/#{view_name}"
else
@view_name = "#{design_doc}/#{view_name}"
end
end end
def paginate(options = {}) def paginate(options = {})
page, per_page = parse_options(options) page, per_page = parse_options(options)
rows = @database.view(@view_name, @view_options.merge(pagination_options(page, per_page)))['rows'] results = @database.view(@view_name, @view_options.merge(pagination_options(page, per_page)))
convert_to_container_array(rows) convert_to_container_array(results)
end end
def paginated_each(options = {}, &block) def paginated_each(options = {}, &block)
@ -99,8 +113,8 @@ module CouchRest
def load_target def load_target
unless loaded? unless loaded?
rows = @database.view(@view_name, @view_options)['rows'] results = @database.view(@view_name, @view_options)
@target = convert_to_container_array(rows) @target = convert_to_container_array(results)
end end
@loaded = true @loaded = true
@target @target
@ -126,12 +140,12 @@ module CouchRest
@target.inspect @target.inspect
end end
def convert_to_container_array(rows) def convert_to_container_array(results)
return rows if @container_class.nil? if @container_class.nil?
results
container = [] else
rows.each { |row| container << @container_class.new(row['value']) } unless rows.nil? results['rows'].collect { |row| @container_class.new(row['doc']) } unless results['rows'].nil?
container end
end end
def pagination_options(page, per_page) def pagination_options(page, per_page)
@ -139,12 +153,9 @@ module CouchRest
end end
def parse_options(options) def parse_options(options)
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
options = options.symbolize_keys
page = options[:page] || 1 page = options[:page] || 1
per_page = options[:per_page] || 30 per_page = options[:per_page] || 30
[page, per_page] [page.to_i, per_page.to_i]
end end
end end

View file

@ -141,8 +141,12 @@ module CouchRest
fetch_view(db, name, opts, &block) fetch_view(db, name, opts, &block)
else else
begin begin
if block.nil?
collection_proxy_for(design_doc, name, opts.merge({:include_docs => true}))
else
view = fetch_view db, name, opts.merge({:include_docs => true}), &block view = fetch_view db, name, opts.merge({:include_docs => true}), &block
view['rows'].collect{|r|new(r['doc'])} if view['rows'] view['rows'].collect{|r|new(r['doc'])} if view['rows']
end
rescue rescue
# fallback for old versions of couchdb that don't # fallback for old versions of couchdb that don't
# have include_docs support # have include_docs support

View file

@ -338,4 +338,36 @@ describe "ExtendedDocument views" do
end end
end end
describe "with a collection" do
before(:all) do
reset_test_db!
@titles = ["very uniq one", "really interesting", "some fun",
"really awesome", "crazy bob", "this rocks", "super rad"]
@titles.each_with_index do |title,i|
a = Article.new(:title => title, :date => Date.today)
a.save
end
end
it "should return an array of 7 Article objects" do
articles = Article.by_date :key => Date.today
articles.class.should == Array
articles.size.should == 7
end
it "should get a subset of articles using paginate" do
articles = Article.by_date :key => Date.today
articles.paginate(:page => 1, :per_page => 3).size.should == 3
articles.paginate(:page => 2, :per_page => 3).size.should == 3
articles.paginate(:page => 3, :per_page => 3).size.should == 1
end
# it "should get all articles, a few at a time, using paginated each" do
#
# end
# it "should provide a class method to access the collection directly" do
#
# end
# it "should provide class methods for pagination" do
#
# end
end
end end