From 37c021a6b9cfb6f8335f72f8419580a5bfe3c92e Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Thu, 14 Apr 2011 00:46:55 +0200 Subject: [PATCH] Started initial idea for automated database configuration --- lib/couchrest/model/configuration.rb | 2 ++ lib/couchrest/model/persistence.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/couchrest/model/configuration.rb b/lib/couchrest/model/configuration.rb index 027bf3c..9a89d72 100644 --- a/lib/couchrest/model/configuration.rb +++ b/lib/couchrest/model/configuration.rb @@ -11,11 +11,13 @@ module CouchRest add_config :model_type_key add_config :mass_assign_any_attribute add_config :auto_update_design_doc + add_config :database_config_path configure do |config| config.model_type_key = 'model' # was 'couchrest-type' config.mass_assign_any_attribute = false config.auto_update_design_doc = true + config.database_config_path = File.join(defined?(Rails) ? Rails.root : Dir.pwd, 'config', 'couchdb.yml') end end diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index 4f19e86..774f3a5 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -89,6 +89,9 @@ module CouchRest self end + def database + self.class.database(@database) + end protected @@ -105,6 +108,20 @@ module CouchRest module ClassMethods + # 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. + def database(db = nil) + db ||= @database + if db.nil? + # try to grab from configuration files + + else + db + end + end + # Creates a new instance, bypassing attribute protection # # ==== Returns