Module: DeepCover::ModuleOverride

Included in:
KernelAutoloadOverride, LoadOverride, ModuleAutoloadOverride, RequireOverride
Defined in:
lib/deep_cover/module_override.rb

Overview

Helps redefine methods in overriden_modules. For each methods in Mod, this defines ‘<method>_without_deep_cover`. Set `active` to true or false to alias <method> to one or the other.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#overriden_modulesObject (readonly)

Returns the value of attribute overriden_modules.



8
9
10
# File 'lib/deep_cover/module_override.rb', line 8

def overriden_modules
  @overriden_modules
end

Instance Method Details

#active=(active) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/deep_cover/module_override.rb', line 10

def active=(active)
  each do |mod, method_name|
    mod.send :alias_method, method_name, :"#{method_name}_#{active ? 'with' : 'without'}_deep_cover"
    if mod == ::Kernel
      mod.send :private, method_name
    end
  end
end

#each(&block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/deep_cover/module_override.rb', line 31

def each(&block)
  overriden_modules.each do |mod|
    instance_methods(false).each do |method_name|
      yield mod, method_name
    end
  end
end

#override(*modules) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deep_cover/module_override.rb', line 19

def override(*modules)
  @overriden_modules = modules
  each do |mod, method_name|
    mod.send :alias_method, :"#{method_name}_without_deep_cover", method_name
    mod.send :define_method, :"#{method_name}_with_deep_cover", instance_method(method_name)
    if mod == ::Kernel
      mod.send :private, :"#{method_name}_without_deep_cover"
      mod.send :private, :"#{method_name}_with_deep_cover"
    end
  end
end