free_mutant/lib/mutant/matcher/namespace.rb
Markus Schirp f2297e3472 Use a more error tolerant way to access {Module,Class}#name
"Many" library authors override Class or Modules #name not to return a
String or nil. They are not aware these are integral APIs for
reflection / tools. This change prints an ugly warning message in case
the API was violated.

Mutant used to crash often because of this LSP violation. This change
should provide a generic workaround.
2014-01-18 23:58:14 +01:00

105 lines
2.1 KiB
Ruby

# encoding: utf-8
module Mutant
class Matcher
# Matcher for specific namespace
class Namespace < self
include Concord::Public.new(:cache, :namespace)
# Enumerate subjects
#
# @return [self]
# if block given
#
# @return [Enumerator<Subject>]
# otherwise
#
# @api private
#
def each(&block)
return to_enum unless block_given?
scopes.each do |scope|
Scope.each(cache, scope, &block)
end
self
end
private
# Return pattern
#
# @return [Regexp]
#
# @api private
#
def pattern
/\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
# 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
# Yield scope if name matches pattern
#
# @param [Module,Class] scope
#
# @return [undefined]
#
# @api private
#
def emit_scope(scope)
name = self.class.scope_name(scope)
# FIXME: Fix nokogiri to return a string here
unless name.nil? or name.kind_of?(String)
$stderr.puts <<-MESSAGE
WARNING:
#{scope.class}#name did not return a string or nil.
Fix your lib!
MESSAGE
end
if pattern =~ name
yield scope
end
end
end # Namespace
end # Matcher
end # Mutant