2012-09-10 19:00:18 -04:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2013-01-21 14:08:30 -05:00
|
|
|
# Matcher for specific namespace
|
|
|
|
class Namespace < self
|
|
|
|
include Equalizer.new(:pattern)
|
2012-09-10 19:00:18 -04:00
|
|
|
|
|
|
|
# Enumerate subjects
|
|
|
|
#
|
|
|
|
# @return [self]
|
2013-01-21 14:08:30 -05:00
|
|
|
# if block given
|
|
|
|
#
|
|
|
|
# @return [Enumerator<Subject>]
|
|
|
|
# otherwise
|
2012-09-10 19:00:18 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def each(&block)
|
|
|
|
return to_enum unless block_given?
|
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
scopes.each do |scope|
|
|
|
|
emit_scope_matches(scope, &block)
|
2012-09-10 19:00:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-01-21 14:08:30 -05:00
|
|
|
# Return namespace
|
2012-09-10 19:00:18 -04:00
|
|
|
#
|
2013-01-21 14:08:30 -05:00
|
|
|
# @return [Class::Module]
|
2012-09-10 19:00:18 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-01-21 14:08:30 -05:00
|
|
|
attr_reader :namespace
|
|
|
|
|
|
|
|
MATCHERS = [Matcher::Methods::Singleton, Matcher::Methods::Instance]
|
2012-09-10 19:00:18 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Initialize object space matcher
|
|
|
|
#
|
2013-01-21 14:08:30 -05:00
|
|
|
# @param [Class, Module] namespace
|
2012-09-10 19:00:18 -04:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-01-21 14:08:30 -05:00
|
|
|
def initialize(namespace)
|
|
|
|
@namespace = namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return pattern
|
|
|
|
#
|
|
|
|
# @return [Regexp]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def pattern
|
2013-01-21 16:56:52 -05:00
|
|
|
%r(\A#{Regexp.escape(namespace.name)}(?:::)?\z)
|
2012-09-10 19:00:18 -04:00
|
|
|
end
|
2013-01-21 14:08:30 -05:00
|
|
|
memoize :pattern
|
2012-09-10 19:00:18 -04:00
|
|
|
|
|
|
|
# Yield matchers for scope
|
|
|
|
#
|
|
|
|
# @param [::Class,::Module] scope
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_scope_matches(scope, &block)
|
2013-01-21 14:08:30 -05:00
|
|
|
MATCHERS.each do |matcher|
|
2013-01-21 16:56:52 -05:00
|
|
|
matcher.each(scope, &block)
|
2012-09-10 19:00:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return scope enumerator
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Object>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def scopes(&block)
|
|
|
|
return to_enum(__method__) unless block_given?
|
|
|
|
|
2013-01-21 16:56:52 -05:00
|
|
|
::ObjectSpace.each_object(Module).each do |scope|
|
2012-09-10 19:00:18 -04:00
|
|
|
emit_scope(scope, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Yield scope if name matches pattern
|
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @param [::Module,::Class] scope
|
2012-09-10 19:00:18 -04:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_scope(scope)
|
2013-01-21 16:56:52 -05:00
|
|
|
if pattern =~ scope.name
|
2012-09-10 19:00:18 -04:00
|
|
|
yield scope
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|