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
Instance Attribute Summary collapse
-
#configurations ⇒ Object
readonly
Returns the value of attribute configurations.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#uri_builder ⇒ Object
readonly
Returns the value of attribute uri_builder.
Instance Method Summary collapse
-
#build(config) ⇒ Hash
private
Builds a configuration hash from a flat database config hash.
-
#call ⇒ Object
private
Returns gateway configuration for the current environment.
-
#initialize(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations) ⇒ Configuration
constructor
A new instance of Configuration.
Constructor Details
#initialize(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations) ⇒ Configuration
Returns a new instance of Configuration.
25 26 27 28 29 30 31 |
# File 'lib/rom/rails/active_record/configuration.rb', line 25 def initialize(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations) @configurations = configurations @env = env @root = root @uri_builder = ROM::Rails::ActiveRecord::UriBuilder.new end |
Instance Attribute Details
#configurations ⇒ Object (readonly)
Returns the value of attribute configurations.
20 21 22 |
# File 'lib/rom/rails/active_record/configuration.rb', line 20 def configurations @configurations end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
21 22 23 |
# File 'lib/rom/rails/active_record/configuration.rb', line 21 def env @env end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
22 23 24 |
# File 'lib/rom/rails/active_record/configuration.rb', line 22 def root @root end |
#uri_builder ⇒ Object (readonly)
Returns the value of attribute uri_builder.
23 24 25 |
# File 'lib/rom/rails/active_record/configuration.rb', line 23 def uri_builder @uri_builder end |
Instance 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.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rom/rails/active_record/configuration.rb', line 66 def build(config) adapter = config.fetch(:adapter) = config.except(:adapter).merge( root: root, scheme: adapter ) = config.except(*BASE_OPTIONS) uri = uri_builder.build(adapter, ) { uri: uri, options: } 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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rom/rails/active_record/configuration.rb', line 39 def call specs = {} configurations.configs_for(env_name: env).each do |config| if config.respond_to?(:configuration_hash) name, hash = [config.name, config.configuration_hash] else # Rails 6.0 name, hash = [config.spec_name, config.config] end specs[:default] ||= hash specs[name.to_sym] = hash end specs.transform_values { |hash| build hash.symbolize_keys } end |