Server URI can now include a prefix to the database name

This commit is contained in:
Brian Candler 2009-03-26 09:44:19 +00:00 committed by Matt Aimonetti
parent 190bd13723
commit 14acd95444
2 changed files with 50 additions and 5 deletions

View file

@ -2,9 +2,19 @@ module CouchRest
class Server class Server
attr_accessor :uri, :uuid_batch_count, :available_databases attr_accessor :uri, :uuid_batch_count, :available_databases
def initialize(server = 'http://127.0.0.1:5984', uuid_batch_count = 1000) def initialize(server = 'http://127.0.0.1:5984', uuid_batch_count = 1000)
@uri = server case server
when %r{\A(http://[^/]+)/(.*)\z}
@uri, @prefix = $1, $2
else
@uri, @prefix = server, ""
end
@uuid_batch_count = uuid_batch_count @uuid_batch_count = uuid_batch_count
end end
# Add default prefix to database name
def expand(name)
@prefix + name
end
# Lists all "available" databases. # Lists all "available" databases.
# An available database, is a database that was specified # An available database, is a database that was specified
@ -32,7 +42,7 @@ module CouchRest
# @couch.available_database?(:default) # @couch.available_database?(:default)
# #
def available_database?(ref_or_name) def available_database?(ref_or_name)
ref_or_name.is_a?(Symbol) ? available_databases.keys.include?(ref_or_name) : available_databases.values.map{|db| db.name}.include?(ref_or_name) ref_or_name.is_a?(Symbol) ? available_databases.keys.include?(ref_or_name) : available_databases.values.map{|db| db.name}.include?(expand(ref_or_name))
end end
def default_database=(name, create_unless_exists = true) def default_database=(name, create_unless_exists = true)
@ -45,12 +55,17 @@ module CouchRest
# Lists all databases on the server # Lists all databases on the server
def databases def databases
CouchRest.get "#{@uri}/_all_dbs" dbs = CouchRest.get "#{@uri}/_all_dbs"
unless @prefix.empty?
pfx = @prefix.gsub('/','%2F')
dbs.reject! { |db| db.index(pfx) != 0 }
end
dbs
end end
# Returns a CouchRest::Database for the given name # Returns a CouchRest::Database for the given name
def database(name) def database(name)
CouchRest::Database.new(self, name) CouchRest::Database.new(self, expand(name))
end end
# Creates the database if it doesn't exist # Creates the database if it doesn't exist
@ -66,7 +81,7 @@ module CouchRest
# Create a database # Create a database
def create_db(name) def create_db(name)
CouchRest.put "#{@uri}/#{name}" CouchRest.put "#{@uri}/#{expand(name).gsub('/','%2F')}"
database(name) database(name)
end end

View file

@ -2,6 +2,36 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe CouchRest::Server do describe CouchRest::Server do
describe "named databases" do
it "should generate database without prefix" do
couch = CouchRest::Server.new "http://192.0.2.1:1234"
db = couch.database("foo")
db.name.should == "foo"
db.uri.should == "http://192.0.2.1:1234/foo"
end
it "should generate database with prefix" do
couch = CouchRest::Server.new "http://192.0.2.1:1234/dev"
db = couch.database("foo")
db.name.should == "devfoo"
db.uri.should == "http://192.0.2.1:1234/devfoo"
end
it "should generate database with prefix and slash" do
couch = CouchRest::Server.new "http://192.0.2.1:1234/dev/"
db = couch.database("foo")
db.name.should == "dev/foo"
db.uri.should == "http://192.0.2.1:1234/dev%2Ffoo"
end
it "should generate database with slashes" do
couch = CouchRest::Server.new "http://192.0.2.1:1234/dev/sample/"
db = couch.database("foo/bar")
db.name.should == "dev/sample/foo/bar"
db.uri.should == "http://192.0.2.1:1234/dev%2Fsample%2Ffoo%2Fbar"
end
end
describe "available databases" do describe "available databases" do
before(:each) do before(:each) do
@couch = CouchRest::Server.new @couch = CouchRest::Server.new