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