Fixing issues with proxying and new database code

This commit is contained in:
Sam Lown 2011-04-30 01:04:04 +02:00
parent 814e38f75f
commit da3e524020
2 changed files with 35 additions and 8 deletions

View file

@ -42,8 +42,10 @@ module CouchRest
# Define the name of a method to call to determine the name of # Define the name of a method to call to determine the name of
# the database to use as a proxy. # the database to use as a proxy.
def proxy_database_method=(name); @proxy_database_method = name; end def proxy_database_method(name = nil)
def proxy_database_method; @proxy_database_method; end @proxy_database_method = name if name
@proxy_database_method
end
private private
@ -51,7 +53,9 @@ module CouchRest
# by overwriting it to provide a basic accessor. # by overwriting it to provide a basic accessor.
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; @database; end def self.database
raise StandardError, "#{self.to_s} database must be accessed via '#{model_name}' proxy"
end
EOS EOS
end end

View file

@ -3,8 +3,9 @@ require File.expand_path("../../spec_helper", __FILE__)
require File.join(FIXTURE_PATH, 'more', 'cat') require File.join(FIXTURE_PATH, 'more', 'cat')
class DummyProxyable < CouchRest::Model::Base class DummyProxyable < CouchRest::Model::Base
def proxy_database proxy_database_method :db
'db' # Do not use this! def db
'db'
end end
end end
@ -41,11 +42,11 @@ describe "Proxyable" do
describe "class methods" do describe "class methods" do
before(:each) do
@class = DummyProxyable.clone
end
describe ".proxy_owner_method" do describe ".proxy_owner_method" do
before(:each) do
@class = DummyProxyable.clone
end
it "should provide proxy_owner_method accessors" do it "should provide proxy_owner_method accessors" do
@class.should respond_to(:proxy_owner_method) @class.should respond_to(:proxy_owner_method)
@class.should respond_to(:proxy_owner_method=) @class.should respond_to(:proxy_owner_method=)
@ -56,7 +57,20 @@ describe "Proxyable" do
end end
end end
describe ".proxy_database_method" do
before do
@class = Class.new(CouchRest::Model::Base)
end
it "should be possible to set the proxy database method" do
@class.proxy_database_method :db
@class.proxy_database_method.should eql(:db)
end
end
describe ".proxy_for" do describe ".proxy_for" do
before(:each) do
@class = DummyProxyable.clone
end
it "should be provided" do it "should be provided" do
@class.should respond_to(:proxy_for) @class.should respond_to(:proxy_for)
@ -91,6 +105,10 @@ describe "Proxyable" do
end end
describe ".proxied_by" do describe ".proxied_by" do
before do
@class = Class.new(CouchRest::Model::Base)
end
it "should be provided" do it "should be provided" do
@class.should respond_to(:proxied_by) @class.should respond_to(:proxied_by)
end end
@ -118,6 +136,11 @@ describe "Proxyable" do
@class.proxied_by(:department) @class.proxied_by(:department)
lambda { @class.proxied_by(:company) }.should raise_error lambda { @class.proxied_by(:company) }.should raise_error
end end
it "should overwrite the database method to provide an error" do
@class.proxied_by(:company)
lambda { @class.database }.should raise_error(StandardError, /database must be accessed via/)
end
end end
end end