2012-11-21 16:28:08 -05:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
|
|
|
# Abstract base class for matcher that returns subjects extracted from scope methods
|
|
|
|
class ScopeMethods < self
|
2012-11-26 05:30:00 -05:00
|
|
|
include AbstractType
|
2012-11-21 16:28:08 -05:00
|
|
|
|
|
|
|
# Return scope
|
|
|
|
#
|
|
|
|
# @return [Class,Model]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
attr_reader :scope
|
|
|
|
|
|
|
|
# Enumerate subjects
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# if block given
|
|
|
|
#
|
|
|
|
# @return [Enumerator<Subject>]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def each(&block)
|
|
|
|
return to_enum unless block_given?
|
2012-12-07 17:27:21 -05:00
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
methods.each do |method|
|
|
|
|
emit_matches(method, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-12-07 17:27:21 -05:00
|
|
|
# Return methods
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Method, UnboundMethod>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def methods
|
|
|
|
method_names.map do |name|
|
|
|
|
access(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
memoize :methods
|
|
|
|
|
|
|
|
# Return method matcher class
|
|
|
|
#
|
|
|
|
# @return [Class:Matcher::Method]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def matcher
|
|
|
|
self.class::MATCHER
|
|
|
|
end
|
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
# Initialize object
|
|
|
|
#
|
|
|
|
# @param [Class,Module] scope
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def initialize(scope)
|
|
|
|
@scope = scope
|
|
|
|
end
|
|
|
|
|
|
|
|
# Emit matches for method
|
|
|
|
#
|
2012-12-07 17:27:21 -05:00
|
|
|
# @param [UnboundMethod, Method] method
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_matches(method)
|
|
|
|
matcher.new(scope, method).each do |subject|
|
|
|
|
yield subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-07 17:27:21 -05:00
|
|
|
# Return method names
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
2012-12-07 17:27:21 -05:00
|
|
|
# @return [Enumerable<Symbol>]
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-07 17:27:21 -05:00
|
|
|
abstract_method :method_names
|
2012-11-21 16:28:08 -05:00
|
|
|
|
|
|
|
class Singleton < self
|
|
|
|
MATCHER = Mutant::Matcher::Method::Singleton
|
|
|
|
|
2012-12-07 17:27:21 -05:00
|
|
|
# Return method for name
|
|
|
|
#
|
|
|
|
# @param [Symbol] method_name
|
|
|
|
#
|
|
|
|
# @return [Method]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def access(method_name)
|
|
|
|
scope.method(method_name)
|
|
|
|
end
|
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
# Return singleton methods defined on scope
|
|
|
|
#
|
|
|
|
# @param [Class|Module] scope
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Symbol>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-07 17:27:21 -05:00
|
|
|
def method_names
|
2012-11-21 16:28:08 -05:00
|
|
|
singleton_class = scope.singleton_class
|
|
|
|
|
|
|
|
names =
|
|
|
|
singleton_class.public_instance_methods(false) +
|
|
|
|
singleton_class.private_instance_methods(false) +
|
|
|
|
singleton_class.protected_instance_methods(false)
|
|
|
|
|
2012-12-07 17:27:21 -05:00
|
|
|
names.sort.reject do |name|
|
2012-11-21 16:28:08 -05:00
|
|
|
name.to_sym == :__class_init__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Instance < self
|
|
|
|
MATCHER = Mutant::Matcher::Method::Instance
|
|
|
|
|
2012-12-07 17:27:21 -05:00
|
|
|
# Return method for name
|
|
|
|
#
|
|
|
|
# @param [Symbol] method_name
|
|
|
|
#
|
|
|
|
# @return [UnboundMethod]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def access(method_name)
|
|
|
|
scope.instance_method(method_name)
|
|
|
|
end
|
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
# Return instance methods names of scope
|
|
|
|
#
|
|
|
|
# @param [Class|Module] scope
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Symbol>]
|
|
|
|
#
|
2012-12-07 17:27:21 -05:00
|
|
|
def method_names
|
2012-11-21 16:28:08 -05:00
|
|
|
scope = self.scope
|
|
|
|
return [] unless scope.kind_of?(Module)
|
|
|
|
|
|
|
|
names =
|
|
|
|
scope.public_instance_methods(false) +
|
|
|
|
scope.private_instance_methods(false) +
|
|
|
|
scope.protected_instance_methods(false)
|
|
|
|
|
2012-12-07 17:27:21 -05:00
|
|
|
names.uniq.sort
|
2012-11-21 16:28:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|