Class: Wayfarer::Routing::PathMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfarer/routing/path_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, route) ⇒ PathMatcher

Returns a new instance of PathMatcher.



11
12
13
14
15
16
# File 'lib/wayfarer/routing/path_matcher.rb', line 11

def initialize(path, route)
  @path = path
  @route = route
  @peeking = false
  @matcher = Mustermann.new(path, type: "sinatra")
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



6
7
8
# File 'lib/wayfarer/routing/path_matcher.rb', line 6

def matcher
  @matcher
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/wayfarer/routing/path_matcher.rb', line 6

def path
  @path
end

#peekingObject (readonly)

Returns the value of attribute peeking.



6
7
8
# File 'lib/wayfarer/routing/path_matcher.rb', line 6

def peeking
  @peeking
end

#routeObject (readonly)

Returns the value of attribute route.



6
7
8
# File 'lib/wayfarer/routing/path_matcher.rb', line 6

def route
  @route
end

Instance Method Details

#match(url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wayfarer/routing/path_matcher.rb', line 18

def match(url)
  route.accept(self)

  # If the route's branch contains other path matchers in child routes,
  # match the beginning of the path (peeking), instead of the whole path.
  !!(if peeking
       matcher.peek(url.path)
     else
       matcher.match(url.path)
     end)
end

#params(url) ⇒ Object



30
31
32
33
34
# File 'lib/wayfarer/routing/path_matcher.rb', line 30

def params(url)
  return {} unless match(url)

  matcher.params(url.path) || {}
end

#visit(route) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/wayfarer/routing/path_matcher.rb', line 36

def visit(route)
  return true if route == self.route

  return unless route.matcher.is_a?(self.class)

  @peeking = true
  false
end