free_mutant/lib/mutant/require_highjack.rb

29 lines
649 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-09-12 13:15:43 +00:00
2014-04-04 12:38:06 +00:00
module Mutant
# Require highjack
module RequireHighjack
2014-04-04 12:38:06 +00:00
# Install require callback
2014-04-04 12:38:06 +00:00
#
# @param [Module] target
# @param [#call] callback
2014-04-04 12:38:06 +00:00
#
# @return [#call]
# the original implementation on singleton
def self.call(target, callback)
target.public_method(:require).tap do
target.module_eval do
undef_method(:require)
define_method(:require, &callback)
class << self
undef_method(:require)
end
define_singleton_method(:require, &callback)
2014-04-04 12:38:06 +00:00
end
end
end
end # RequireHighjack
end # Mutant