1 #!/usr/bin/env ruby -wKU
6 # Highly experimental view requestor for CouchDb
8 # >> pp db.post("_temp_view", "proc{|doc| if doc['foo'] =~ /ba/; return doc;end }")
9 # #<CouchObject::Response:0x616944
12 # [{"_rev"=>928806717,
13 # "_id"=>"28D568C5992CBD2B4711F57225A19517",
15 # {"_id"=>"28D568C5992CBD2B4711F57225A19517",
18 # {"_rev"=>-1696868121,
19 # "_id"=>"601D858DB2E298EFC4BBA92A11760D1E",
21 # {"_id"=>"601D858DB2E298EFC4BBA92A11760D1E",
22 # "_rev"=>-1696868121,
24 # {"_rev"=>-2093091288,
25 # "_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E",
27 # {"_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E",
28 # "_rev"=>-2093091288,
32 # "view"=>"_temp_view:proc{|doc| if doc['foo'] =~ /ba/; return doc;end }"},
33 # @response=#<Net::HTTPOK 200 OK readbody=true>>
38 #$stderr.puts "@@@==> got: #{cmd.inspect}"
46 # second arg is a string that will compile to a function
47 callable
= eval(cmd
[1])
48 if callable
.respond_to
?(:call)
49 $callables << callable
52 puts JSON
.unparse(["error", "String must respond_to #call, eg proc{|doc| doc.title == 'foo' }"])
59 $callables.each
do |callable
|
61 result
= callable
.call(doc
)
63 results
<< 0 # indicate no match
64 elsif result
== 0 # not sure if this one is correct
65 results
<< {"value"=>0}
70 # An error mapping the document. Indicate no match.
74 puts JSON
.unparse(results
)