Module: Kettle::Change::ConstantChange

Defined in:
lib/kettle/change.rb

Class Method Summary collapse

Class Method Details

.to_mod(constants: nil, path: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kettle/change.rb', line 16

def to_mod(constants: nil, path: nil)
  Module.new do
    if constants && path
      define_method(:reset_const) do |*_args, &block|
        delete_const do
          block&.call
          load(path)
        end
      end

      define_method(:delete_const) do |*_args, &block|
        constants.each do |var|
          remove_const(var) if defined?(var)
        end
        block&.call
        nil
      end
    end
  end
end