Class: Wayfarer::Routing::Route

Inherits:
Object
  • Object
show all
Includes:
DSL, Serializable, Stringify
Defined in:
lib/wayfarer/routing/route.rb

Direct Known Subclasses

RootRoute, SubRoute, TargetRoute

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

#childrenObject (readonly)

Returns the value of attribute children.



10
11
12
# File 'lib/wayfarer/routing/route.rb', line 10

def children
  @children
end

#matcherObject (readonly)

Returns the value of attribute matcher.



10
11
12
# File 'lib/wayfarer/routing/route.rb', line 10

def matcher
  @matcher
end

#parentObject (readonly)

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, ...

Parameters:

  • PathFinder (_path_finder)

Returns:



71
72
73
# File 'lib/wayfarer/routing/route.rb', line 71

def action(_path_finder)
  @action
end

#evaluate(path_finder) ⇒ true, ...

Parameters:

  • PathFinder (path_finder)

Returns:



83
84
85
# File 'lib/wayfarer/routing/route.rb', line 83

def evaluate(path_finder)
  matcher.evaluate(path_finder)
end

#leaf?true, false

Returns:

  • (true, false)


43
44
45
# File 'lib/wayfarer/routing/route.rb', line 43

def leaf?
  children.empty?
end

#match(path_finder) ⇒ Result::Match, ...

Parameters:

  • PathFinder (path_finder)

Returns:



77
78
79
# File 'lib/wayfarer/routing/route.rb', line 77

def match(path_finder)
  evaluate(path_finder)
end

#params(path_finder) ⇒ Hash

Parameters:

  • PathFinder (path_finder)

Returns:

  • (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

Returns:

  • (true, false)


38
39
40
# File 'lib/wayfarer/routing/route.rb', line 38

def root?
  parent.nil?
end

#target?false

Returns:

  • (false)


48
49
50
# File 'lib/wayfarer/routing/route.rb', line 48

def target?
  false
end

#to_hObject



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