get views

This commit is contained in:
Chris Anderson 2008-03-19 22:38:01 -07:00
parent dfff6f3d8b
commit c3b6e3e2d0
3 changed files with 23 additions and 8 deletions

View file

@ -10,19 +10,15 @@ class CouchRest
end
def documents
view "_all_docs"
CouchRest.get "#{@root}/_all_docs"
end
def temp_view func
# CouchRest.post "#{@root}/_temp_view", func
# headers: {"Content-Type": "text/javascript"},
# ripping from CouchRest because leaky abstraction.
# payload = JSON.unparse doc if doc
JSON.parse(RestClient.post("#{@root}/_temp_view", func, {"Content-Type" => "text/javascript"}))
end
def view name
CouchRest.get "#{@root}/#{name}"
CouchRest.get "#{@root}/_view/#{name}"
end
def get id

View file

@ -30,6 +30,24 @@ describe CouchRest::Database do
end
end
describe "select from an existing view" do
before(:each) do
r = @db.save({"_id" => "_design/first", "views" => {"test" => "function(doc){for(var w in doc){ if(!w.match(/^_/))map(w,doc[w])}}"}})
@db.bulk_save([
{"wild" => "and random"},
{"mild" => "yet local"},
{"another" => ["set","of","keys"]}
])
end
it "should have the view" do
@db.get('_design/first')['views']['test'].should == "function(doc){for(var w in doc){ if(!w.match(/^_/))map(w,doc[w])}}"
end
it "should list from the view" do
rs = @db.view('first/test')
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
end
end
describe "GET (document by id) when the doc exists" do
before(:each) do
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})

View file

@ -1,14 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<title>CouchRest - a simple CouchDB Client</title>
</head>
<body>
<h1>
CouchRest - a simple CouchDB Client
</h1>
<p>
The stuff
CouchDB is sweet. It doesn't take much Ruby to make it easy.
</p>
<h2>Sweet and easy.</h2>
</body>
</html>