Class: Mutant::RequireHighjack

Inherits:
Object
  • Object
show all
Defined in:
lib/mutant/require_highjack.rb

Overview

Require highjack

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#original#call (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return original method

Returns:

  • (#call)


12
13
14
# File 'lib/mutant/require_highjack.rb', line 12

def original
  @original
end

Instance Method Details

#disinfectself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Imperfectly disinfect kernel from highjack

Returns:

  • (self)


52
53
54
55
56
57
58
59
60
61
# File 'lib/mutant/require_highjack.rb', line 52

def disinfect
  original = @original
  target.module_eval do
    undef :require
    define_method(:require) do |logical_name|
      original.call(logical_name)
    end
    module_function :require
  end
end

#infectself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Infect kernel with highjack

Returns:

  • (self)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mutant/require_highjack.rb', line 34

def infect
  callback = @callback
  @original = target.method(:require)
  target.module_eval do
    undef :require
    define_method(:require) do |logical_name|
      callback.call(logical_name)
    end
    module_function :require
  end
end

#runself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run block with highjacked require

Returns:

  • (self)


20
21
22
23
24
25
26
# File 'lib/mutant/require_highjack.rb', line 20

def run
  infect
  yield
  self
ensure
  disinfect
end