Module: FigTree::ClassMethods
- Defined in:
- lib/fig_tree.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#after_configure(&block) ⇒ Object
Pass a block to run after configuration is done.
- #config ⇒ ConfigStruct
-
#configure(&block) ⇒ Object
Configure the settings with values.
-
#define_settings(&block) ⇒ Object
Define and redefine settings.
- #trap_config(&block) ⇒ Object
-
#with_config(values = {}, &block) ⇒ Object
Evaluate a block with the given configuration values.
Instance Method Details
#after_configure(&block) ⇒ Object
Pass a block to run after configuration is done.
339 340 341 342 343 344 |
# File 'lib/fig_tree.rb', line 339 def after_configure(&block) mod = self config.class.set_callback(:configure, :after, proc { mod.instance_eval(&block) }) config.clear_removed_fields! end |
#config ⇒ ConfigStruct
302 303 304 |
# File 'lib/fig_tree.rb', line 302 def config @config ||= ConfigStruct.new('config') end |
#configure(&block) ⇒ Object
Configure the settings with values.
295 296 297 298 299 |
# File 'lib/fig_tree.rb', line 295 def configure(&block) config.run_callbacks(:configure) do config.instance_eval(&block) end end |
#define_settings(&block) ⇒ Object
Define and redefine settings.
290 291 292 |
# File 'lib/fig_tree.rb', line 290 def define_settings(&block) config.instance_eval(&block) end |
#trap_config(&block) ⇒ Object
306 307 308 |
# File 'lib/fig_tree.rb', line 306 def trap_config(&block) config.trap_config(&block) end |
#with_config(values = {}, &block) ⇒ Object
Evaluate a block with the given configuration values. Reset back to the original values when done.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/fig_tree.rb', line 313 def with_config(values={}, &block) # rubocop:disable Metrics/AbcSize set_value = lambda do |k, v| obj = self.config tokens = k.split('.') tokens[0..-2].each do |token| obj = obj.send(token) end obj.send(:"#{tokens.last}=", v) end get_value = lambda do |k| obj = self.config tokens = k.split('.') tokens.each do |token| obj = obj.send(token) end obj end old_values = values.keys.map { |k| [k, get_value.call(k)] }.to_h values.each { |k, v| set_value.call(k, v) } block.call old_values.each { |k, v| set_value.call(k, v) } end |