Class: Belt::CLI::BackupConfig::ConfigEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/cli/backup_config.rb

Overview

Evaluates the belt.rb config file in an isolated context that intercepts Belt.configure calls

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ConfigEvaluator

Returns a new instance of ConfigEvaluator.



45
46
47
# File 'lib/belt/cli/backup_config.rb', line 45

def initialize(config)
  @config = config
end

Instance Method Details

#evaluate(content, filename) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/belt/cli/backup_config.rb', line 49

def evaluate(content, filename)
  # The config file calls Belt.configure { |config| ... }
  # We wrap the content to intercept the Belt constant lookup
  # by evaluating in a module where Belt is redefined
  config = @config
  sandbox = Module.new
  # Define a Belt module inside the sandbox that has .configure
  belt_proxy = Module.new do
    define_singleton_method(:configure) do |&block|
      block.call(ConfigDSL.new(config))
    end
  end
  sandbox.const_set(:Belt, belt_proxy)

  # Evaluate the file content within the sandbox module
  sandbox.module_eval(content, filename)
end