Finalizing support for automatic configuration

This commit is contained in:
Sam Lown 2011-04-29 23:06:31 +02:00
parent f3dd4ae06e
commit 85109b4b22
6 changed files with 59 additions and 40 deletions

View file

@ -35,6 +35,7 @@ module CouchRest
:port => '5984', :port => '5984',
:prefix => app_name, :prefix => app_name,
:suffix => nil, :suffix => nil,
:join => '_',
:username => nil, :username => nil,
:password => nil :password => nil
} }

View file

@ -3,10 +3,6 @@ module CouchRest
module Connection module Connection
extend ActiveSupport::Concern extend ActiveSupport::Concern
def database
self.class.database
end
def server def server
self.class.server self.class.server
end end
@ -16,15 +12,13 @@ module CouchRest
# Overwrite the normal use_database method so that a database # Overwrite the normal use_database method so that a database
# name can be provided instead of a full connection. # name can be provided instead of a full connection.
def use_database(db) def use_database(db)
@_database_name = db @database = prepare_database(db)
end end
# Replace CouchRest's database reader with a more advanced # Overwrite the default database method so that it always
# version that will make a best guess at the database you might # provides something from the configuration
# want to use. Allows for a string to be provided instead of
# a database object.
def database def database
@database ||= prepare_database(@_database_name) super || (@database ||= prepare_database)
end end
def server def server
@ -34,7 +28,7 @@ module CouchRest
def prepare_database(db = nil) def prepare_database(db = nil)
unless db.is_a?(CouchRest::Database) unless db.is_a?(CouchRest::Database)
conf = connection_configuration conf = connection_configuration
db = [conf[:prefix], db.to_s, conf[:suffix]].compact.join('_') db = [conf[:prefix], db.to_s, conf[:suffix]].reject{|s| s.to_s.empty?}.join(conf[:join])
self.server.database!(db) self.server.database!(db)
else else
db db
@ -51,7 +45,7 @@ module CouchRest
end end
def connection_configuration def connection_configuration
@server_configuration ||= @connection_configuration ||=
self.connection.update( self.connection.update(
(load_connection_config_file[environment] || {}).symbolize_keys (load_connection_config_file[environment] || {}).symbolize_keys
) )
@ -59,9 +53,9 @@ module CouchRest
def load_connection_config_file def load_connection_config_file
connection_config_cache[connection_config_file] ||= connection_config_cache[connection_config_file] ||=
File.exists?(connection_config_file) ? (File.exists?(connection_config_file) ?
YAML::load(ERB.new(IO.read(connection_config_file)).result) : YAML::load(ERB.new(IO.read(connection_config_file)).result) :
{ } { }).symbolize_keys
end end
def connection_config_cache def connection_config_cache

View file

@ -47,14 +47,11 @@ module CouchRest
private private
# Ensure that the model can no longer be used for normal requests # Ensure that no attempt is made to autoload a database connection
# be overwriting the database reader method so that a helpful # by overwriting it to provide a basic accessor.
# error message is displayed.
def overwrite_database_reader(model_name) def overwrite_database_reader(model_name)
class_eval <<-EOS, __FILE__, __LINE__ + 1 class_eval <<-EOS, __FILE__, __LINE__ + 1
def database def database; @database; end
raise StandardError, "#{self.to_s} documents must be accessed via the '#{model_name}' proxy"
end
EOS EOS
end end

View file

@ -85,6 +85,12 @@ describe CouchRest::Model::Base do
@class.should respond_to(:prepare_database) @class.should respond_to(:prepare_database)
end end
it "should join the database name correctly" do
@class.connection[:suffix] = 'db'
db = @class.prepare_database('test')
db.name.should eql('couchrest_test_db')
end
end end
describe "protected methods" do describe "protected methods" do
@ -93,6 +99,13 @@ describe CouchRest::Model::Base do
it "should provide main config by default" do it "should provide main config by default" do
@class.send(:connection_configuration).should eql(@class.connection) @class.send(:connection_configuration).should eql(@class.connection)
end end
it "should load file if available" do
@class.connection_config_file = File.join(FIXTURE_PATH, 'config', 'couchdb.yml')
hash = @class.send(:connection_configuration)
hash[:protocol].should eql('https')
hash[:host].should eql('sample.cloudant.com')
hash[:join].should eql('_')
end
end end
describe ".load_connection_config_file" do describe ".load_connection_config_file" do
@ -101,10 +114,9 @@ describe CouchRest::Model::Base do
end end
it "should load file if available" do it "should load file if available" do
@class.connection_config_file = File.join(FIXTURE_PATH, 'config', 'couchdb.yml') @class.connection_config_file = File.join(FIXTURE_PATH, 'config', 'couchdb.yml')
puts @class.send(:connection_config_cache).inspect
hash = @class.send(:load_connection_config_file) hash = @class.send(:load_connection_config_file)
hash[:development].should_not be_nil hash[:development].should_not be_nil
@class.server.uri.should eql("https://test:uesr@sample.cloudant.com:443") @class.server.uri.should eql("https://test:user@sample.cloudant.com:443")
end end
end end

