views accept blocks

This commit is contained in:
Chris Anderson 2008-10-14 15:08:17 -07:00
parent 254eb20161
commit 6851c7a2be
11 changed files with 25 additions and 65 deletions

View file

@ -60,7 +60,6 @@ module CouchRest
CouchRest.post(url, {:keys => keys}) CouchRest.post(url, {:keys => keys})
else else
if block_given? if block_given?
puts "streamer"
@streamer.view(name, params, &block) @streamer.view(name, params, &block)
else else
CouchRest.get url CouchRest.get url

View file

@ -302,9 +302,6 @@ module CouchRest
self.meta_class.instance_eval do self.meta_class.instance_eval do
define_method method_name do |*args| define_method method_name do |*args|
# block = args.pop if args.last.is_a?(Proc)
block = nil
puts "block" if block_given?
query = opts.merge(args[0] || {}) query = opts.merge(args[0] || {})
query[:raw] = true if query[:reduce] query[:raw] = true if query[:reduce]
unless design_doc_fresh unless design_doc_fresh
@ -312,7 +309,7 @@ module CouchRest
end end
raw = query.delete(:raw) raw = query.delete(:raw)
view_name = "#{design_doc_slug}/#{method_name}" view_name = "#{design_doc_slug}/#{method_name}"
fetch_view_with_docs(view_name, query, raw, &block) fetch_view_with_docs(view_name, query, raw)
end end
end end
end end
@ -326,7 +323,6 @@ module CouchRest
def view name, query={}, &block def view name, query={}, &block
name = name.to_s name = name.to_s
view_name = "#{design_doc_slug}/#{name}" view_name = "#{design_doc_slug}/#{name}"
puts view_name
fetch_view_with_docs(view_name, query, true, &block) fetch_view_with_docs(view_name, query, true, &block)
end end
@ -352,7 +348,6 @@ module CouchRest
def fetch_view view_name, opts, &block def fetch_view view_name, opts, &block
retryable = true retryable = true
begin begin
puts "block" if block
database.view(view_name, opts, &block) database.view(view_name, opts, &block)
# the design doc could have been deleted by a rouge process # the design doc could have been deleted by a rouge process
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e

View file

@ -9,18 +9,16 @@ module CouchRest
def view name, params = nil, &block def view name, params = nil, &block
urlst = /^_/.match(name) ? "#{@db.root}/#{name}" : "#{@db.root}/_view/#{name}" urlst = /^_/.match(name) ? "#{@db.root}/#{name}" : "#{@db.root}/_view/#{name}"
url = CouchRest.paramify_url urlst, params url = CouchRest.paramify_url urlst, params
# puts "stream #{url}"
first = nil first = nil
IO.popen("curl --silent #{url}") do |view| IO.popen("curl --silent #{url}") do |view|
first = view.gets # discard header first = view.gets # discard header
# puts first
while line = view.gets while line = view.gets
# puts line
row = parse_line(line) row = parse_line(line)
block.call row block.call row
end end
end end
# parse_line(line) parse_first(first)
first
end end
private private
@ -31,6 +29,16 @@ module CouchRest
JSON.parse($1) JSON.parse($1)
end end
end end
def parse_first first
return nil unless first
parts = first.split(',')
parts.pop
line = parts.join(',')
JSON.parse("#{line}}")
rescue
nil
end
end end
end end

View file

@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
describe "couchapp" do describe "couchapp" do
before(:all) do before(:all) do
@fixdir = File.expand_path(File.dirname(__FILE__)) + '/fixtures/couchapp-test' @fixdir = FIXTURE_PATH + '/couchapp-test'
@couchapp = File.expand_path(File.dirname(__FILE__)) + '/../bin/couchapp' @couchapp = File.expand_path(File.dirname(__FILE__)) + '/../bin/couchapp'
`rm -rf #{@fixdir}` `rm -rf #{@fixdir}`
`mkdir -p #{@fixdir}` `mkdir -p #{@fixdir}`

View file

@ -123,7 +123,7 @@ describe CouchRest::Database do
rows << row rows << row
end end
rows.length.should == 4 rows.length.should == 4
rs.should == 'a parsed thing. not that easy.' rs["total_rows"].should == 3
end end
end end
@ -223,7 +223,7 @@ describe CouchRest::Database do
describe "PUT attachment from file" do describe "PUT attachment from file" do
before(:each) do before(:each) do
filename = File.dirname(__FILE__) + '/../../fixtures/attachments/couchdb.png' filename = FIXTURE_PATH + '/attachments/couchdb.png'
@file = File.open(filename) @file = File.open(filename)
end end
after(:each) do after(:each) do

View file

@ -440,7 +440,6 @@ describe CouchRest::Model do
end end
it "should yield" do it "should yield" do
courses = [] courses = []
puts "Course.view(:by_title)"
rs = Course.by_title # remove me rs = Course.by_title # remove me
Course.view(:by_title) do |course| Course.view(:by_title) do |course|
# puts "course" # puts "course"
@ -459,6 +458,7 @@ end
duck["dept"].should == true duck["dept"].should == true
end end
it "should make the design doc" do it "should make the design doc" do
@as = Course.by_dept
@doc = Course.design_doc @doc = Course.design_doc
@doc["views"]["by_dept"]["map"].should_not include("couchrest") @doc["views"]["by_dept"]["map"].should_not include("couchrest")
end end

