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.
- #default_configuration ⇒ Object
-
#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.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rom/rails/active_record/configuration.rb', line 69 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 |
# File 'lib/rom/rails/active_record/configuration.rb', line 39 def call specs = { default: build(default_configuration.symbolize_keys) } if rails6? configurations.configs_for(env_name: env).each do |config| specs[config.spec_name.to_sym] = build(config.config.symbolize_keys) end end specs end |
#default_configuration ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/rom/rails/active_record/configuration.rb', line 51 def default_configuration if rails6? configurations.default_hash(env) else configurations.fetch(env) end end |