views accept blocks
This commit is contained in:
parent
254eb20161
commit
6851c7a2be
|
@ -60,7 +60,6 @@ module CouchRest
|
|||
CouchRest.post(url, {:keys => keys})
|
||||
else
|
||||
if block_given?
|
||||
puts "streamer"
|
||||
@streamer.view(name, params, &block)
|
||||
else
|
||||
CouchRest.get url
|
||||
|
|
|
@ -302,9 +302,6 @@ module CouchRest
|
|||
|
||||
self.meta_class.instance_eval do
|
||||
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[:raw] = true if query[:reduce]
|
||||
unless design_doc_fresh
|
||||
|
@ -312,7 +309,7 @@ module CouchRest
|
|||
end
|
||||
raw = query.delete(:raw)
|
||||
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
|
||||
|
@ -326,7 +323,6 @@ module CouchRest
|
|||
def view name, query={}, &block
|
||||
name = name.to_s
|
||||
view_name = "#{design_doc_slug}/#{name}"
|
||||
puts view_name
|
||||
fetch_view_with_docs(view_name, query, true, &block)
|
||||
end
|
||||
|
||||
|
@ -352,7 +348,6 @@ module CouchRest
|
|||
def fetch_view view_name, opts, &block
|
||||
retryable = true
|
||||
begin
|
||||
puts "block" if block
|
||||
database.view(view_name, opts, &block)
|
||||
# the design doc could have been deleted by a rouge process
|
||||
rescue RestClient::ResourceNotFound => e
|
||||
|
|
|
@ -9,18 +9,16 @@ module CouchRest
|
|||
def view name, params = nil, &block
|
||||
urlst = /^_/.match(name) ? "#{@db.root}/#{name}" : "#{@db.root}/_view/#{name}"
|
||||
url = CouchRest.paramify_url urlst, params
|
||||
# puts "stream #{url}"
|
||||
first = nil
|
||||
IO.popen("curl --silent #{url}") do |view|
|
||||
first = view.gets # discard header
|
||||
# puts first
|
||||
while line = view.gets
|
||||
# puts line
|
||||
row = parse_line(line)
|
||||
block.call row
|
||||
end
|
||||
end
|
||||
# parse_line(line)
|
||||
first
|
||||
parse_first(first)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -31,6 +29,16 @@ module CouchRest
|
|||
JSON.parse($1)
|
||||
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
|
|
@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|||
|
||||
describe "couchapp" 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'
|
||||
`rm -rf #{@fixdir}`
|
||||
`mkdir -p #{@fixdir}`
|
||||
|
|
|
@ -123,7 +123,7 @@ describe CouchRest::Database do
|
|||
rows << row
|
||||
end
|
||||
rows.length.should == 4
|
||||
rs.should == 'a parsed thing. not that easy.'
|
||||
rs["total_rows"].should == 3
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -223,7 +223,7 @@ describe CouchRest::Database do
|
|||
|
||||
describe "PUT attachment from file" do
|
||||
before(:each) do
|
||||
filename = File.dirname(__FILE__) + '/../../fixtures/attachments/couchdb.png'
|
||||
filename = FIXTURE_PATH + '/attachments/couchdb.png'
|
||||
@file = File.open(filename)
|
||||
end
|
||||
after(:each) do
|
||||
|
|
|
@ -440,7 +440,6 @@ describe CouchRest::Model do
|
|||
end
|
||||
it "should yield" do
|
||||
courses = []
|
||||
puts "Course.view(:by_title)"
|
||||
rs = Course.by_title # remove me
|
||||
Course.view(:by_title) do |course|
|
||||
# puts "course"
|
||||
|
@ -459,6 +458,7 @@ end
|
|||
duck["dept"].should == true
|
||||
end
|
||||
it "should make the design doc" do
|
||||
@as = Course.by_dept
|
||||
@doc = Course.design_doc
|
||||
@doc["views"]["by_dept"]["map"].should_not include("couchrest")
|
||||
end
|
||||
|
|
|
@ -26,7 +26,7 @@ end
|
|||
|
||||
describe CouchRest::FileManager, "generating an app" do
|
||||
before(:all) do
|
||||
@appdir = File.expand_path(File.dirname(__FILE__)) + '/fixtures/couchapp'
|
||||
@appdir = FIXTURE_PATH + '/couchapp'
|
||||
`rm -rf #{@appdir}`
|
||||
`mkdir -p #{@appdir}`
|
||||
CouchRest::FileManager.generate_app(@appdir)
|
||||
|
@ -56,7 +56,7 @@ describe CouchRest::FileManager, "pushing an app" do
|
|||
@db.delete! 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}`
|
||||
`mkdir -p #{@appdir}`
|
||||
CouchRest::FileManager.generate_app(@appdir)
|
||||
|
@ -86,7 +86,7 @@ describe CouchRest::FileManager, "pushing views" do
|
|||
@db = @cr.create_db(TESTDB) rescue nil
|
||||
|
||||
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
|
||||
@view_dir = File.dirname(__FILE__) + '/fixtures/views'
|
||||
@view_dir = FIXTURE_PATH + '/views'
|
||||
ds = @fm.push_views(@view_dir)
|
||||
@design = @db.get("_design/test_view")
|
||||
end
|
||||
|
@ -119,7 +119,7 @@ describe CouchRest::FileManager, "pushing a directory with id" do
|
|||
@db = @cr.create_db(TESTDB) rescue nil
|
||||
|
||||
@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')
|
||||
end
|
||||
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
|
||||
|
||||
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
|
||||
@push_dir = File.dirname(__FILE__) + '/fixtures/attachments'
|
||||
@push_dir = FIXTURE_PATH + '/attachments'
|
||||
ds = @fm.push_directory(@push_dir)
|
||||
end
|
||||
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
|
||||
|
||||
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
|
||||
@push_dir = File.dirname(__FILE__) + '/fixtures/attachments/'
|
||||
@push_dir = FIXTURE_PATH + '/attachments/'
|
||||
ds = @fm.push_directory(@push_dir)
|
||||
end
|
||||
it "should use the dirname" do
|
||||
|
|
|
@ -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>
|
|
@ -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);
|
||||
};
|
|
@ -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;
|
||||
}
|
||||
};
|
|
@ -1,4 +1,6 @@
|
|||
require File.dirname(__FILE__) + '/../lib/couchrest'
|
||||
|
||||
FIXTURE_PATH = File.dirname(__FILE__) + '/fixtures'
|
||||
|
||||
COUCHHOST = "http://localhost:5984"
|
||||
TESTDB = 'couchrest-test'
|
Loading…
Reference in a new issue