Class: Wayfarer::Routing::Route
Constant Summary
collapse
- EMPTY_PARAMS =
{}.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Stringify
#to_s
Methods included from DSL
#custom, #host, #path, #query, #scheme, #to, #url
Constructor Details
#initialize(parent: nil, matcher: nil, action: nil, **options, &block) ⇒ Route
Returns a new instance of Route.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/wayfarer/routing/route.rb', line 18
def initialize(
parent: nil,
matcher: nil,
action: nil,
**options,
&block
)
raise "missing parent" unless parent || is_a?(RootRoute)
@parent = parent
@matcher = matcher
@action = action
@children = []
leaf = options.reduce(self) { |acc, (key, val)| acc.public_send(key, val) }
leaf.instance_eval(&block) if block
end
|
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
10
11
12
|
# File 'lib/wayfarer/routing/route.rb', line 10
def children
@children
end
|
#matcher ⇒ Object
Returns the value of attribute matcher.
10
11
12
|
# File 'lib/wayfarer/routing/route.rb', line 10
def matcher
@matcher
end
|
#parent ⇒ Object
Returns the value of attribute parent.
10
11
12
|
# File 'lib/wayfarer/routing/route.rb', line 10
def parent
@parent
end
|
Instance Method Details
#accept(visitor) ⇒ Object
Accepts a visitor for pre-order traversal.
53
54
55
56
57
58
59
60
61
|
# File 'lib/wayfarer/routing/route.rb', line 53
def accept(visitor)
visitor.enter(self)
return visitor.leave unless visitor.visit(self)
children.each { |child| child.accept(visitor) }
visitor.leave
end
|
#action(_path_finder) ⇒ nil, ...
71
72
73
|
# File 'lib/wayfarer/routing/route.rb', line 71
def action(_path_finder)
@action
end
|
#evaluate(path_finder) ⇒ true, ...
83
84
85
|
# File 'lib/wayfarer/routing/route.rb', line 83
def evaluate(path_finder)
matcher.evaluate(path_finder)
end
|
#leaf? ⇒ true, false
43
44
45
|
# File 'lib/wayfarer/routing/route.rb', line 43
def leaf?
children.empty?
end
|
#match(path_finder) ⇒ Result::Match, ...
77
78
79
|
# File 'lib/wayfarer/routing/route.rb', line 77
def match(path_finder)
evaluate(path_finder)
end
|
#params(path_finder) ⇒ Hash
65
66
67
|
# File 'lib/wayfarer/routing/route.rb', line 65
def params(path_finder)
matcher&.params(path_finder) || EMPTY_PARAMS
end
|
#root? ⇒ true, false
38
39
40
|
# File 'lib/wayfarer/routing/route.rb', line 38
def root?
parent.nil?
end
|
#target? ⇒ false
48
49
50
|
# File 'lib/wayfarer/routing/route.rb', line 48
def target?
false
end
|
#to_h ⇒ Object
87
88
89
90
91
|
# File 'lib/wayfarer/routing/route.rb', line 87
def to_h
return {} unless matcher
{ matcher.class.name.demodulize.underscore => matcher.to_h }
end
|