Class: Deimos::Configurable::ConfigStruct
- Inherits:
-
Object
- Object
- Deimos::Configurable::ConfigStruct
- Includes:
- ActiveSupport::Callbacks, PhobosConfig
- Defined in:
- lib/deimos/config/configurable.rb,
lib/deimos/config/configuration.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#clone_and_reset ⇒ Object
:nodoc:.
-
#deprecate(old_config, new_config) ⇒ Object
Mark a configuration as deprecated and replaced with the new config.
-
#initialize(name) ⇒ ConfigStruct
constructor
A new instance of ConfigStruct.
-
#inspect ⇒ Object
:nodoc:.
-
#method_missing(method, *args, &block) ⇒ Object
:nodoc:.
-
#reset! ⇒ Object
Reset config back to default values.
-
#respond_to_missing?(method, include_all = true) ⇒ Boolean
:nodoc:.
-
#setting(name, default_value = nil, default_proc: nil, &block) ⇒ Object
Define a setting with the given name.
-
#setting_object(name, &block) ⇒ Object
Define a setting template for an array of objects via a block: setting_object :producer do setting :topic setting :class_name end This will create the
producermethod to define these values as well as theproducer_objectsmethod to retrieve them. - #to_h ⇒ Hash
Methods included from PhobosConfig
#phobos_config, #phobos_config_file=, #ssl_var_contents
Constructor Details
#initialize(name) ⇒ ConfigStruct
Returns a new instance of ConfigStruct.
64 65 66 67 68 69 |
# File 'lib/deimos/config/configurable.rb', line 64 def initialize(name) @name = name @settings = {} @setting_objects = {} @setting_templates = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
:nodoc:
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/deimos/config/configurable.rb', line 149 def method_missing(method, *args, &block) config_key = method.to_s.sub(/=$/, '').to_sym # Return the list of setting objects with the given name if config_key.to_s.end_with?('objects') return _setting_object_method(config_key) end # Define a new setting object with the given name if @setting_templates.key?(config_key) && block_given? return _new_setting_object_method(config_key, &block) end setting = @settings[config_key] if setting&.deprecation return _deprecated_config_method(method, *args) end return super unless setting if block_given? return _block_config_method(config_key, &block) end _default_config_method(config_key, *args) end |
Instance Method Details
#clone_and_reset ⇒ Object
:nodoc:
96 97 98 99 100 101 |
# File 'lib/deimos/config/configurable.rb', line 96 def clone_and_reset new_config = self.clone new_config.setting_objects = new_config.setting_objects.clone new_config.settings = new_config.settings.map { |k, v| [k, v.clone_and_reset] }.to_h new_config end |
#deprecate(old_config, new_config) ⇒ Object
Mark a configuration as deprecated and replaced with the new config.
80 81 82 83 |
# File 'lib/deimos/config/configurable.rb', line 80 def deprecate(old_config, new_config) @settings[old_config.to_sym] ||= ConfigValue.new @settings[old_config.to_sym].deprecation = new_config end |
#inspect ⇒ Object
:nodoc:
86 87 88 |
# File 'lib/deimos/config/configurable.rb', line 86 def inspect "#{@name}: #{@settings.inspect} #{@setting_objects.inspect}" end |
#reset! ⇒ Object
Reset config back to default values.
72 73 74 75 |
# File 'lib/deimos/config/configurable.rb', line 72 def reset! @setting_objects = @setting_templates.map { |k, _| [k, []] }.to_h @settings.values.each(&:reset!) end |
#respond_to_missing?(method, include_all = true) ⇒ Boolean
:nodoc:
140 141 142 143 144 145 146 |
# File 'lib/deimos/config/configurable.rb', line 140 def respond_to_missing?(method, include_all=true) method = method.to_s.sub(/=$/, '') method.ends_with?('objects') || @setting_templates.key?(method.to_sym) || @settings.key?(method.to_sym) || super end |
#setting(name, default_value = nil, default_proc: nil, &block) ⇒ Object
Define a setting with the given name.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/deimos/config/configurable.rb', line 122 def setting(name, default_value=nil, default_proc: nil, &block) if block_given? # Create a nested setting setting_config = @settings[name]&.value || ConfigStruct.new("#{@name}.#{name}") setting = ConfigValue.new setting.value = setting_config @settings[name] = setting setting_config.instance_eval(&block) else setting = ConfigValue.new setting.default_proc = default_proc setting.default_value = default_value setting.reset! @settings[name] = setting end end |
#setting_object(name, &block) ⇒ Object
Define a setting template for an array of objects via a block:
setting_object :producer do
setting :topic
setting :class_name
end
This will create the producer method to define these values as well as the producer_objects method to retrieve them.
111 112 113 114 115 116 |
# File 'lib/deimos/config/configurable.rb', line 111 def setting_object(name, &block) new_config = ConfigStruct.new("#{@name}.#{name}") @setting_objects[name] = [] @setting_templates[name] = new_config new_config.instance_eval(&block) end |
#to_h ⇒ Hash
91 92 93 |
# File 'lib/deimos/config/configurable.rb', line 91 def to_h @settings.map { |k, v| [k, v.value] }.to_h end |