Finalizing support for automatic configuration
This commit is contained in:
parent
f3dd4ae06e
commit
85109b4b22
6 changed files with 59 additions and 40 deletions
|
@ -35,6 +35,7 @@ module CouchRest
|
|||
:port => '5984',
|
||||
:prefix => app_name,
|
||||
:suffix => nil,
|
||||
:join => '_',
|
||||
:username => nil,
|
||||
:password => nil
|
||||
}
|
||||
|
|
|
@ -3,10 +3,6 @@ module CouchRest
|
|||
module Connection
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def database
|
||||
self.class.database
|
||||
end
|
||||
|
||||
def server
|
||||
self.class.server
|
||||
end
|
||||
|
@ -16,15 +12,13 @@ module CouchRest
|
|||
# Overwrite the normal use_database method so that a database
|
||||
# name can be provided instead of a full connection.
|
||||
def use_database(db)
|
||||
@_database_name = db
|
||||
@database = prepare_database(db)
|
||||
end
|
||||
|
||||
# Replace CouchRest's database reader with a more advanced
|
||||
# version that will make a best guess at the database you might
|
||||
# want to use. Allows for a string to be provided instead of
|
||||
# a database object.
|
||||
# Overwrite the default database method so that it always
|
||||
# provides something from the configuration
|
||||
def database
|
||||
@database ||= prepare_database(@_database_name)
|
||||
super || (@database ||= prepare_database)
|
||||
end
|
||||
|
||||
def server
|
||||
|
@ -34,7 +28,7 @@ module CouchRest
|
|||
def prepare_database(db = nil)
|
||||
unless db.is_a?(CouchRest::Database)
|
||||
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)
|
||||
else
|
||||
db
|
||||
|
@ -51,7 +45,7 @@ module CouchRest
|
|||
end
|
||||
|
||||
def connection_configuration
|
||||
@server_configuration ||=
|
||||
@connection_configuration ||=
|
||||
self.connection.update(
|
||||
(load_connection_config_file[environment] || {}).symbolize_keys
|
||||
)
|
||||
|
@ -59,9 +53,9 @@ module CouchRest
|
|||
|
||||
def load_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) :
|
||||
{ }
|
||||
{ }).symbolize_keys
|
||||
end
|
||||
|
||||
def connection_config_cache
|
||||
|
|
|
@ -47,14 +47,11 @@ module CouchRest
|
|||
|
||||
private
|
||||
|
||||
# Ensure that the model can no longer be used for normal requests
|
||||
# be overwriting the database reader method so that a helpful
|
||||
# error message is displayed.
|
||||
# Ensure that no attempt is made to autoload a database connection
|
||||
# by overwriting it to provide a basic accessor.
|
||||
def overwrite_database_reader(model_name)
|
||||
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
||||
def database
|
||||
raise StandardError, "#{self.to_s} documents must be accessed via the '#{model_name}' proxy"
|
||||
end
|
||||
def database; @database; end
|
||||
EOS
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue