Merge branch 'master' of git://github.com/jchris/couchrest
* 'master' of git://github.com/jchris/couchrest: file manager a little better update gemspec don't error when the given directories don't exist change count to limit
This commit is contained in:
commit
71f5ea2a1b
7 changed files with 95 additions and 93 deletions
|
@ -61,7 +61,7 @@ module CouchRest
|
||||||
# * The most recent 20 articles. Remember that the <tt>view_by :date</tt>
|
# * The most recent 20 articles. Remember that the <tt>view_by :date</tt>
|
||||||
# has the default option <tt>:descending => true</tt>.
|
# has the default option <tt>:descending => true</tt>.
|
||||||
#
|
#
|
||||||
# Article.by_date :count => 20
|
# Article.by_date :limit => 20
|
||||||
#
|
#
|
||||||
# * The raw CouchDB view reduce result for the custom <tt>:tags</tt> view.
|
# * The raw CouchDB view reduce result for the custom <tt>:tags</tt> view.
|
||||||
# In this case we'll get a count of the number of articles tagged "ruby".
|
# In this case we'll get a count of the number of articles tagged "ruby".
|
||||||
|
@ -131,7 +131,7 @@ module CouchRest
|
||||||
# opts<Hash>::
|
# opts<Hash>::
|
||||||
# View options, see <tt>CouchRest::Database#view</tt> options for more info.
|
# View options, see <tt>CouchRest::Database#view</tt> options for more info.
|
||||||
def first opts = {}
|
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
|
first_instance.empty? ? nil : first_instance.first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,22 @@ module CouchRest
|
||||||
"txt" => "text/plain"
|
"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")
|
def initialize(dbname, host="http://127.0.0.1:5984")
|
||||||
@db = CouchRest.new(host).database(dbname)
|
@db = CouchRest.new(host).database(dbname)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# maintain the correspondence between an fs and couch
|
||||||
|
|
||||||
def push_app(appdir, appname)
|
def push_app(appdir, appname)
|
||||||
libs = []
|
libs = []
|
||||||
viewdir = File.join(appdir,"views")
|
viewdir = File.join(appdir,"views")
|
||||||
|
@ -60,15 +72,6 @@ module CouchRest
|
||||||
return fields
|
return fields
|
||||||
end
|
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)
|
def push_directory(push_dir, docid=nil)
|
||||||
docid ||= push_dir.split('/').reverse.find{|part|!part.empty?}
|
docid ||= push_dir.split('/').reverse.find{|part|!part.empty?}
|
||||||
|
|
||||||
|
|
|
@ -5,13 +5,13 @@ module CouchRest
|
||||||
@db = db
|
@db = db
|
||||||
end
|
end
|
||||||
|
|
||||||
def all_docs(count=100, &block)
|
def all_docs(limit=100, &block)
|
||||||
startkey = nil
|
startkey = nil
|
||||||
oldend = nil
|
oldend = nil
|
||||||
|
|
||||||
while docrows = request_all_docs(count+1, startkey)
|
while docrows = request_all_docs(limit+1, startkey)
|
||||||
startkey = docrows.last['key']
|
startkey = docrows.last['key']
|
||||||
docrows.pop if docrows.length > count
|
docrows.pop if docrows.length > limit
|
||||||
if oldend == startkey
|
if oldend == startkey
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -20,13 +20,13 @@ module CouchRest
|
||||||
end
|
end
|
||||||
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
|
# start with no keys
|
||||||
startkey = firstkey
|
startkey = firstkey
|
||||||
# lastprocessedkey = nil
|
# lastprocessedkey = nil
|
||||||
keepgoing = true
|
keepgoing = true
|
||||||
|
|
||||||
while keepgoing && viewrows = request_view(view, count, startkey)
|
while keepgoing && viewrows = request_view(view, limit, startkey)
|
||||||
startkey = viewrows.first['key']
|
startkey = viewrows.first['key']
|
||||||
endkey = viewrows.last['key']
|
endkey = viewrows.last['key']
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ module CouchRest
|
||||||
# we need to do an offset thing to find the next startkey
|
# we need to do an offset thing to find the next startkey
|
||||||
# otherwise we just get stuck
|
# otherwise we just get stuck
|
||||||
lastdocid = viewrows.last['id']
|
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']
|
newendkey = fornextloop.last['key']
|
||||||
if (newendkey == endkey)
|
if (newendkey == endkey)
|
||||||
|
@ -79,18 +79,18 @@ module CouchRest
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def request_all_docs count, startkey = nil
|
def request_all_docs limit, startkey = nil
|
||||||
opts = {}
|
opts = {}
|
||||||
opts[:count] = count if count
|
opts[:limit] = limit if limit
|
||||||
opts[:startkey] = startkey if startkey
|
opts[:startkey] = startkey if startkey
|
||||||
results = @db.documents(opts)
|
results = @db.documents(opts)
|
||||||
rows = results['rows']
|
rows = results['rows']
|
||||||
rows unless rows.length == 0
|
rows unless rows.length == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def request_view view, count = nil, startkey = nil, endkey = nil
|
def request_view view, limit = nil, startkey = nil, endkey = nil
|
||||||
opts = {}
|
opts = {}
|
||||||
opts[:count] = count if count
|
opts[:limit] = limit if limit
|
||||||
opts[:startkey] = startkey if startkey
|
opts[:startkey] = startkey if startkey
|
||||||
opts[:endkey] = endkey if endkey
|
opts[:endkey] = endkey if endkey
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,8 @@ describe CouchRest::Database do
|
||||||
rs = @db.temp_view(@temp_view, :key => "wild")
|
rs = @db.temp_view(@temp_view, :key => "wild")
|
||||||
rs['rows'].length.should == 1
|
rs['rows'].length.should == 1
|
||||||
end
|
end
|
||||||
it "should work with a count" do
|
it "should work with a limit" do
|
||||||
rs = @db.temp_view(@temp_view, :count => 1)
|
rs = @db.temp_view(@temp_view, :limit => 1)
|
||||||
rs['rows'].length.should == 1
|
rs['rows'].length.should == 1
|
||||||
end
|
end
|
||||||
it "should work with multi-keys" do
|
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
|
describe "map/reduce query with _temp_view in Javascript" do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@db.bulk_save([
|
@db.bulk_save([
|
||||||
{"beverage" => "beer", :count => 4},
|
{"beverage" => "beer", :limit => 4},
|
||||||
{"beverage" => "beer", :count => 2},
|
{"beverage" => "beer", :limit => 2},
|
||||||
{"beverage" => "tea", :count => 3}
|
{"beverage" => "tea", :limit => 3}
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
it "should return the result of the temporary function" do
|
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 = @db.view('first/test', :key => "wild")
|
||||||
rs['rows'].length.should == 1
|
rs['rows'].length.should == 1
|
||||||
end
|
end
|
||||||
it "should work with a count" do
|
it "should work with a limit" do
|
||||||
rs = @db.view('first/test', :count => 1)
|
rs = @db.view('first/test', :limit => 1)
|
||||||
rs['rows'].length.should == 1
|
rs['rows'].length.should == 1
|
||||||
end
|
end
|
||||||
it "should work with multi-keys" do
|
it "should work with multi-keys" do
|
||||||
|
|
|
@ -611,7 +611,7 @@ describe CouchRest::Model do
|
||||||
articles[1].title.should == 'not junk'
|
articles[1].title.should == 'not junk'
|
||||||
end
|
end
|
||||||
it "should be queryable with couchrest options" do
|
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.length.should == 1
|
||||||
articles[0].title.should == "even more interesting"
|
articles[0].title.should == "even more interesting"
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,13 +26,13 @@ end
|
||||||
|
|
||||||
describe CouchRest::FileManager, "generating an app" do
|
describe CouchRest::FileManager, "generating an app" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
@appdir = FIXTURE_PATH + '/couchapp'
|
@appdir = FIXTURE_PATH + '/couchapp/template-app'
|
||||||
`rm -rf #{@appdir}`
|
`rm -rf #{@appdir}`
|
||||||
`mkdir -p #{@appdir}`
|
`mkdir -p #{@appdir}`
|
||||||
CouchRest::FileManager.generate_app(@appdir)
|
CouchRest::FileManager.generate_app(@appdir)
|
||||||
end
|
end
|
||||||
it "should create an attachments directory" do
|
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
|
end
|
||||||
it "should create a views directory" do
|
it "should create a views directory" do
|
||||||
Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1
|
Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1
|
||||||
|
@ -49,9 +49,9 @@ describe CouchRest::FileManager, "generating an app" do
|
||||||
html.should match(/Couchapp will/)
|
html.should match(/Couchapp will/)
|
||||||
end
|
end
|
||||||
it "should create an example view" do
|
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\)/)
|
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/)
|
reduce.should match(/rereduce/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -63,7 +63,8 @@ describe CouchRest::FileManager, "pushing an app" do
|
||||||
@db.delete! rescue nil
|
@db.delete! rescue nil
|
||||||
@db = @cr.create_db(TESTDB) rescue nil
|
@db = @cr.create_db(TESTDB) rescue nil
|
||||||
|
|
||||||
@appdir = FIXTURE_PATH + '/couchapp'
|
@appdir = FIXTURE_PATH + '/couchapp/template-app'
|
||||||
|
|
||||||
`rm -rf #{@appdir}`
|
`rm -rf #{@appdir}`
|
||||||
`mkdir -p #{@appdir}`
|
`mkdir -p #{@appdir}`
|
||||||
CouchRest::FileManager.generate_app(@appdir)
|
CouchRest::FileManager.generate_app(@appdir)
|
||||||
|
@ -74,24 +75,25 @@ describe CouchRest::FileManager, "pushing an app" do
|
||||||
it "should create a design document" do
|
it "should create a design document" do
|
||||||
lambda{@db.get("_design/couchapp")}.should_not raise_error
|
lambda{@db.get("_design/couchapp")}.should_not raise_error
|
||||||
end
|
end
|
||||||
it "should create the views" do
|
# it "should create the views" do
|
||||||
doc = @db.get("_design/couchapp")
|
# doc = @db.get("_design/couchapp")
|
||||||
doc['views']['example']['map'].should match(/function/)
|
# doc['views']['example']['map'].should match(/function/)
|
||||||
end
|
# end
|
||||||
it "should create the index" do
|
# it "should create the index" do
|
||||||
doc = @db.get("_design/couchapp")
|
# doc = @db.get("_design/couchapp")
|
||||||
doc['_attachments']['index.html']["content_type"].should == 'text/html'
|
# doc['_attachments']['index.html']["content_type"].should == 'text/html'
|
||||||
end
|
# end
|
||||||
it "should push bar.txt and pals" do
|
# it "should push bar.txt and pals" do
|
||||||
File.open("#{@appdir}/foo/test.json",'w') do |f|
|
# FileUtils.mkdir_p("#{@appdir}/foo")
|
||||||
f.write("[1,2,3,4]")
|
# File.open("#{@appdir}/foo/test.json",'w') do |f|
|
||||||
end
|
# f.write("[1,2,3,4]")
|
||||||
r = @fm.push_app(@appdir, "couchapp")
|
# end
|
||||||
doc = @db.get("_design/couchapp")
|
# r = @fm.push_app(@appdir, "couchapp")
|
||||||
doc["foo"].should_not be_nil
|
# doc = @db.get("_design/couchapp")
|
||||||
doc["foo"]["bar"].should include("Couchapp will")
|
# doc["foo"].should_not be_nil
|
||||||
doc["foo"]["test"].should == [1,2,3,4]
|
# doc["foo"]["bar"].should include("Couchapp will")
|
||||||
end
|
# doc["foo"]["test"].should == [1,2,3,4]
|
||||||
|
# end
|
||||||
it "should push json as json" do
|
it "should push json as json" do
|
||||||
File.open("#{@appdir}/test.json",'w') do |f|
|
File.open("#{@appdir}/test.json",'w') do |f|
|
||||||
f.write("[1,2,3,4]")
|
f.write("[1,2,3,4]")
|
||||||
|
@ -100,13 +102,10 @@ describe CouchRest::FileManager, "pushing an app" do
|
||||||
doc = @db.get("_design/couchapp")
|
doc = @db.get("_design/couchapp")
|
||||||
doc['test'].should == [1,2,3,4]
|
doc['test'].should == [1,2,3,4]
|
||||||
end
|
end
|
||||||
it "should apply keys from doc.json directly to the doc" do
|
it "handles not having a forms directory" do
|
||||||
File.open("#{@appdir}/doc.json",'w') do |f|
|
`rm -rf #{@appdir}/forms`
|
||||||
f.write('{"magical":"so magic"}')
|
lambda { @fm.push_app(@appdir, "couchapp") }.should_not raise_error
|
||||||
end
|
`mkdir -p #{@appdir}/forms`
|
||||||
r = @fm.push_app(@appdir, "couchapp")
|
|
||||||
doc = @db.get("_design/couchapp")
|
|
||||||
doc['magical'].should == "so magic"
|
|
||||||
end
|
end
|
||||||
it "handles not having a forms directory" do
|
it "handles not having a forms directory" do
|
||||||
`rm -rf #{@appdir}/forms`
|
`rm -rf #{@appdir}/forms`
|
||||||
|
@ -116,38 +115,38 @@ describe CouchRest::FileManager, "pushing an app" do
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
describe CouchRest::FileManager, "pushing views" do
|
# describe CouchRest::FileManager, "pushing views" do
|
||||||
before(:all) do
|
# before(:all) do
|
||||||
@cr = CouchRest.new(COUCHHOST)
|
# @cr = CouchRest.new(COUCHHOST)
|
||||||
@db = @cr.database(TESTDB)
|
# @db = @cr.database(TESTDB)
|
||||||
@db.delete! rescue nil
|
# @db.delete! rescue nil
|
||||||
@db = @cr.create_db(TESTDB) rescue nil
|
# @db = @cr.create_db(TESTDB) rescue nil
|
||||||
|
#
|
||||||
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
|
# @fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
|
||||||
@view_dir = FIXTURE_PATH + '/views'
|
# @view_dir = FIXTURE_PATH + '/views'
|
||||||
ds = @fm.push_views(@view_dir)
|
# ds = @fm.push_views(@view_dir)
|
||||||
@design = @db.get("_design/test_view")
|
# @design = @db.get("_design/test_view")
|
||||||
end
|
# end
|
||||||
it "should create a design document for each folder" do
|
# it "should create a design document for each folder" do
|
||||||
@design["views"].should_not be_nil
|
# @design["views"].should_not be_nil
|
||||||
end
|
# end
|
||||||
it "should push a map and reduce view" do
|
# it "should push a map and reduce view" do
|
||||||
@design["views"]["test"]["map"].should_not be_nil
|
# @design["views"]["test"]["map"].should_not be_nil
|
||||||
@design["views"]["test"]["reduce"].should_not be_nil
|
# @design["views"]["test"]["reduce"].should_not be_nil
|
||||||
end
|
# end
|
||||||
it "should push a map only view" do
|
# it "should push a map only view" do
|
||||||
@design["views"]["only"]["map"].should_not be_nil
|
# @design["views"]["only"]["map"].should_not be_nil
|
||||||
@design["views"]["only"]["reduce"].should be_nil
|
# @design["views"]["only"]["reduce"].should be_nil
|
||||||
end
|
# end
|
||||||
it "should include library files" do
|
# it "should include library files" do
|
||||||
@design["views"]["only"]["map"].should include("globalLib")
|
# @design["views"]["only"]["map"].should include("globalLib")
|
||||||
@design["views"]["only"]["map"].should include("justThisView")
|
# @design["views"]["only"]["map"].should include("justThisView")
|
||||||
end
|
# end
|
||||||
it "should not create extra design docs" do
|
# it "should not create extra design docs" do
|
||||||
docs = @db.documents(:startkey => '_design', :endkey => '_design/ZZZZZZ')
|
# docs = @db.documents(:startkey => '_design', :endkey => '_design/ZZZZZZ')
|
||||||
docs['total_rows'].should == 1
|
# docs['total_rows'].should == 1
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
|
||||||
describe CouchRest::FileManager, "pushing a directory with id" do
|
describe CouchRest::FileManager, "pushing a directory with id" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
|
|
|
@ -28,7 +28,7 @@ describe CouchRest::Pager do
|
||||||
end
|
end
|
||||||
@db.bulk_save(@docs)
|
@db.bulk_save(@docs)
|
||||||
end
|
end
|
||||||
it "should yield total_docs / count times" do
|
it "should yield total_docs / limit times" do
|
||||||
n = 0
|
n = 0
|
||||||
@pager.all_docs(10) do |doc|
|
@pager.all_docs(10) do |doc|
|
||||||
n += 1
|
n += 1
|
||||||
|
@ -76,7 +76,7 @@ describe CouchRest::Pager do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have a view" do
|
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
|
end
|
||||||
|
|
||||||
it "should yield once per key" do
|
it "should yield once per key" do
|
||||||
|
|
Loading…
Add table
Reference in a new issue