2012-07-23 22:54:35 +02:00
|
|
|
module Mutant
|
2012-07-31 04:10:37 +02:00
|
|
|
class Matcher
|
2013-04-17 20:31:21 -07:00
|
|
|
class Method
|
2012-07-26 19:25:23 +02:00
|
|
|
# Matcher for instance methods
|
2012-08-09 23:07:22 +02:00
|
|
|
class Instance < self
|
2013-01-13 22:30:06 +01:00
|
|
|
SUBJECT_CLASS = Subject::Method::Instance
|
2012-08-28 20:07:55 +02:00
|
|
|
|
2014-03-28 15:29:57 +00:00
|
|
|
# Dispatching builder, detects memoizable case
|
2013-07-05 00:54:50 +02:00
|
|
|
#
|
|
|
|
# @param [Cache] cache
|
|
|
|
# @param [Class, Module] scope
|
|
|
|
# @param [UnboundMethod] method
|
|
|
|
#
|
|
|
|
# @return [Matcher::Method::Instance]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.build(cache, scope, method)
|
2013-07-28 20:00:53 +02:00
|
|
|
name = method.name
|
2014-06-09 10:51:26 +00:00
|
|
|
if scope.ancestors.include?(::Memoizable) && scope.memoized?(name)
|
2013-07-05 00:54:50 +02:00
|
|
|
return Memoized.new(cache, scope, method)
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2012-08-16 04:10:54 +02:00
|
|
|
# Return identification
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def identification
|
|
|
|
"#{scope.name}##{method_name}"
|
|
|
|
end
|
2013-01-21 22:56:52 +01:00
|
|
|
memoize :identification
|
2012-08-16 19:10:24 +02:00
|
|
|
|
2013-06-04 19:22:33 +02:00
|
|
|
NAME_INDEX = 0
|
|
|
|
|
2012-07-23 22:54:35 +02:00
|
|
|
private
|
2012-07-26 19:25:23 +02:00
|
|
|
|
2012-08-16 19:10:24 +02:00
|
|
|
# Check if node is matched
|
2012-07-31 04:10:37 +02:00
|
|
|
#
|
2013-06-04 10:25:13 +02:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-08-16 19:10:24 +02:00
|
|
|
#
|
|
|
|
# @return [true]
|
2013-09-11 20:48:44 +02:00
|
|
|
# if node matches method
|
2012-08-16 19:10:24 +02:00
|
|
|
#
|
|
|
|
# @return [false]
|
2013-09-11 20:48:44 +02:00
|
|
|
# otherwise
|
2012-07-24 01:41:08 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-16 19:10:24 +02:00
|
|
|
def match?(node)
|
2013-06-04 19:22:33 +02:00
|
|
|
location = node.location || return
|
|
|
|
expression = location.expression || return
|
|
|
|
expression.line == source_line &&
|
|
|
|
node.type == :def &&
|
|
|
|
node.children[NAME_INDEX] == method_name
|
2012-07-23 22:54:35 +02:00
|
|
|
end
|
|
|
|
|
2013-07-05 00:54:50 +02:00
|
|
|
# Matcher for memoized instance methods
|
|
|
|
class Memoized < self
|
2013-07-05 13:28:21 +02:00
|
|
|
SUBJECT_CLASS = Subject::Method::Instance::Memoized
|
2013-07-05 00:54:50 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Return source location
|
|
|
|
#
|
|
|
|
# @return [Array]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_location
|
2014-02-02 22:48:08 +01:00
|
|
|
scope.unmemoized_instance_method(method.name).source_location
|
2013-07-05 00:54:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end # Memoized
|
|
|
|
|
2013-06-04 10:25:13 +02:00
|
|
|
end # Instance
|
|
|
|
end # Method
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|