View file

@ -13,6 +13,32 @@ end
describe "Proxyable" do describe "Proxyable" do
describe "#proxy_database" do
before do
@class = Class.new(CouchRest::Model::Base)
@class.class_eval do
def slug; 'proxy'; end
end
@obj = @class.new
end
it "should respond to method" do
@obj.should respond_to(:proxy_database)
end
it "should provide proxy database from method" do
@class.stub!(:proxy_database_method).twice.and_return(:slug)
@obj.proxy_database.should be_a(CouchRest::Database)
@obj.proxy_database.name.should eql('couchrest_proxy')
end
it "should raise an error if called and no proxy_database_method set" do
lambda { @obj.proxy_database }.should raise_error(StandardError, /Please set/)
end
end
describe "class methods" do describe "class methods" do
before(:each) do before(:each) do
@ -48,7 +74,6 @@ describe "Proxyable" do
@obj = DummyProxyable.new @obj = DummyProxyable.new
CouchRest::Model::Proxyable::ModelProxy.should_receive(:new).with(Cat, @obj, 'dummy_proxyable', 'db').and_return(true) CouchRest::Model::Proxyable::ModelProxy.should_receive(:new).with(Cat, @obj, 'dummy_proxyable', 'db').and_return(true)
@obj.should_receive('proxy_database').and_return('db') @obj.should_receive('proxy_database').and_return('db')
@obj.should_receive(:respond_to?).with('proxy_database').and_return(true)
@obj.cats @obj.cats
end end
@ -60,22 +85,8 @@ describe "Proxyable" do
@obj = DummyProxyable.new @obj = DummyProxyable.new
CouchRest::Model::Proxyable::ModelProxy.should_receive(:new).with(::Document, @obj, 'dummy_proxyable', 'db').and_return(true) CouchRest::Model::Proxyable::ModelProxy.should_receive(:new).with(::Document, @obj, 'dummy_proxyable', 'db').and_return(true)
@obj.should_receive('proxy_database').and_return('db') @obj.should_receive('proxy_database').and_return('db')
@obj.should_receive(:respond_to?).with('proxy_database').and_return(true)
@obj.documents @obj.documents
end end
it "should raise an error if the database method is missing" do
@class.proxy_for(:cats)
@obj = @class.new
@obj.should_receive(:respond_to?).with('proxy_database').and_return(false)
lambda { @obj.cats }.should raise_error(StandardError, "Missing #proxy_database method for proxy")
end
it "should raise an error if custom database method missing" do
@class.proxy_for(:proxy_kittens, :database_method => "foobardom")
@obj = @class.new
lambda { @obj.proxy_kittens }.should raise_error(StandardError, "Missing #foobardom method for proxy")
end
end end
end end
@ -316,7 +327,7 @@ describe "Proxyable" do
it "should allow creation of new entries" do it "should allow creation of new entries" do
inv = @company.proxyable_invoices.new(:client => "Lorena", :total => 35) inv = @company.proxyable_invoices.new(:client => "Lorena", :total => 35)
inv.database.should_not be_nil # inv.database.should_not be_nil
inv.save.should be_true inv.save.should be_true
@company.proxyable_invoices.count.should eql(1) @company.proxyable_invoices.count.should eql(1)
@company.proxyable_invoices.first.client.should eql("Lorena") @company.proxyable_invoices.first.client.should eql("Lorena")

View file

@ -7,11 +7,15 @@ require File.join(FIXTURE_PATH, 'more', 'course')
describe "Model views" do describe "Model views" do
class Unattached < CouchRest::Model::Base class Unattached < CouchRest::Model::Base
# Note: no use_database here
property :title property :title
property :questions property :questions
property :professor property :professor
view_by :title view_by :title
# Force the database to always be nil
def self.database
nil
end
end end
@ -195,7 +199,7 @@ describe "Model views" do
end end
end end
describe "a model class not tied to a database" do describe "a model class with database provided manually" do
before(:all) do before(:all) do
reset_test_db! reset_test_db!
@db = DB @db = DB