2013-07-28 19:03:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
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
|
|
|
|
2013-07-04 18:54:50 -04:00
|
|
|
# Dispatching builder, detects adamantium case
|
|
|
|
#
|
|
|
|
# @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
|
|
|
|
if scope.ancestors.include?(::Adamantium) and 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
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# returns true if node matches method
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# returns false if node NOT matches method
|
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
|
|
|
|
scope.original_instance_method(method.name).source_location
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Memoized
|
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
end # Instance
|
|
|
|
end # Method
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|