Class: ROM::Rails::ActiveRecord::Configuration
- Inherits:
-
Object
- Object
- ROM::Rails::ActiveRecord::Configuration
- Defined in:
- lib/rom/rails/active_record/configuration.rb
Overview
A helper class to derive ‘rom-sql` configuration from ActiveRecord.
Constant Summary collapse
- BASE_OPTIONS =
[ :root, :adapter, :database, :password, :username, :hostname, :host ].freeze
Class Method Summary collapse
-
.build(config) ⇒ Hash
private
Builds a configuration hash from a flat database config hash.
- .build_uri(attrs) ⇒ Object
-
.call ⇒ Object
private
Returns gateway configuration for the current environment.
- .generic_uri(config) ⇒ Object
- .mysql_uri(config) ⇒ Object
- .postgresql_uri(config) ⇒ Object
- .sqlite3_uri(config) ⇒ Object
Class Method Details
.build(config) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a configuration hash from a flat database config hash.
This is used to support typical database.yml-complaint configs. It also uses adapter interface for things that are adapter-specific like handling schema naming.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rom/rails/active_record/configuration.rb', line 41 def self.build(config) adapter = config.fetch(:adapter) = config.except(:adapter).merge(scheme: adapter) = config.except(*BASE_OPTIONS) builder_method = :"#{adapter}_uri" uri = if respond_to?(builder_method) send(builder_method, ) else generic_uri() end # JRuby connection strings require special care. if RUBY_ENGINE == 'jruby' && adapter != 'postgresql' uri = "jdbc:#{uri}" end { uri: uri, options: } end |
.build_uri(attrs) ⇒ Object
97 98 99 |
# File 'lib/rom/rails/active_record/configuration.rb', line 97 def self.build_uri(attrs) Addressable::URI.new(attrs).to_s end |
.call ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This relies on ActiveRecord being initialized already.
Returns gateway configuration for the current environment.
26 27 28 29 |
# File 'lib/rom/rails/active_record/configuration.rb', line 26 def self.call configuration = ::ActiveRecord::Base.configurations.fetch(::Rails.env) build(configuration.symbolize_keys.update(root: ::Rails.root)) end |
.generic_uri(config) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rom/rails/active_record/configuration.rb', line 86 def self.generic_uri(config) build_uri( scheme: config.fetch(:scheme), user: config[:username], password: config[:password], host: config[:host], port: config[:port], path: config[:database] ) end |
.mysql_uri(config) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/rom/rails/active_record/configuration.rb', line 78 def self.mysql_uri(config) if config.key?(:username) && !config.key?(:password) config.update(password: '') end generic_uri(config) end |
.postgresql_uri(config) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/rom/rails/active_record/configuration.rb', line 71 def self.postgresql_uri(config) generic_uri(config.merge( host: config.fetch(:host) { '' }, scheme: 'postgres' )) end |
.sqlite3_uri(config) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/rom/rails/active_record/configuration.rb', line 61 def self.sqlite3_uri(config) path = Pathname.new(config.fetch(:root)).join(config.fetch(:database)) build_uri( scheme: 'sqlite', host: '', path: path.to_s ) end |