2013-07-28 19:03:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-09-10 19:00:18 -04:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2013-04-20 15:16:48 -04:00
|
|
|
|
2013-01-21 14:08:30 -05:00
|
|
|
# Matcher for specific namespace
|
|
|
|
class Namespace < self
|
2013-06-27 16:18:07 -04:00
|
|
|
include Concord::Public.new(:cache, :namespace)
|
2013-04-19 17:36:49 -04:00
|
|
|
|
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?
|
2013-06-14 14:54:02 -04:00
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
scopes.each do |scope|
|
2013-06-27 16:18:07 -04:00
|
|
|
Scope.each(cache, scope, &block)
|
2012-09-10 19:00:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-21 14:08:30 -05:00
|
|
|
# Return pattern
|
|
|
|
#
|
|
|
|
# @return [Regexp]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def pattern
|
2014-06-06 16:28:01 -04:00
|
|
|
/\A#{Regexp.escape(namespace)}(?:\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
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2014-01-18 17:58:14 -05:00
|
|
|
# Return scope name
|
|
|
|
#
|
|
|
|
# @param [Class,Module] scope
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
# if scope has a name and does not raise exceptions optaining it
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.scope_name(scope)
|
|
|
|
scope.name
|
|
|
|
rescue => exception
|
|
|
|
$stderr.puts <<-MESSAGE
|
|
|
|
WARNING:
|
|
|
|
While optaining #{scope.class}#name from: #{scope.inspect}
|
|
|
|
It raised an error: #{exception.inspect} fix your lib!
|
|
|
|
MESSAGE
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-09-10 19:00:18 -04:00
|
|
|
# Yield scope if name matches pattern
|
|
|
|
#
|
2013-04-20 14:45:58 -04:00
|
|
|
# @param [Module,Class] scope
|
2012-09-10 19:00:18 -04:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_scope(scope)
|
2014-01-18 17:58:14 -05:00
|
|
|
name = self.class.scope_name(scope)
|
|
|
|
unless name.nil? or name.kind_of?(String)
|
|
|
|
$stderr.puts <<-MESSAGE
|
|
|
|
WARNING:
|
2014-05-29 16:21:11 -04:00
|
|
|
#{scope.class}#name from: #{scope.inspect} did not return a String or nil.
|
|
|
|
Fix your lib to support normal ruby semantics!
|
2014-01-18 17:58:14 -05:00
|
|
|
MESSAGE
|
2014-03-26 16:38:44 -04:00
|
|
|
return
|
2014-01-18 17:58:14 -05:00
|
|
|
end
|
2013-07-17 08:59:47 -04:00
|
|
|
if pattern =~ name
|
2013-04-17 23:31:21 -04:00
|
|
|
yield scope
|
2012-09-10 19:00:18 -04:00
|
|
|
end
|
|
|
|
end
|
2013-04-20 15:16:48 -04:00
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # Namespace
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|