Goal
Hanami 2.0 should be simple for newcomers (or people that upgrade).
That implies that reading code under config/
directory of a newly generated application shouldn’t be overwhelming.
The framework must define defaults for configuration/settings, and just work . Users that wants to override default choices can add configuration/settings for the relevant runtime environment (development, testing, etc…).
Example
Let’s take an example to explain the principle and its proposed implementation: logging defaults.
I would suggest set the defaults for the logger like this:
-
level:
debug
-
stream:
$stdout
In the current alpha2, we use settings (config/settings.rb
) to define the value of those defaults.
Then we manually assign the values from settings into config/application.rb
.
This setup introduces useless code to be generated us and digested by the final user.
Proposal
Step 1
I would assign default values to Hanami::Application::Settings
, to be inherited by MyApp::Settings
:
class Hanami::Application::Settings
def self.inherited(base)
# ...
base.class_eval do
setting :logger do
setting :level, default: :debug
setting :stream, default: $stdout
end
end
end
end
When MyApp::Settings
inherits from Hanami::Application::Settings
it would get the defaults that we define at the framework level.
Step 2
When Hanami application loads the settings, then we can assign their values to the application configuration.
class Hanami::Application
def self.init
# ...
load_settings
assign_settings # NEW METHOD
end
private
def self.assign_settings
config.logger = settings.logger
end
end
Step 3
With this change, we can remove from the application template, any code that configures the logger under config/
directory.
In case final users would override defaults, they could do it in a few ways:
- Add the relative ENV var in
.env.*
files. E.g. `HANAMI_LOGGER__LEVEL=“info” - Add the relative lines of codes in
config/application.rb
. E.g.config.logger.stream = "#{ENV['DEPLOY_PATH']}/log/production.log"
Assumptions
- The way to override an existing setting value would be via ENV var (in
.env.*
) files. - The way to add a new setting would be via
config/settings.rb
and optionally via ENV vars. - The way to override an existing configuration that cannot be done via ENV, or the behavior differs between environments, would be via
config/application.rb
.
Required Changes
dry-configurable
powers application settings.
- Make
dry-configurable
, be able to be assigned and return nested settings. - Make
dry-configurable
, be able to work with ENV variables to assign nested setting values.