free_mutant/lib/mutant/matcher/namespace.rb
Markus Schirp a19f3b1691 Nuke UTF-8 encoding headers
* I do not use 1.9.3
* Also keeping them in each file increases mental overhead (true it *can* be autoamted)
* None of the files encodes NON ASCII chars.
* I do not expect it makes any difference, since nobody programmatically
  will consume strings generated by mutant under the assumption they are UTF-8 encoded.
* 1.9.3 Users have to deal with the encoding fuckup under ruby anyways.
2014-06-09 15:37:48 +00:00

101 lines
2.1 KiB
Ruby

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)}(?:\z|::)/
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)
unless name.nil? or name.kind_of?(String)
$stderr.puts <<-MESSAGE
WARNING:
#{scope.class}#name from: #{scope.inspect} did not return a String or nil.
Fix your lib to support normal ruby semantics!
MESSAGE
return
end
yield scope if pattern =~ name
end
end # Namespace
end # Matcher
end # Mutant