[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
2
3
# Gemfile

gem 'dry-configurable', `0.12.1'

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class App
extend Dry::Configurable

# Pass a block for nested configuration (works to any depth)
setting :database do
# Can pass a default value
setting :dsn, 'sqlite:memory'
end
# Defaults to nil if no default value is given
setting :adapter
# Pre-process values
setting(:path, 'test') { |value| Pathname(value) }
# Passing the reader option as true will create attr_reader method for the class
setting :pool, 5, reader: true
# Passing the reader attributes works with nested configuration
setting :uploader, reader: true do
setting :bucket, 'dev'
end
end

App.config.database.dsn
# => "sqlite:memory"

App.config.database.dsn = 'jdbc:sqlite:memory'
App.config.database.dsn
# => "jdbc:sqlite:memory"
App.config.adapter
# => nil
App.config.path
# => #<Pathname:test>
App.pool
# => 5
App.uploader.bucket
# => 'dev'

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
2
3
4
5
6
7
8
9
10
require 'dry-configurable'

module App
extend Dry::Configurable

setting :api_url
end

App.config.api_url = 'https://hanamimastery.com'
App.config.api_url # => 'https://hanamimastery.com'

Class level

On the class level, nothing changes, you can use it exactly in the same way.

1
2
3
4
5
6
7
8
9
10
require 'dry-configurable'

class App
extend Dry::Configurable

setting :api_url
end

App.config.api_url = 'https://example2.com'
App.config.api_url

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
2
3
4
5
6
7
8
9
10
11
class App
include Dry::Configurable

setting :api_url
end

app1 = App.new.config.api_url = 'https://example3.com'
app1.config.api_url

app2 = App.new.config.api_url
app2.config.api_url

References

[1] GitHub - dry-rb/dry-configurable: A simple mixin to make Ruby classes configurable - https://github.com/dry-rb/dry-configurable

[2] dry-configurable | RubyGems.org | your community gem host - https://rubygems.org/gems/dry-configurable/

[3] dry-rb - dry-configurable master - Introduction & Usage - https://dry-rb.org/gems/dry-configurable/master/

[4] #5 Configure anything with Dry::Configurable | Hanami Mastery - a knowledge base to hanami framework - https://hanamimastery.com/episodes/5-configure-anything-with-dry-configurable