From 6995d478c952cbfe71e0df7569aa26960832c741 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Wed, 19 Mar 2008 18:10:16 -0700 Subject: [PATCH] temp view --- lib/database.rb | 8 ++++++++ spec/database_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/database.rb b/lib/database.rb index bece72c..41843eb 100644 --- a/lib/database.rb +++ b/lib/database.rb @@ -13,6 +13,14 @@ class CouchRest view "_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}" end diff --git a/spec/database_spec.rb b/spec/database_spec.rb index d100125..6d05118 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -16,6 +16,20 @@ describe CouchRest::Database do end end + describe "query with _temp_view in Javascript" do + before(:each) do + @db.bulk_save([ + {"wild" => "and random"}, + {"mild" => "yet local"}, + {"another" => ["set","of","keys"]} + ]) + end + it "should return the result of the temporary function" do + rs = @db.temp_view("function(doc){for(var w in doc){ if(!w.match(/^_/))map(w,doc[w])}}") + 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'}) @@ -120,9 +134,18 @@ describe CouchRest::Database do before(:each) do @db.save({'_id' => 'my-doc', 'will-exist' => 'here'}) @doc = @db.get('my-doc') + @docid = "http://example.com/stuff.cgi?things=and%20stuff" + @db.save({'_id' => @docid, 'now' => 'save'}) end it "should start with the document" do @doc['will-exist'].should == 'here' + @db.get(@docid)['now'].should == 'save' + end + it "should save with url id" do + doc = @db.get(@docid) + doc['yaml'] = ['json', 'word.'] + @db.save doc + @db.get(@docid)['yaml'].should == ['json', 'word.'] end it "should update the document" do @doc['them-keys'] = 'huge'