Class: Sinclair::ConfigFactory Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class responsible for configuring the configuration class
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 class is expected to be ConfigClass. ' \ "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
-
#add_configs(*args) ⇒ Array<Symbol>
private
Adds possible configurations.
-
#child ⇒ ConfigFactory
private
Returns a new instance of ConfigFactory.
-
#config ⇒ Config, Object
Returns current instance of config.
-
#configure(config_hash = {}) {|ConfigBuilder| ... } ⇒ Object
Set the values in the config.
-
#initialize(config_class: Class.new(Config), config_attributes: []) ⇒ ConfigFactory
constructor
private
A new instance of ConfigFactory.
-
#reset_config ⇒ NilClass
Cleans the current config instance.
Constructor Details
#initialize(config_class: Class.new(Config), config_attributes: []) ⇒ ConfigFactory
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of ConfigFactory.
29 30 31 32 33 34 35 36 |
# File 'lib/sinclair/config_factory.rb', line 29 def initialize(config_class: Class.new(Config), config_attributes: []) @config_class = config_class @config_attributes = config_attributes.dup return if config_class.is_a?(ConfigClass) warn CONFIG_CLASS_WARNING end |
Instance Method Details
#add_configs(*args) ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
remove class check once only ConfigClass are accepted
Adds possible configurations
It change the configuration class adding methods
and keeps track of those configurations so that
{ConfigBuilder} is able to set those values when invoked
59 60 61 62 63 64 65 66 67 |
# File 'lib/sinclair/config_factory.rb', line 59 def add_configs(*args) builder = if config_class.is_a?(Sinclair::ConfigClass) config_class.add_configs(*args) else Config::MethodsBuilder.build(config_class, *args) end config_attributes.concat(builder.config_names.map(&:to_sym)) end |
#child ⇒ ConfigFactory
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of ConfigFactory
the new instance will have the same config_attributes and for config_class a child of the current config_class
This method is called when initializing Sinclair::Configurable config_factory and the superclass is also configurable
This way, child classes from other Sinclair::Configurable classes will have a config_class that is a child from the original config_class
138 139 140 141 142 143 |
# File 'lib/sinclair/config_factory.rb', line 138 def child self.class.new( config_class: Class.new(config_class), config_attributes: config_attributes ) end |
#config ⇒ Config, Object
Returns current instance of config
the method returns the same instance until reset_config
is called
74 75 76 |
# File 'lib/sinclair/config_factory.rb', line 74 def config @config ||= config_class.new end |
#configure(config_hash = {}) {|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
114 115 116 117 118 119 120 121 122 |
# File 'lib/sinclair/config_factory.rb', line 114 def configure(config_hash = {}, &block) config_builder.instance_eval(&block) if block config_builder.instance_eval do config_hash.each do |key, value| public_send(key, value) end end end |
#reset_config ⇒ NilClass
Cleans the current config instance
After cleaning it, #config will generate a new instance
88 89 90 |
# File 'lib/sinclair/config_factory.rb', line 88 def reset_config @config = nil end |