def save_doc(doc, bulk = false)
if doc['_attachments']
doc['_attachments'] = encode_attachments(doc['_attachments'])
end
if bulk
@bulk_save_cache << doc
return bulk_save if @bulk_save_cache.length >= @bulk_save_cache_limit
return {"ok" => true}
elsif !bulk && @bulk_save_cache.length > 0
bulk_save
end
result = if doc['_id']
slug = escape_docid(doc['_id'])
CouchRest.put "#{@uri}/#{slug}", doc
else
begin
slug = doc['_id'] = @server.next_uuid
CouchRest.put "#{@uri}/#{slug}", doc
rescue
CouchRest.post @uri, doc
end
end
if result['ok']
doc['_id'] = result['id']
doc['_rev'] = result['rev']
doc.database = self if doc.respond_to?(:database=)
end
result
end