free_mutant/lib/mutant/matcher/namespace.rb

94 lines
1.8 KiB
Ruby
Raw Normal View History

module Mutant
class Matcher
# Matcher for specific namespace
class Namespace < self
2013-05-21 20:40:33 -04:00
include Concord::Public.new(:namespace)
2013-04-19 17:36:49 -04:00
# Enumerate subjects
#
# @return [self]
# if block given
#
# @return [Enumerator<Subject>]
# otherwise
#
# @api private
#
def each(&block)
return to_enum unless block_given?
2013-06-14 14:54:02 -04:00
scopes.each do |scope|
Scope.each(scope, &block)
end
self
end
private
# Return pattern
#
# @return [Regexp]
#
# @api private
#
def pattern
2013-04-19 17:36:49 -04:00
%r(\A#{Regexp.escape(namespace.name)}(?:::)?)
end
memoize :pattern
# Return scope enumerator
#
# @return [Enumerable<Object>]
#
# @api private
#
def scopes(&block)
return to_enum(__method__) unless block_given?
::ObjectSpace.each_object(Module).each do |scope|
emit_scope(scope, &block)
end
end
# Test for bogus class
#
# FIXME: Remove this hack once bogus is fixed!
#
# @param [Object] object
#
# @return [true]
# if is a bogous class
#
# @return [false]
# otherwise
#
# @api private
#
def self.bogous?(object)
defined?(Bogus::Fake) && object == Bogus::Fake
end
# Yield scope if name matches pattern
#
2013-04-20 14:45:58 -04:00
# @param [Module,Class] scope
#
# @return [undefined]
#
# @api private
#
def emit_scope(scope)
return if self.class.bogous?(scope)
name = scope.name
# FIXME Fix nokogiri!
return unless name.kind_of?(String)
2013-04-17 23:31:21 -04:00
if pattern =~ scope.name
yield scope
end
end
2013-06-14 14:54:02 -04:00
end # Namespace
end # Matcher
end # Mutant