-
-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Alchemy.config and DSL for type-safe configuration #3178
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3178 +/- ##
==========================================
+ Coverage 96.59% 96.69% +0.10%
==========================================
Files 237 256 +19
Lines 6380 6606 +226
==========================================
+ Hits 6163 6388 +225
- Misses 217 218 +1 ☔ View full report in Codecov by Sentry. |
7cb31d1
to
4626d14
Compare
Codeclimate is not right. Abstracting that out would create an indirection that will be hard to read. The metaprogramming here is not so easy as-is. |
86ac849
to
d936c0c
Compare
d936c0c
to
c42fb84
Compare
cff6cf5
to
bee8d87
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Thanks
This describes the very basic functioning of a new configuration class that other class can inherit from. It provides a class method `.option` that takes a type and a default value, and generates setter and getter methods.
We want to make sure the type of things is right. This ensures that.
This can validate and store anything in a typed fashion.
This class takes strings and constantizes them on each. Lifted from https://github.com/solidusio/solidus/blob/main/core/lib/spree/core/class_constantizer.rb
We have a lot of Boolean config options.
This should allow us to simply ingest yml files and keep the current `alchemy/config.yml` files.
This class contains all of the keys documented in the alchemy/config.yml file. When doing this I realized I needed to tweak the `Alchemy::Configuration` superclass a little bit: - Replace "/" with "_" when getting and setting keys, because methods with slashes in them are strange - Handle nil when setting values. Nil should be allowed for now (we could do mandatory/optional values, but that's not in scope right now.
This allows us to have a programmatic configuration DSL in e.g. an initializer: ```rb Alchemy.configure do |config| config.auto_logout_time = Rails.env.test? ? 10 : 20 end ```
We don't do this in the config class anymore, so we have to do it somewhere. Defaults are given in the configuration classes, so we don't need to run the config.yml in Alchemy itself.
This deprecated option was also in the old config.rb file, so we reimplement it here.
We don't need it in Alchemy's config directory anymore, because it's all in the default config. Also, moving the config into an initializer.
The `const_missing` needs to be defined both as a class method and as an instance method in order to catch references to Alchemy::Config from the class level and from the instance level. To avoid repetition, I've moved the method into its own module that we now both `include` and `extend` in the main `Alchemy` module.
While this is somewhat complicated code, I am sure it'll come in handy - not just for testing the `config.yml` file.
bee8d87
to
19f2f7c
Compare
What is this pull request for?
This adds a Ruby configuration parent class with a DSL for configuration objects. The intention is for this to replace the current use of parsed
YAML
files.The disadvantages of the current system are
The configuration object in this PR offers the following DSL
This is equivalent to the following hash, and can indeed be initialized with it:
Parts of the configuration can be set later on, via Ruby or via a Hash:
Trying to set an option that has not been defined before will raise an
NoMethodError
.Trying to set an option with data that does not have the right type will raise a
TypeError
.With this change, default configuration options are now located in Alchemy's
lib
folder, so we don't have a defaultconfig.yml
file anymore. I've removed it in this PR, because it's likely going to go stale soon (defaults change, and so on).New users will get a generated
alchemy.rb
file, but the existing config.yml files will continue to work with no deprecation messages for now.Checklist