free_mutant/lib/mutant/zombifier.rb

89 lines
1.7 KiB
Ruby
Raw Normal View History

module Mutant
# Zombifier namespace
class Zombifier
include Adamantium::Flat, Concord.new(:namespace)
2014-04-04 11:46:55 -04:00
# Excluded into zombification
2014-05-27 11:12:36 -04:00
includes = %w[
2014-04-04 11:46:55 -04:00
mutant
morpher
adamantium
equalizer
anima
concord
2014-05-27 11:12:36 -04:00
]
2014-04-04 11:46:55 -04:00
2014-05-27 11:12:36 -04:00
INCLUDES = %r{\A#{Regexp.union(includes)}(?:/.*)?\z}.freeze
# Initialize object
#
# @param [Symbol] namespace
#
# @return [undefined]
#
# @api private
#
def initialize(namespace)
2014-04-04 11:46:55 -04:00
@zombified = Set.new
@highjack = RequireHighjack.new(Kernel, method(:require))
2014-04-20 14:27:05 -04:00
super(namespace)
end
# Perform zombification of target library
#
# @param [String] logical_name
# @param [Symbol] namespace
#
2014-06-28 16:52:47 -04:00
# @return [self]
#
# @api private
#
def self.run(logical_name, namespace)
new(namespace).run(logical_name)
2014-06-28 16:52:47 -04:00
self
end
# Run zombifier
#
# @param [String] logical_name
#
# @return [undefined]
#
# @api private
#
def run(logical_name)
2014-04-04 11:46:55 -04:00
@highjack.infect
require(logical_name)
end
2014-04-04 11:46:55 -04:00
# Test if logical name is subjected to zombification
#
# @param [String]
#
# @api private
#
def include?(logical_name)
!@zombified.include?(logical_name) && INCLUDES =~ logical_name
end
# Require file in zombie namespace
#
2014-06-15 11:50:38 -04:00
# @param [#to_s] logical_name
#
# @return [self]
#
# @api private
#
def require(logical_name)
2014-06-15 11:50:38 -04:00
logical_name = logical_name.to_s
2014-04-04 11:46:55 -04:00
@highjack.original.call(logical_name)
return unless include?(logical_name)
@zombified << logical_name
2014-04-04 10:18:45 -04:00
file = File.find(logical_name)
file.zombify(namespace) if file
self
end
end # Zombifier
end # Mutant