Module: Sinclair::Configurable
- Defined in:
- lib/sinclair/configurable.rb
Overview
Module capable of giving configuration capability
By extending Configurable, class receives the methods public .config, .reset_config and .configure and the private methods .configurable_with and .configurable_by
Constant Summary collapse
- CONFIG_CLASS_WARNING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Deprecation warning message
'Config classes attributes should ' \ 'be defined inside the class or through the usage of ' \ "configurable_with.\n" \ "In future releases this will be enforced.\n" \ 'see more on https://github.com/darthjee/sinclair/blob/master/WARNINGS.md#usage-of-custom-config-classes'
Instance Method Summary collapse
-
#config ⇒ Config, Object
Returns current instance of config.
-
#configurable_by(config_class, with: []) ⇒ ConfigFactory
Allows configuration to happen through custom config class.
-
#configurable_with(*attributes) ⇒ Array<Symbol>
Adds a configuration option to config class.
-
#configure {|ConfigBuilder| ... } ⇒ Object
Set the values in the config.
-
#reset_config ⇒ NilClass
Cleans the current config instance.
Instance Method Details
#config ⇒ Config, Object
Returns current instance of config
the method returns the same instance until reset_config
is called
30 31 32 |
# File 'lib/sinclair/configurable.rb', line 30 def config config_factory.config end |
#configurable_by(config_class, with: []) ⇒ ConfigFactory
Allows configuration to happen through custom config class
configurable_with does not add methods to config_class. If needed, those can be added by a subsequent call to #configurable_with
156 157 158 159 160 161 162 163 |
# File 'lib/sinclair/configurable.rb', line 156 def configurable_by(config_class, with: []) warn CONFIG_CLASS_WARNING if with.present? @config_factory = ConfigFactory.new( config_class: config_class, config_attributes: with.map(&:to_sym) ) end |
#configurable_with(*attributes) ⇒ Array<Symbol>
Adds a configuration option to config class
107 108 109 |
# File 'lib/sinclair/configurable.rb', line 107 def configurable_with(*attributes) config_factory.add_configs(*attributes) end |
#configure {|ConfigBuilder| ... } ⇒ Object
Set the values in the config
The block given is evaluated by the Sinclair::ConfigBuilder where each method missed will be used to set a variable in the config
42 43 44 |
# File 'lib/sinclair/configurable.rb', line 42 def configure(&block) config_factory.configure(&block) end |
#reset_config ⇒ NilClass
Cleans the current config instance
After cleaning it, #config will generate a new instance
36 37 38 |
# File 'lib/sinclair/configurable.rb', line 36 def reset_config config_factory.reset_config end |