/
node.rb
99 lines (79 loc) · 2.64 KB
/
node.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
92
93
94
95
96
97
98
99
class HttpRouter
class Node
autoload :Root, 'http_router/node/root'
autoload :Glob, 'http_router/node/glob'
autoload :GlobRegex, 'http_router/node/glob_regex'
autoload :Variable, 'http_router/node/variable'
autoload :Regex, 'http_router/node/regex'
autoload :SpanningRegex, 'http_router/node/spanning_regex'
autoload :GlobRegex, 'http_router/node/glob_regex'
autoload :FreeRegex, 'http_router/node/free_regex'
autoload :Arbitrary, 'http_router/node/arbitrary'
autoload :Request, 'http_router/node/request'
autoload :Lookup, 'http_router/node/lookup'
autoload :Path, 'http_router/node/path'
attr_reader :priority, :router, :node_position, :parent
def initialize(router, parent, matchers = [])
@router, @parent, @matchers = router, parent, matchers
end
def add_variable
add(Variable.new(@router, self))
end
def add_glob
add(Glob.new(@router, self))
end
def add_glob_regexp(matcher)
add(GlobRegex.new(@router, self, matcher))
end
def add_request(opts)
raise unless opts
add(Request.new(@router, self, opts))
end
def add_arbitrary(blk, allow_partial, param_names)
add(Arbitrary.new(@router, self, allow_partial, blk, param_names))
end
def add_match(regexp, matching_indicies = [0], splitting_indicies = nil)
add(Regex.new(@router, self, regexp, matching_indicies, splitting_indicies))
end
def add_spanning_match(regexp, matching_indicies = [0], splitting_indicies = nil)
add(SpanningRegex.new(@router, self, regexp, matching_indicies, splitting_indicies))
end
def add_free_match(regexp)
add(FreeRegex.new(@router, self, regexp))
end
def add_destination(route, path, param_names = [])
add(Path.new(@router, self, route, path, param_names))
end
def add_lookup(part)
add(Lookup.new(@router, self)).add(part)
end
def usable?(other)
false
end
private
def inject_root_methods(code = nil, &blk)
code ? root.methods_module.module_eval(code) : root.methods_module.module_eval(&blk)
end
def inject_root_ivar(obj)
root.inject_root_ivar(obj)
end
def add(matcher)
@matchers << matcher unless matcher.usable?(@matchers.last)
@matchers.last
end
def to_code
@matchers.map{ |m| "# #{m.class}\n" << m.to_code }.join("\n") << "\n"
end
def root
@router.root
end
def depth
d, p = 0, @parent
d, p = d + 1, p.parent until p.nil?
d
end
def use_named_captures?
//.respond_to?(:names)
end
end
end