Class: ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
- Defined in:
- lib/active_record/connection_adapters/connection_specification.rb
Overview
Builds a ConnectionSpecification from user input.
Instance Attribute Summary collapse
-
#configurations ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#initialize(configurations) ⇒ Resolver
constructor
Accepts a list of db config objects.
-
#resolve(config_or_env, pool_name = nil) ⇒ Object
Returns a hash with database connection information.
-
#spec(config) ⇒ Object
Returns an instance of ConnectionSpecification for a given adapter.
Constructor Details
#initialize(configurations) ⇒ Resolver
Accepts a list of db config objects.
117 118 119 |
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 117 def initialize(configurations) @configurations = configurations end |
Instance Attribute Details
#configurations ⇒ Object (readonly)
:nodoc:
114 115 116 |
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 114 def configurations @configurations end |
Instance Method Details
#resolve(config_or_env, pool_name = nil) ⇒ Object
Returns a hash with database connection information.
Examples
Full hash Configuration.
configurations = { "production" => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" } }
Resolver.new(configurations).resolve(:production)
# => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3"}
Initialized with URL configuration strings.
configurations = { "production" => "postgresql://localhost/foo" }
Resolver.new(configurations).resolve(:production)
# => { "host" => "localhost", "database" => "foo", "adapter" => "postgresql" }
137 138 139 140 141 142 143 |
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 137 def resolve(config_or_env, pool_name = nil) if config_or_env resolve_connection config_or_env, pool_name else raise AdapterNotSpecified end end |
#spec(config) ⇒ Object
Returns an instance of ConnectionSpecification for a given adapter. Accepts a hash one layer deep that contains all connection information.
Example
config = { "production" => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" } }
spec = Resolver.new(config).spec(:production)
spec.adapter_method
# => "sqlite3_connection"
spec.config
# => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" }
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 157 def spec(config) pool_name = config if config.is_a?(Symbol) spec = resolve(config, pool_name).symbolize_keys raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter) # Require the adapter itself and give useful feedback about # 1. Missing adapter gems and # 2. Adapter gems' missing dependencies. path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter" begin require path_to_adapter rescue LoadError => e # We couldn't require the adapter itself. Raise an exception that # points out config typos and missing gems. if e.path == path_to_adapter # We can assume that a non-builtin adapter was specified, so it's # either misspelled or missing from Gemfile. raise LoadError, "Could not load the '#{spec[:adapter]}' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.", e.backtrace # Bubbled up from the adapter require. Prefix the exception message # with some guidance about how to address it and reraise. else raise LoadError, "Error loading the '#{spec[:adapter]}' Active Record adapter. Missing a gem it depends on? #{e.}", e.backtrace end end adapter_method = "#{spec[:adapter]}_connection" unless ActiveRecord::Base.respond_to?(adapter_method) raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter" end ConnectionSpecification.new(spec.delete(:name) || "primary", spec, adapter_method) end |