View file

@ -26,7 +26,7 @@ end
describe CouchRest::FileManager, "generating an app" do describe CouchRest::FileManager, "generating an app" do
before(:all) do before(:all) do
@appdir = File.expand_path(File.dirname(__FILE__)) + '/fixtures/couchapp' @appdir = FIXTURE_PATH + '/couchapp'
`rm -rf #{@appdir}` `rm -rf #{@appdir}`
`mkdir -p #{@appdir}` `mkdir -p #{@appdir}`
CouchRest::FileManager.generate_app(@appdir) CouchRest::FileManager.generate_app(@appdir)
@ -56,7 +56,7 @@ describe CouchRest::FileManager, "pushing an app" do
@db.delete! rescue nil @db.delete! rescue nil
@db = @cr.create_db(TESTDB) rescue nil @db = @cr.create_db(TESTDB) rescue nil
@appdir = File.expand_path(File.dirname(__FILE__)) + '/fixtures/couchapp' @appdir = FIXTURE_PATH + '/couchapp'
`rm -rf #{@appdir}` `rm -rf #{@appdir}`
`mkdir -p #{@appdir}` `mkdir -p #{@appdir}`
CouchRest::FileManager.generate_app(@appdir) CouchRest::FileManager.generate_app(@appdir)
@ -86,7 +86,7 @@ describe CouchRest::FileManager, "pushing views" do
@db = @cr.create_db(TESTDB) rescue nil @db = @cr.create_db(TESTDB) rescue nil
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST) @fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
@view_dir = File.dirname(__FILE__) + '/fixtures/views' @view_dir = FIXTURE_PATH + '/views'
ds = @fm.push_views(@view_dir) ds = @fm.push_views(@view_dir)
@design = @db.get("_design/test_view") @design = @db.get("_design/test_view")
end end
@ -119,7 +119,7 @@ describe CouchRest::FileManager, "pushing a directory with id" do
@db = @cr.create_db(TESTDB) rescue nil @db = @cr.create_db(TESTDB) rescue nil
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST) @fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
@push_dir = File.dirname(__FILE__) + '/fixtures/attachments' @push_dir = FIXTURE_PATH + '/attachments'
ds = @fm.push_directory(@push_dir, 'attached') ds = @fm.push_directory(@push_dir, 'attached')
end end
it "should create a document for the folder" do it "should create a document for the folder" do
@ -143,7 +143,7 @@ describe CouchRest::FileManager, "pushing a directory without id" do
@db = @cr.create_db(TESTDB) rescue nil @db = @cr.create_db(TESTDB) rescue nil
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST) @fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
@push_dir = File.dirname(__FILE__) + '/fixtures/attachments' @push_dir = FIXTURE_PATH + '/attachments'
ds = @fm.push_directory(@push_dir) ds = @fm.push_directory(@push_dir)
end end
it "should use the dirname" do it "should use the dirname" do
@ -160,7 +160,7 @@ describe CouchRest::FileManager, "pushing a directory/ without id" do
@db = @cr.create_db(TESTDB) rescue nil @db = @cr.create_db(TESTDB) rescue nil
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST) @fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
@push_dir = File.dirname(__FILE__) + '/fixtures/attachments/' @push_dir = FIXTURE_PATH + '/attachments/'
ds = @fm.push_directory(@push_dir) ds = @fm.push_directory(@push_dir)
end end
it "should use the dirname" do it "should use the dirname" do

View file

@ -1,26 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Generated CouchApp</title>
<link rel="stylesheet" href="screen.css" type="text/css">
</head>
<body>
<h1>Generated CouchApp</h1>
<ul id="view"></ul>
</body>
<script src="/_utils/script/json2.js"></script>
<script src="/_utils/script/jquery.js?1.2.6"></script>
<script src="/_utils/script/jquery.couch.js?0.8.0"></script>
<script type="text/javascript" charset="utf-8">
$(function() {
var dbname = document.location.href.split('/')[3];
var design = unescape(document.location.href.split('/')[4]).split('/')[1];
var DB = $.couch.db(dbname);
DB.view(design+"/example",{success: function(json) {
$("#view").html(json.rows.map(function(row) {
return '<li>'+row.key+'</li>';
}).join(''));
}});
});
</script>
</html>

View file

@ -1,8 +0,0 @@
// an example map function, emits the doc id
// and the list of keys it contains
function(doc) {
var k, keys = []
for (k in doc) keys.push(k);
emit(doc._id, keys);
};

View file

@ -1,10 +0,0 @@
// example reduce function to count the
// number of rows in a given key range.
function(keys, values, rereduce) {
if (rereduce) {
return sum(values);
} else {
return values.length;
}
};

View file

@ -1,4 +1,6 @@
require File.dirname(__FILE__) + '/../lib/couchrest' require File.dirname(__FILE__) + '/../lib/couchrest'
FIXTURE_PATH = File.dirname(__FILE__) + '/fixtures'
COUCHHOST = "http://localhost:5984" COUCHHOST = "http://localhost:5984"
TESTDB = 'couchrest-test' TESTDB = 'couchrest-test'