free_mutant/lib/mutant/expression/method.rb

77 lines
1.5 KiB
Ruby
Raw Normal View History

module Mutant
class Expression
# Explicit method expression
class Method < self
2014-07-06 03:41:04 +00:00
MATCHERS = IceNine.deep_freeze(
'.' => Matcher::Methods::Singleton,
'#' => Matcher::Methods::Instance
2014-07-06 03:41:04 +00:00
)
register(
/\A(?<scope_name>#{SCOPE_PATTERN})(?<scope_symbol>[.#])(?<method_name>#{METHOD_NAME_PATTERN})\z/
)
# Return method matcher
#
2014-07-06 02:44:55 +00:00
# @param [Env] env
#
# @return [Matcher::Method]
#
# @api private
#
2014-07-06 02:44:55 +00:00
def matcher(env)
methods_matcher = MATCHERS.fetch(scope_symbol).new(env, scope)
method = methods_matcher.methods.detect do |meth|
2014-06-16 10:50:32 +00:00
meth.name.equal?(method_name)
2014-07-07 20:04:03 +00:00
end or raise NameError, "Cannot find method #{method_name}"
2014-07-06 02:44:55 +00:00
methods_matcher.matcher.build(env, scope, method)
end
private
# Return scope name
#
# @return [String]
#
# @api private
#
def scope_name
match[__method__]
end
# Return scope
#
# @return [Class, Method]
#
# @api private
#
def scope
Mutant.constant_lookup(scope_name)
end
# Return method name
#
# @return [String]
#
# @api private
#
def method_name
match[__method__].to_sym
end
# Return scope symbol
#
# @return [Symbol]
#
# @api private
#
def scope_symbol
match[__method__]
end
end # Method
end # Expression
end # Mutant