From 6851c7a2be82298bed8ecb6dae64836c23e576d8 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Tue, 14 Oct 2008 15:08:17 -0700 Subject: [PATCH] views accept blocks --- lib/couchrest/core/database.rb | 1 - lib/couchrest/core/model.rb | 7 +---- lib/couchrest/helper/streamer.rb | 16 +++++++++--- spec/couchapp_spec.rb | 2 +- spec/couchrest/core/database_spec.rb | 4 +-- spec/couchrest/core/model_spec.rb | 2 +- spec/couchrest/helpers/file_manager_spec.rb | 12 ++++----- .../fixtures/couchapp/attachments/index.html | 26 ------------------- .../fixtures/couchapp/views/example-map.js | 8 ------ .../fixtures/couchapp/views/example-reduce.js | 10 ------- spec/spec_helper.rb | 2 ++ 11 files changed, 25 insertions(+), 65 deletions(-) delete mode 100644 spec/couchrest/helpers/fixtures/couchapp/attachments/index.html delete mode 100644 spec/couchrest/helpers/fixtures/couchapp/views/example-map.js delete mode 100644 spec/couchrest/helpers/fixtures/couchapp/views/example-reduce.js diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb index 247410b..fc0d0e2 100644 --- a/lib/couchrest/core/database.rb +++ b/lib/couchrest/core/database.rb @@ -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 diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 7a9c62e..7d1678f 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -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 diff --git a/lib/couchrest/helper/streamer.rb b/lib/couchrest/helper/streamer.rb index a237cf7..6d48ca0 100644 --- a/lib/couchrest/helper/streamer.rb +++ b/lib/couchrest/helper/streamer.rb @@ -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 \ No newline at end of file diff --git a/spec/couchapp_spec.rb b/spec/couchapp_spec.rb index aa3e0e7..6fc61d6 100644 --- a/spec/couchapp_spec.rb +++ b/spec/couchapp_spec.rb @@ -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}` diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index a21e17c..a51ee26 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -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 diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 981ad5c..9967687 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -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 diff --git a/spec/couchrest/helpers/file_manager_spec.rb b/spec/couchrest/helpers/file_manager_spec.rb index 65a5118..59116f3 100644 --- a/spec/couchrest/helpers/file_manager_spec.rb +++ b/spec/couchrest/helpers/file_manager_spec.rb @@ -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 diff --git a/spec/couchrest/helpers/fixtures/couchapp/attachments/index.html b/spec/couchrest/helpers/fixtures/couchapp/attachments/index.html deleted file mode 100644 index 8b3c2b7..0000000 --- a/spec/couchrest/helpers/fixtures/couchapp/attachments/index.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - Generated CouchApp - - - -

Generated CouchApp

- - - - - - - \ No newline at end of file diff --git a/spec/couchrest/helpers/fixtures/couchapp/views/example-map.js b/spec/couchrest/helpers/fixtures/couchapp/views/example-map.js deleted file mode 100644 index 878684d..0000000 --- a/spec/couchrest/helpers/fixtures/couchapp/views/example-map.js +++ /dev/null @@ -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); -}; diff --git a/spec/couchrest/helpers/fixtures/couchapp/views/example-reduce.js b/spec/couchrest/helpers/fixtures/couchapp/views/example-reduce.js deleted file mode 100644 index 23eea60..0000000 --- a/spec/couchrest/helpers/fixtures/couchapp/views/example-reduce.js +++ /dev/null @@ -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; - } -}; \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 22063b9..3cb4791 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,6 @@ require File.dirname(__FILE__) + '/../lib/couchrest' +FIXTURE_PATH = File.dirname(__FILE__) + '/fixtures' + COUCHHOST = "http://localhost:5984" TESTDB = 'couchrest-test' \ No newline at end of file