[Awesome Ruby Gem] Use dry-configurable gem to add thread-safe configuration behaviour to your Ruby classes
dry-configurable
dry-configurable is a simple mixin to add thread-safe configuration behaviour to your classes. There are many libraries that make use of configuration, and each seemed to have their own implementation with a similar or duplicate interface, so we thought it was strange that this behaviour had not already been encapsulated into a reusable gem, hence dry-configurable was born.
Installation
You can install it as a gem:
1 | gem install dry-configurable |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usages
dry-configurable is extremely simple to use, just extend the mixin and use the setting macro to add configuration options.
1 | class App |
Just extend the mixin and use the setting macro to add configuration options:
You can use it either on ruby module
on a class
or an instance
.
Module level
On the module level, you need to extend the Dry::Configurable
module and you can safely use all features it provides. Here I set the api_url
to hanamimastery.com
1 | require 'dry-configurable' |
Class level
On the class level, nothing changes, you can use it exactly in the same way.
1 | require 'dry-configurable' |
Instance level
If you want to use it on the instance of the class, however, the only difference is that you need to include the Dry::Configurable module instead of extending it.
1 | class App |