Class: ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/connection_specification.rb

Overview

Builds a ConnectionSpecification from user input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configurations) ⇒ Resolver

Accepts a hash two layers deep, keys on the first layer represent environments such as “production”. Keys must be strings.



116
117
118
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 116

def initialize(configurations)
  @configurations = configurations
end

Instance Attribute Details

#configurationsObject (readonly)

:nodoc:



112
113
114
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 112

def configurations
  @configurations
end

Instance Method Details

#resolve(config) ⇒ 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" }


136
137
138
139
140
141
142
143
144
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 136

def resolve(config)
  if config
    resolve_connection config
  elsif env = ActiveRecord::ConnectionHandling::RAILS_ENV.call
    resolve_symbol_connection env.to_sym
  else
    raise AdapterNotSpecified
  end
end

#resolve_allObject

Expands each key in @configurations hash into fully resolved hash



147
148
149
150
151
152
153
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 147

def resolve_all
  config = configurations.dup
  config.each do |key, value|
    config[key] = resolve(value) if value
  end
  config
end

#spec(config, name = nil) ⇒ 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" }


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_record/connection_adapters/connection_specification.rb', line 167

def spec(config, name = nil)
  spec = resolve(config).symbolize_keys

  raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

  path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
  begin
    require path_to_adapter
  rescue Gem::LoadError => e
    raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
  rescue LoadError => e
    raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.", e.backtrace
  end

  adapter_method = "#{spec[:adapter]}_connection"

  name ||=
    if config.is_a?(Symbol)
      config.to_s
    else
      "primary"
    end
  ConnectionSpecification.new(name, spec, adapter_method)
end