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.



273
274
275
276
277
# File 'lib/fig_tree.rb', line 273

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

#configConfigStruct

Returns:



240
241
242
# File 'lib/fig_tree.rb', line 240

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

#configure(&block) ⇒ Object

Configure the settings with values.



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/fig_tree.rb', line 226

def configure(&block)
  if defined?(Rake) && defined?(Rake.application)
    tasks = Rake.application.top_level_tasks
    if tasks.any? { |t| %w(assets webpacker yarn).include?(t.split(':').first) }
      puts 'Skipping Deimos configuration since we are in JS/CSS compilation'
      return
    end
  end
  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:

  • values (Hash)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/fig_tree.rb', line 247

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