Class: FigTree::ConfigStruct
- Inherits:
-
Object
- Object
- FigTree::ConfigStruct
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/fig_tree.rb
Overview
Class that defines and keeps the configuration values.
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
Constructor Details
#initialize(name) ⇒ ConfigStruct
Returns a new instance of ConfigStruct.
42 43 44 45 46 47 |
# File 'lib/fig_tree.rb', line 42 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:
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/fig_tree.rb', line 127 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:
74 75 76 77 78 79 |
# File 'lib/fig_tree.rb', line 74 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.
58 59 60 61 |
# File 'lib/fig_tree.rb', line 58 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:
64 65 66 |
# File 'lib/fig_tree.rb', line 64 def inspect "#{@name}: #{@settings.inspect} #{@setting_objects.inspect}" end |
#reset! ⇒ Object
Reset config back to default values.
50 51 52 53 |
# File 'lib/fig_tree.rb', line 50 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:
118 119 120 121 122 123 124 |
# File 'lib/fig_tree.rb', line 118 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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fig_tree.rb', line 100 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.
89 90 91 92 93 94 |
# File 'lib/fig_tree.rb', line 89 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
69 70 71 |
# File 'lib/fig_tree.rb', line 69 def to_h @settings.map { |k, v| [k, v.value] }.to_h end |