Added attachment methods to CocuhRest::Document: #put_attachment, #fetch_attachment and #delete_attachment. Note you can overwrite exisitng attachments with #put_attachment.

This commit is contained in:
Matt Lyon 2009-02-02 00:35:37 -08:00 committed by Chris Anderson
parent b915f7f708
commit a4a2b202ae
2 changed files with 87 additions and 1 deletions

View file

@ -74,7 +74,28 @@ module CouchRest
result = database.move(self, dest) result = database.move(self, dest)
result['ok'] result['ok']
end end
# saves an attachment directly to couchdb
def put_attachment(name, file, options={})
raise ArgumentError, "doc.database required to put_attachment" unless database
result = database.put_attachment(self, name, file, options)
self['_rev'] = result['rev']
result['ok']
end
# returns an attachment's data
def fetch_attachment(name)
raise ArgumentError, "doc.database required to put_attachment" unless database
database.fetch_attachment(self, name)
end
# deletes an attachment directly from couchdb
def delete_attachment(name)
raise ArgumentError, "doc.database required to delete_attachment" unless database
result = database.delete_attachment(self, name)
self['_rev'] = result['rev']
result['ok']
end
end end

View file

@ -211,3 +211,68 @@ describe "MOVE existing document" do
end end
end end
end end
describe "dealing with attachments" do
before do
@db = reset_test_db!
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
response = @db.save({'key' => 'value'})
@doc = @db.get(response['id'])
end
def append_attachment(name='test.html', attach=@attach)
@doc['_attachments'] ||= {}
@doc['_attachments'][name] = {
'type' => 'text/html',
'data' => attach
}
@doc.save
@rev = @doc['_rev']
end
describe "PUTing an attachment directly to the doc" do
before do
@doc.put_attachment('test.html', @attach)
end
it "is there" do
@db.fetch_attachment(@doc, 'test.html').should == @attach
end
it "updates the revision" do
@doc['_rev'].should_not == @rev
end
it "updates attachments" do
@attach2 = "<html><head><title>My Doc</title></head><body><p>Is Different.</p></body></html>"
@doc.put_attachment('test.html', @attach2)
@db.fetch_attachment(@doc, 'test.html').should == @attach2
end
end
describe "fetching an attachment from a doc directly" do
before do
append_attachment
end
it "pulls the attachment" do
@doc.fetch_attachment('test.html').should == @attach
end
end
describe "deleting an attachment from a doc directly" do
before do
append_attachment
@doc.delete_attachment('test.html')
end
it "removes it" do
lambda { @db.fetch_attachment(@doc, 'test.html').should }.should raise_error(RestClient::ResourceNotFound)
end
it "updates the revision" do
@doc['_rev'].should_not == @rev
end
end
end