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
|
2014-06-26 20:57:14 +00:00
|
|
|
#
|
|
|
|
# rubocop:disable LineLength
|
2013-01-21 20:08:30 +01:00
|
|
|
class Namespace < self
|
2014-06-30 13:06:57 +00:00
|
|
|
include Concord::Public.new(:env, :expression)
|
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?
|
2012-11-21 22:28:08 +01:00
|
|
|
scopes.each do |scope|
|
2014-06-30 12:06:51 +00:00
|
|
|
scope.each(&block)
|
2012-09-11 01:00:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Return scope enumerator
|
|
|
|
#
|
2014-06-30 12:06:51 +00:00
|
|
|
# @return [Array<Class, Module>]
|
2012-09-11 01:00:18 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-06-30 12:06:51 +00:00
|
|
|
def scopes
|
|
|
|
::ObjectSpace.each_object(Module).each_with_object([]) do |scope, aggregate|
|
2014-06-30 13:06:57 +00:00
|
|
|
aggregate << Scope.new(env, scope) if match?(scope)
|
2014-06-30 12:06:51 +00:00
|
|
|
end.sort_by(&:identification)
|
2012-09-11 01:00:18 +02:00
|
|
|
end
|
2014-06-30 12:06:51 +00:00
|
|
|
memoize :scopes
|
2012-09-11 01:00:18 +02:00
|
|
|
|
2014-01-18 23:58:14 +01: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
|
|
|
|
#
|
2014-06-26 20:57:14 +00:00
|
|
|
# rubocop:disable LineLength
|
|
|
|
#
|
2014-06-30 12:06:51 +00:00
|
|
|
def scope_name(scope)
|
2014-01-18 23:58:14 +01:00
|
|
|
scope.name
|
|
|
|
rescue => exception
|
2014-06-26 20:57:14 +00:00
|
|
|
$stderr.puts("WARNING: While optaining #{scope.class}#name from: #{scope.inspect} It raised an error: #{exception.inspect} fix your lib!")
|
2014-01-18 23:58:14 +01:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-06-30 12:06:51 +00:00
|
|
|
# Test scope if name matches expresion
|
2012-09-11 01:00:18 +02:00
|
|
|
#
|
2013-04-20 20:45:58 +02:00
|
|
|
# @param [Module,Class] scope
|
2012-09-11 01:00:18 +02:00
|
|
|
#
|
2014-06-30 12:06:51 +00:00
|
|
|
# @return [Boolean]
|
2012-09-11 01:00:18 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-06-30 12:06:51 +00:00
|
|
|
def match?(scope)
|
|
|
|
name = scope_name(scope) or return false
|
2014-06-29 22:33:40 +00:00
|
|
|
|
|
|
|
unless name.kind_of?(String)
|
2014-06-26 20:57:14 +00:00
|
|
|
$stderr.puts("WARNING: #{scope.class}#name from: #{scope.inspect} did not return a String or nil. Fix your lib to support normal ruby semantics!")
|
2014-06-30 12:06:51 +00:00
|
|
|
return false
|
2014-01-18 23:58:14 +01:00
|
|
|
end
|
2014-06-29 22:33:40 +00:00
|
|
|
|
2014-06-29 23:16:34 +00:00
|
|
|
scope_expression = Expression.try_parse(name)
|
2014-06-29 22:33:40 +00:00
|
|
|
|
|
|
|
unless scope_expression
|
|
|
|
$stderr.puts("WARNING: #{name.inspect} is not an identifiable ruby class name.")
|
2014-06-30 12:06:51 +00:00
|
|
|
return false
|
2014-06-29 22:33:40 +00:00
|
|
|
end
|
|
|
|
|
2014-06-30 12:06:51 +00:00
|
|
|
expression.prefix?(scope_expression)
|
2012-09-11 01:00:18 +02:00
|
|
|
end
|
2013-04-20 21:16:48 +02:00
|
|
|
|
2013-06-14 20:54:02 +02:00
|
|
|
end # Namespace
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|