Add additional information to the basic docs.
This commit is contained in:
parent
4c7e88c11a
commit
9dece5b099
|
@ -11,12 +11,14 @@ sections:
|
|||
|
||||
### Introduction
|
||||
|
||||
`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.
|
||||
`dry-configurable` is a simple mixin to add **thread-safe configuration** behavior to your classes. There are many libraries that make use of the configuration, and each seemed to have its own implementation with a similar or duplicate interface, so we thought it was strange that this behavior had not already been encapsulated into a reusable gem, hence `dry-configurable` was born.
|
||||
|
||||
### Usage
|
||||
|
||||
`dry-configurable` is extremely simple to use, just extend the mixin and use the `setting` macro to add configuration options:
|
||||
|
||||
#### Overview
|
||||
|
||||
```ruby
|
||||
class App
|
||||
extend Dry::Configurable
|
||||
|
@ -53,3 +55,82 @@ App.pool
|
|||
App.uploader.bucket
|
||||
# => 'dev'
|
||||
```
|
||||
|
||||
#### Configuring different types of objects
|
||||
|
||||
With `dry-configurable`, **you can easily configure anything**, doesn't matter if you need to work with `module`, `class`, or an `instance of a class`
|
||||
|
||||
To configure a module or class, you need to extend the `Dry::Configurable` module.
|
||||
|
||||
```ruby
|
||||
module MyModule
|
||||
extend Dry::Configurable
|
||||
|
||||
setting :adapter
|
||||
end
|
||||
|
||||
class MyClass
|
||||
extend Dry::Configurable
|
||||
|
||||
setting :adapter
|
||||
end
|
||||
|
||||
MyModule.config.adapter = :http
|
||||
MyModule.config.adapter # => :http
|
||||
|
||||
MyClass.config.adapter = :tcp
|
||||
MyClass.config.adapter # => :tcp
|
||||
```
|
||||
|
||||
To configure an instance of a class, the only difference is that you need to `include` the `Dry::Configurable` instead of extending it.
|
||||
|
||||
```ruby
|
||||
class MyClass
|
||||
include Dry::Configurable
|
||||
|
||||
setting :adapter
|
||||
end
|
||||
|
||||
foo = MyClass.new
|
||||
bar = MyClass.new
|
||||
|
||||
foo.config.adapter = :grpc
|
||||
bar.config.adapter = :http
|
||||
|
||||
foo.config.adapter #=> :grpc
|
||||
bar.config.adapter #=> :http
|
||||
```
|
||||
|
||||
#### Configure block syntax
|
||||
|
||||
There is an alternative way to configure your objects, using `configure` method. It sends the `config` instance to the block you pass as an argument and then yields whatever is inside.
|
||||
|
||||
```ruby
|
||||
App.configure do |config|
|
||||
config.database.dsn = "sqlite:memory"
|
||||
config.adapter = :grpc
|
||||
config.pool = 5
|
||||
config.uploader.bucket = 'production'
|
||||
end
|
||||
```
|
||||
|
||||
The returned value is the object that the `configure` method is called upon. This means you can easily get multiple objects configured independently.
|
||||
|
||||
```ruby
|
||||
class Client
|
||||
include Dry::Configurable
|
||||
|
||||
setting :adapter, reader: true
|
||||
end
|
||||
|
||||
client1 = Client.new.configure do |config|
|
||||
config.adapter :grpc
|
||||
end
|
||||
|
||||
client2 = Client.new.configure do |config|
|
||||
config.adapter = :http
|
||||
end
|
||||
|
||||
client1.adapter # => :grpc
|
||||
client2.adapter # => :http
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue