free_mutant/lib/mutant/expression/namespace.rb

83 lines
1.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-09-12 13:15:43 +00:00
module Mutant
class Expression
# Abstract base class for expressions matching namespaces
class Namespace < self
include AbstractType, Anima.new(:scope_name)
private(*anima.attribute_names)
2014-09-17 00:47:09 +00:00
# Recursive namespace expression
class Recursive < self
REGEXP = /\A#{SCOPE_NAME_PATTERN}?\*\z/.freeze
# Initialize object
#
# @return [undefined]
def initialize(*)
super
2014-06-29 21:42:18 +00:00
@recursion_pattern = Regexp.union(
/\A#{scope_name}\z/,
/\A#{scope_name}::/,
/\A#{scope_name}[.#]/
2014-06-29 21:42:18 +00:00
)
end
# Syntax for expression
#
# @return [String]
def syntax
"#{scope_name}*"
end
memoize :syntax
# Matcher for expression
#
# @return [Matcher]
def matcher
Matcher::Namespace.new(self)
end
# Length of match with other expression
#
# @param [Expression] expression
#
# @return [Integer]
def match_length(expression)
if eql?(expression)
syntax.length
elsif @recursion_pattern.match?(expression.syntax)
scope_name.length
else
0
end
end
end # Recursive
# Exact namespace expression
class Exact < self
MATCHER = Matcher::Scope
private_constant(*constants(false))
REGEXP = /\A#{SCOPE_NAME_PATTERN}\z/.freeze
# Matcher matcher on expression
#
# @return [Matcher]
def matcher
Matcher::Scope.new(Object.const_get(scope_name))
end
# Syntax for expression
#
# @return [String]
alias_method :syntax, :scope_name
public :syntax
end # Exact
end # Namespace
2016-04-10 14:33:47 -07:00
end # Expression
end # Mutant