Module: FigTree::ClassMethods

Defined in:
lib/fig_tree.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#after_configure(&block) ⇒ Object

Pass a block to run after configuration is done.



266
267
268
269
270
# File 'lib/fig_tree.rb', line 266

def after_configure(&block)
  mod = self
  config.class.set_callback(:configure, :after,
                            proc { mod.instance_eval(&block) })
end

#configConfigStruct

Returns:



233
234
235
# File 'lib/fig_tree.rb', line 233

def config
  @config ||= ConfigStruct.new('config')
end

#configure(&block) ⇒ Object

Configure the settings with values.



226
227
228
229
230
# File 'lib/fig_tree.rb', line 226

def configure(&block)
  config.run_callbacks(:configure) do
    config.instance_eval(&block)
  end
end

#define_settings(&block) ⇒ Object

Define and redefine settings.



221
222
223
# File 'lib/fig_tree.rb', line 221

def define_settings(&block)
  config.instance_eval(&block)
end

#with_config(values = {}, &block) ⇒ Object

Evaluate a block with the given configuration values. Reset back to the original values when done.

Parameters:

  • (defaults to: {})


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/fig_tree.rb', line 240

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