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})
else
if block_given?
puts "streamer"
@streamer.view(name, params, &block)
else
CouchRest.get url

View file

@ -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

View file

@ -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