2013-07-28 16:03:06 -07:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-09-11 01:00:18 +02:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2013-04-20 21:16:48 +02:00
|
|
|
|
2013-01-21 20:08:30 +01:00
|
|
|
# Matcher for specific namespace
|
|
|
|
class Namespace < self
|
2013-06-27 22:18:07 +02:00
|
|
|
include Concord::Public.new(:cache, :namespace)
|
2013-04-19 23:36:49 +02:00
|
|
|
|
2012-09-11 01:00:18 +02:00
|
|
|
# Enumerate subjects
|
|
|
|
#
|
|
|
|
# @return [self]
|
2013-01-21 20:08:30 +01:00
|
|
|
# if block given
|
|
|
|
#
|
|
|
|
# @return [Enumerator<Subject>]
|
|
|
|
# otherwise
|
2012-09-11 01:00:18 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def each(&block)
|
|
|
|
return to_enum unless block_given?
|
2013-06-14 20:54:02 +02:00
|
|
|
|
2012-11-21 22:28:08 +01:00
|
|
|
scopes.each do |scope|
|
2013-06-27 22:18:07 +02:00
|
|
|
Scope.each(cache, scope, &block)
|
2012-09-11 01:00:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-21 20:08:30 +01:00
|
|
|
# Return pattern
|
|
|
|
#
|
|
|
|
# @return [Regexp]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def pattern
|
2013-07-28 12:16:45 -07:00
|
|
|
/\A#{Regexp.escape(namespace.name)}(?:::)?/
|
2012-09-11 01:00:18 +02:00
|
|
|
end
|
2013-01-21 20:08:30 +01:00
|
|
|
memoize :pattern
|
2012-09-11 01:00:18 +02:00
|
|
|
|
|
|
|
# Return scope enumerator
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Object>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def scopes(&block)
|
|
|
|
return to_enum(__method__) unless block_given?
|
|
|
|
|
2013-01-21 22:56:52 +01:00
|
|
|
::ObjectSpace.each_object(Module).each do |scope|
|
2012-09-11 01:00:18 +02:00
|
|
|
emit_scope(scope, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Yield scope if name matches pattern
|
|
|
|
#
|
2013-04-20 20:45:58 +02:00
|
|
|
# @param [Module,Class] scope
|
2012-09-11 01:00:18 +02:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_scope(scope)
|
2013-06-24 22:30:25 +02:00
|
|
|
name = scope.name
|
2013-07-28 12:16:45 -07:00
|
|
|
# FIXME: Fix nokogiri to return a string here
|
2013-06-24 22:30:25 +02:00
|
|
|
return unless name.kind_of?(String)
|
2013-07-17 14:59:47 +02:00
|
|
|
if pattern =~ name
|
2013-04-17 20:31:21 -07:00
|
|
|
yield scope
|
2012-09-11 01:00:18 +02:00
|
|
|
end
|
|
|
|
end
|
2013-04-20 21:16:48 +02:00
|
|
|
|
2013-06-14 20:54:02 +02:00
|
|
|
end # Namespace
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|