Configurability module
Extended With |
|
A unified, unintrusive, assume-nothing configuration system for Ruby
Constants
- REVISION
Version-control revision constant
- VERSION
Library version constant
Attributes
- after_configure_hooks R
- configurable_objects RW
Public Class Methods
Register a callback to be run after the config is loaded.
Set the flag that indicates that the after-configure hooks have run at least once.
Returns true
if the after-configuration hooks have run at least once.
Call the post-configuration callbacks.
Configure objects that have had Configurability
added to them with the sections of the specified config
that correspond to their config_key
. If the config
doesn’t respond_to the object’s config_key
, the object’s configure
method is called with nil
instead.
Gather the default configuration in a Configurability::Config
object and return it.
Nest the specified hash
inside subhashes for each subsection of the given key
and return the result.
Add configurability to the given object
.
Find the section of the specified config
object that corresponds to the given key
.
Gather defaults from objects with Configurability
in the given collection
object. Objects that wish to add a section to the defaults should implement a defaults
method in the same scope as configure
that returns the Hash of default, or set one of the constants in the default implementation of defaults
. The hash for each object will be merged into the collection
via merge!.
Return the subsection of the specified config
that corresponds to key
, trying both struct-like and hash-like interfaces.
Mixin hook: extend including classes instead
Install the appropriate section of the config
into the given object
.
Try to generate a config key from the given object. If it responds_to name, the result will be stringified and stripped of non-word characters. If the object itself doesn’t have a name, the name of its class will be used instead.
Return the specified key
normalized into a valid Symbol config key.
If a configuration has been loaded (via {#configure_objects}), clear it.
Get the library version. If include_buildnum
is true, the version string will include the VCS rev ID.
Public Instance Methods
the loaded configuration (after ::configure_objects
has been called at least once)
Configuration API
↑ topPublic Instance Methods
Get (and optionally set) the config_key
(a Symbol).
Set the config key of the object.
Default configuration method. This will merge the provided config
with the defaults if there are any and the config
responds to #to_h
. If the config
responds to #each_pair
, any writable attributes of the calling object with the same name as a key of the config
will be called with the corresponding value. E.g.,
class MyClass extend Configurability CONFIG_DEFAULTS = { environment: 'develop', apikey: 'testing-key' } config_key :my_class class << self attr_accessor :environment, :apikey end end config = { my_class: {apikey: 'demo-key'} } Configurability.configure_objects( config ) MyClass.apikey # => 'demo-key' MyClass.environment # => 'develop'
Configuration Defaults API
↑ topPublic Instance Methods
Return a Configurability::Config
object that contains the configuration defaults for the receiver.
The default implementation of the method called by ::gather_defaults
when gathering configuration defaults. This method expects either a DEFAULT_CONFIG
or a CONFIG_DEFAULTS
constant to contain the configuration defaults, and will just return the fallback
value if neither exists.
Configuration settings block
↑ topPublic Instance Methods
Declare configuration settings and defaults. In the provided block
, you can create a configuration setting using the following syntax:
configurability( :my_config_key ) do # Declare a setting with a `nil` default setting :a_config_key # Declare one with a default value setting :another_config_key, default: 18 end