-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
namespace.rb
91 lines (79 loc) · 1.97 KB
/
namespace.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module Mutant
class Expression
# Abstract base class for expressions matching namespaces
class Namespace < self
include AbstractType, Anima.new(:scope_name)
private(*anima.attribute_names)
# Recursive namespace expression
class Recursive < self
REGEXP = /\A#{SCOPE_NAME_PATTERN}?\*\z/.freeze
# Initialize object
#
# @return [undefined]
#
# @api private
def initialize(*)
super
@recursion_pattern = Regexp.union(
/\A#{scope_name}\z/,
/\A#{scope_name}::/,
/\A#{scope_name}[.#]/
)
end
# Syntax for expression
#
# @return [String]
#
# @api private
def syntax
"#{scope_name}*"
end
memoize :syntax
# Matcher for expression
#
# @return [Matcher]
#
# @api private
def matcher
Matcher::Namespace.new(self)
end
# Length of match with other expression
#
# @param [Expression] expression
#
# @return [Fixnum]
#
# @api private
def match_length(expression)
if @recursion_pattern =~ 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]
#
# @api private
def matcher
Matcher::Scope.new(Object.const_get(scope_name))
end
# Syntax for expression
#
# @return [String]
#
# @api private
#
alias_method :syntax, :scope_name
public :syntax
end # Exact
end # Namespace
end # Namespace
end # Mutant