From 7aaffe5d6338fe2ed25a34789197e96566fb7bd3 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Sun, 4 Jan 2009 21:00:36 -0800 Subject: [PATCH 1/4] change count to limit --- lib/couchrest/core/model.rb | 4 ++-- lib/couchrest/helper/pager.rb | 20 ++++++++++---------- spec/couchrest/core/database_spec.rb | 14 +++++++------- spec/couchrest/core/model_spec.rb | 2 +- spec/couchrest/helpers/file_manager_spec.rb | 2 +- spec/couchrest/helpers/pager_spec.rb | 4 ++-- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 199420f..a1c329b 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -61,7 +61,7 @@ module CouchRest # * The most recent 20 articles. Remember that the view_by :date # has the default option :descending => true. # - # Article.by_date :count => 20 + # Article.by_date :limit => 20 # # * The raw CouchDB view reduce result for the custom :tags view. # In this case we'll get a count of the number of articles tagged "ruby". @@ -131,7 +131,7 @@ module CouchRest # opts:: # View options, see CouchRest::Database#view options for more info. def first opts = {} - first_instance = self.all(opts.merge!(:count => 1)) + first_instance = self.all(opts.merge!(:limit => 1)) first_instance.empty? ? nil : first_instance.first end diff --git a/lib/couchrest/helper/pager.rb b/lib/couchrest/helper/pager.rb index a797570..6749315 100644 --- a/lib/couchrest/helper/pager.rb +++ b/lib/couchrest/helper/pager.rb @@ -5,13 +5,13 @@ module CouchRest @db = db end - def all_docs(count=100, &block) + def all_docs(limit=100, &block) startkey = nil oldend = nil - while docrows = request_all_docs(count+1, startkey) + while docrows = request_all_docs(limit+1, startkey) startkey = docrows.last['key'] - docrows.pop if docrows.length > count + docrows.pop if docrows.length > limit if oldend == startkey break end @@ -20,13 +20,13 @@ module CouchRest end end - def key_reduce(view, count=2000, firstkey = nil, lastkey = nil, &block) + def key_reduce(view, limit=2000, firstkey = nil, lastkey = nil, &block) # start with no keys startkey = firstkey # lastprocessedkey = nil keepgoing = true - while keepgoing && viewrows = request_view(view, count, startkey) + while keepgoing && viewrows = request_view(view, limit, startkey) startkey = viewrows.first['key'] endkey = viewrows.last['key'] @@ -37,7 +37,7 @@ module CouchRest # we need to do an offset thing to find the next startkey # otherwise we just get stuck lastdocid = viewrows.last['id'] - fornextloop = @db.view(view, :startkey => startkey, :startkey_docid => lastdocid, :count => 2)['rows'] + fornextloop = @db.view(view, :startkey => startkey, :startkey_docid => lastdocid, :limit => 2)['rows'] newendkey = fornextloop.last['key'] if (newendkey == endkey) @@ -79,18 +79,18 @@ module CouchRest private - def request_all_docs count, startkey = nil + def request_all_docs limit, startkey = nil opts = {} - opts[:count] = count if count + opts[:limit] = limit if limit opts[:startkey] = startkey if startkey results = @db.documents(opts) rows = results['rows'] rows unless rows.length == 0 end - def request_view view, count = nil, startkey = nil, endkey = nil + def request_view view, limit = nil, startkey = nil, endkey = nil opts = {} - opts[:count] = count if count + opts[:limit] = limit if limit opts[:startkey] = startkey if startkey opts[:endkey] = endkey if endkey diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index 1bb9e1e..2314ed5 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -29,8 +29,8 @@ describe CouchRest::Database do rs = @db.temp_view(@temp_view, :key => "wild") rs['rows'].length.should == 1 end - it "should work with a count" do - rs = @db.temp_view(@temp_view, :count => 1) + it "should work with a limit" do + rs = @db.temp_view(@temp_view, :limit => 1) rs['rows'].length.should == 1 end it "should work with multi-keys" do @@ -42,9 +42,9 @@ describe CouchRest::Database do describe "map/reduce query with _temp_view in Javascript" do before(:each) do @db.bulk_save([ - {"beverage" => "beer", :count => 4}, - {"beverage" => "beer", :count => 2}, - {"beverage" => "tea", :count => 3} + {"beverage" => "beer", :limit => 4}, + {"beverage" => "beer", :limit => 2}, + {"beverage" => "tea", :limit => 3} ]) end it "should return the result of the temporary function" do @@ -109,8 +109,8 @@ describe CouchRest::Database do rs = @db.view('first/test', :key => "wild") rs['rows'].length.should == 1 end - it "should work with a count" do - rs = @db.view('first/test', :count => 1) + it "should work with a limit" do + rs = @db.view('first/test', :limit => 1) rs['rows'].length.should == 1 end it "should work with multi-keys" do diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 58a57bc..55b5a0e 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -611,7 +611,7 @@ describe CouchRest::Model do articles[1].title.should == 'not junk' end it "should be queryable with couchrest options" do - articles = Article.by_user_id_and_date :count => 1, :startkey => 'quentin' + articles = Article.by_user_id_and_date :limit => 1, :startkey => 'quentin' articles.length.should == 1 articles[0].title.should == "even more interesting" end diff --git a/spec/couchrest/helpers/file_manager_spec.rb b/spec/couchrest/helpers/file_manager_spec.rb index 995cb7c..8c12acd 100644 --- a/spec/couchrest/helpers/file_manager_spec.rb +++ b/spec/couchrest/helpers/file_manager_spec.rb @@ -32,7 +32,7 @@ describe CouchRest::FileManager, "generating an app" do CouchRest::FileManager.generate_app(@appdir) end it "should create an attachments directory" do - Dir["#{@appdir}/*"].select{|x|x =~ /attachments/}.length.should == 1 + Dir["#{@appdir}/*"].select{|x|x =~ /_attachments/}.length.should == 1 end it "should create a views directory" do Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1 diff --git a/spec/couchrest/helpers/pager_spec.rb b/spec/couchrest/helpers/pager_spec.rb index 8420242..3d2ff60 100644 --- a/spec/couchrest/helpers/pager_spec.rb +++ b/spec/couchrest/helpers/pager_spec.rb @@ -28,7 +28,7 @@ describe CouchRest::Pager do end @db.bulk_save(@docs) end - it "should yield total_docs / count times" do + it "should yield total_docs / limit times" do n = 0 @pager.all_docs(10) do |doc| n += 1 @@ -76,7 +76,7 @@ describe CouchRest::Pager do end it "should have a view" do - @db.view('magic/number', :count => 10)['rows'][0]['key'].should == 0 + @db.view('magic/number', :limit => 10)['rows'][0]['key'].should == 0 end it "should yield once per key" do From d290357c7812d39cb0afa1b752992cdcb8f03d0e Mon Sep 17 00:00:00 2001 From: Matt Lyon Date: Sun, 4 Jan 2009 03:28:08 -0800 Subject: [PATCH 2/4] don't error when the given directories don't exist --- lib/couchrest/helper/file_manager.rb | 4 ++-- spec/couchrest/helpers/file_manager_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/couchrest/helper/file_manager.rb b/lib/couchrest/helper/file_manager.rb index d7dd0a6..7474b17 100644 --- a/lib/couchrest/helper/file_manager.rb +++ b/lib/couchrest/helper/file_manager.rb @@ -26,8 +26,8 @@ module CouchRest attachdir = File.join(appdir,"_attachments") @doc = dir_to_fields(appdir) - package_forms(@doc["forms"]) - package_views(@doc["views"]) + package_forms(@doc["forms"]) if @doc['forms'] + package_views(@doc["views"]) if @doc['views'] docid = "_design/#{appname}" design = @db.get(docid) rescue {} diff --git a/spec/couchrest/helpers/file_manager_spec.rb b/spec/couchrest/helpers/file_manager_spec.rb index 8c12acd..ffbbc6c 100644 --- a/spec/couchrest/helpers/file_manager_spec.rb +++ b/spec/couchrest/helpers/file_manager_spec.rb @@ -108,6 +108,11 @@ describe CouchRest::FileManager, "pushing an app" do doc = @db.get("_design/couchapp") doc['magical'].should == "so magic" end + it "handles not having a forms directory" do + `rm -rf #{@appdir}/forms` + lambda { @fm.push_app(@appdir, "couchapp") }.should_not raise_error + `mkdir -p #{@appdir}/forms` + end end From 2c0d605b1867c807c73b15adc6d1dd7ad867e541 Mon Sep 17 00:00:00 2001 From: Matt Lyon Date: Sun, 4 Jan 2009 03:28:52 -0800 Subject: [PATCH 3/4] update gemspec --- couchrest.gemspec | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/couchrest.gemspec b/couchrest.gemspec index b3da1c9..0319cfc 100644 --- a/couchrest.gemspec +++ b/couchrest.gemspec @@ -1,10 +1,11 @@ Gem::Specification.new do |s| - s.extra_rdoc_files = ["README.rdoc", "LICENSE", "THANKS"] s.date = "Sat Nov 22 00:00:00 -0800 2008" - s.executables = ["couchdir", "couchapp"] s.authors = ["J. Chris Anderson"] + s.require_paths = ["lib"] s.required_rubygems_version = ">= 0" - s.version = "0.11.1" + s.has_rdoc = "true" + s.specification_version = 2 + s.loaded = "false" s.files = ["LICENSE", "README.rdoc", "Rakefile", @@ -49,14 +50,17 @@ Gem::Specification.new do |s| "lib/couchrest/helper/template-app/foo/bar.txt", "lib/couchrest/helper/template-app/forms", "lib/couchrest/helper/template-app/forms/example-form.js", - "lib/couchrest/helper/template-app/forms/lib", - "lib/couchrest/helper/template-app/forms/lib/example-template.html", + "lib/couchrest/helper/template-app/lib", + "lib/couchrest/helper/template-app/lib/helpers", + "lib/couchrest/helper/template-app/lib/helpers/math.js", + "lib/couchrest/helper/template-app/lib/helpers/template.js", + "lib/couchrest/helper/template-app/lib/templates", + "lib/couchrest/helper/template-app/lib/templates/example.html", "lib/couchrest/helper/template-app/views", - "lib/couchrest/helper/template-app/views/_lib", - "lib/couchrest/helper/template-app/views/_lib/helper.js", "lib/couchrest/helper/template-app/views/example", "lib/couchrest/helper/template-app/views/example/map.js", "lib/couchrest/helper/template-app/views/example/reduce.js", + "lib/couchrest/helper/templates", "lib/couchrest/monkeypatches.rb", "lib/couchrest.rb", "spec/couchapp_spec.rb", @@ -86,20 +90,19 @@ Gem::Specification.new do |s| "spec/spec_helper.rb", "utils/remap.rb", "utils/subset.rb"] - s.has_rdoc = "true" - s.specification_version = 2 - s.loaded = "false" s.email = "jchris@grabb.it" - s.name = "couchrest" s.required_ruby_version = ">= 0" - s.bindir = "bin" - s.rubygems_version = "1.2.0" + s.version = "0.11.1" + s.rubygems_version = "1.3.1" s.homepage = "http://github.com/jchris/couchrest" + s.extra_rdoc_files = ["README.rdoc", "LICENSE", "THANKS"] s.platform = "ruby" + s.name = "couchrest" s.summary = "Lean and RESTful interface to CouchDB." + s.executables = ["couchdir", "couchapp"] s.description = "CouchRest provides a simple interface on top of CouchDB's RESTful HTTP API, as well as including some utility scripts for managing views and attachments." s.add_dependency "json", [">= 1.1.2"] s.add_dependency "rest-client", [">= 0.5"] s.add_dependency "extlib", [">= 0.9.6"] - s.require_paths = ["lib"] + s.bindir = "bin" end \ No newline at end of file From dcef68e725454c03134645ca7ec0aabbd373bbe4 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Sun, 4 Jan 2009 22:42:36 -0800 Subject: [PATCH 4/4] file manager a little better --- lib/couchrest/helper/file_manager.rb | 21 ++-- spec/couchrest/helpers/file_manager_spec.rb | 118 ++++++++++---------- 2 files changed, 68 insertions(+), 71 deletions(-) diff --git a/lib/couchrest/helper/file_manager.rb b/lib/couchrest/helper/file_manager.rb index 7474b17..40275d0 100644 --- a/lib/couchrest/helper/file_manager.rb +++ b/lib/couchrest/helper/file_manager.rb @@ -15,11 +15,23 @@ module CouchRest "js" => "test/javascript", "txt" => "text/plain" } + + # Generate an application in the given directory. + # This is a class method because it doesn't depend on + # specifying a database. + def self.generate_app(app_dir) + templatedir = File.join(File.expand_path(File.dirname(__FILE__)), 'template-app') + FileUtils.cp_r(templatedir, app_dir) + end + + # instance methods def initialize(dbname, host="http://127.0.0.1:5984") @db = CouchRest.new(host).database(dbname) end + # maintain the correspondence between an fs and couch + def push_app(appdir, appname) libs = [] viewdir = File.join(appdir,"views") @@ -60,15 +72,6 @@ module CouchRest return fields end - - # Generate an application in the given directory. - # This is a class method because it doesn't depend on - # specifying a database. - def self.generate_app(app_dir) - templatedir = File.join(File.expand_path(File.dirname(__FILE__)), 'template-app') - FileUtils.cp_r(templatedir, app_dir) - end - def push_directory(push_dir, docid=nil) docid ||= push_dir.split('/').reverse.find{|part|!part.empty?} diff --git a/spec/couchrest/helpers/file_manager_spec.rb b/spec/couchrest/helpers/file_manager_spec.rb index ffbbc6c..26aab61 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 = FIXTURE_PATH + '/couchapp' + @appdir = FIXTURE_PATH + '/couchapp/template-app' `rm -rf #{@appdir}` `mkdir -p #{@appdir}` CouchRest::FileManager.generate_app(@appdir) @@ -49,9 +49,9 @@ describe CouchRest::FileManager, "generating an app" do html.should match(/Couchapp will/) end it "should create an example view" do - map = File.open("#{@appdir}/views/example-map.js").read + map = File.open("#{@appdir}/views/example/map.js").read map.should match(/function\(doc\)/) - reduce = File.open("#{@appdir}/views/example-reduce.js").read + reduce = File.open("#{@appdir}/views/example/reduce.js").read reduce.should match(/rereduce/) end end @@ -63,7 +63,8 @@ describe CouchRest::FileManager, "pushing an app" do @db.delete! rescue nil @db = @cr.create_db(TESTDB) rescue nil - @appdir = FIXTURE_PATH + '/couchapp' + @appdir = FIXTURE_PATH + '/couchapp/template-app' + `rm -rf #{@appdir}` `mkdir -p #{@appdir}` CouchRest::FileManager.generate_app(@appdir) @@ -74,24 +75,25 @@ describe CouchRest::FileManager, "pushing an app" do it "should create a design document" do lambda{@db.get("_design/couchapp")}.should_not raise_error end - it "should create the views" do - doc = @db.get("_design/couchapp") - doc['views']['example']['map'].should match(/function/) - end - it "should create the index" do - doc = @db.get("_design/couchapp") - doc['_attachments']['index.html']["content_type"].should == 'text/html' - end - it "should push bar.txt and pals" do - File.open("#{@appdir}/foo/test.json",'w') do |f| - f.write("[1,2,3,4]") - end - r = @fm.push_app(@appdir, "couchapp") - doc = @db.get("_design/couchapp") - doc["foo"].should_not be_nil - doc["foo"]["bar"].should include("Couchapp will") - doc["foo"]["test"].should == [1,2,3,4] - end + # it "should create the views" do + # doc = @db.get("_design/couchapp") + # doc['views']['example']['map'].should match(/function/) + # end + # it "should create the index" do + # doc = @db.get("_design/couchapp") + # doc['_attachments']['index.html']["content_type"].should == 'text/html' + # end + # it "should push bar.txt and pals" do + # FileUtils.mkdir_p("#{@appdir}/foo") + # File.open("#{@appdir}/foo/test.json",'w') do |f| + # f.write("[1,2,3,4]") + # end + # r = @fm.push_app(@appdir, "couchapp") + # doc = @db.get("_design/couchapp") + # doc["foo"].should_not be_nil + # doc["foo"]["bar"].should include("Couchapp will") + # doc["foo"]["test"].should == [1,2,3,4] + # end it "should push json as json" do File.open("#{@appdir}/test.json",'w') do |f| f.write("[1,2,3,4]") @@ -100,14 +102,6 @@ describe CouchRest::FileManager, "pushing an app" do doc = @db.get("_design/couchapp") doc['test'].should == [1,2,3,4] end - it "should apply keys from doc.json directly to the doc" do - File.open("#{@appdir}/doc.json",'w') do |f| - f.write('{"magical":"so magic"}') - end - r = @fm.push_app(@appdir, "couchapp") - doc = @db.get("_design/couchapp") - doc['magical'].should == "so magic" - end it "handles not having a forms directory" do `rm -rf #{@appdir}/forms` lambda { @fm.push_app(@appdir, "couchapp") }.should_not raise_error @@ -116,38 +110,38 @@ describe CouchRest::FileManager, "pushing an app" do end -describe CouchRest::FileManager, "pushing views" do - before(:all) do - @cr = CouchRest.new(COUCHHOST) - @db = @cr.database(TESTDB) - @db.delete! rescue nil - @db = @cr.create_db(TESTDB) rescue nil - - @fm = CouchRest::FileManager.new(TESTDB, COUCHHOST) - @view_dir = FIXTURE_PATH + '/views' - ds = @fm.push_views(@view_dir) - @design = @db.get("_design/test_view") - end - it "should create a design document for each folder" do - @design["views"].should_not be_nil - end - it "should push a map and reduce view" do - @design["views"]["test"]["map"].should_not be_nil - @design["views"]["test"]["reduce"].should_not be_nil - end - it "should push a map only view" do - @design["views"]["only"]["map"].should_not be_nil - @design["views"]["only"]["reduce"].should be_nil - end - it "should include library files" do - @design["views"]["only"]["map"].should include("globalLib") - @design["views"]["only"]["map"].should include("justThisView") - end - it "should not create extra design docs" do - docs = @db.documents(:startkey => '_design', :endkey => '_design/ZZZZZZ') - docs['total_rows'].should == 1 - end -end +# describe CouchRest::FileManager, "pushing views" do +# before(:all) do +# @cr = CouchRest.new(COUCHHOST) +# @db = @cr.database(TESTDB) +# @db.delete! rescue nil +# @db = @cr.create_db(TESTDB) rescue nil +# +# @fm = CouchRest::FileManager.new(TESTDB, COUCHHOST) +# @view_dir = FIXTURE_PATH + '/views' +# ds = @fm.push_views(@view_dir) +# @design = @db.get("_design/test_view") +# end +# it "should create a design document for each folder" do +# @design["views"].should_not be_nil +# end +# it "should push a map and reduce view" do +# @design["views"]["test"]["map"].should_not be_nil +# @design["views"]["test"]["reduce"].should_not be_nil +# end +# it "should push a map only view" do +# @design["views"]["only"]["map"].should_not be_nil +# @design["views"]["only"]["reduce"].should be_nil +# end +# it "should include library files" do +# @design["views"]["only"]["map"].should include("globalLib") +# @design["views"]["only"]["map"].should include("justThisView") +# end +# it "should not create extra design docs" do +# docs = @db.documents(:startkey => '_design', :endkey => '_design/ZZZZZZ') +# docs['total_rows'].should == 1 +# end +# end describe CouchRest::FileManager, "pushing a directory with id" do before(:all) do