Class: Wayfarer::Routing::PathFinder Private

Inherits:
Object
  • Object
show all
Includes:
KV
Defined in:
lib/wayfarer/routing/path_finder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Encapsulates all state needed to route a URL.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KV

#[], #[]=

Constructor Details

#initialize(task, path_consumer: initial_path_consumer(task[:uri]), params_stack: Wayfarer::Routing::HashStack.empty, stop_when_found: true, &callback) ⇒ PathFinder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PathFinder.

Parameters:

  • task (Wayfarer::Task)
  • path_consumer (PathConsumer) (defaults to: initial_path_consumer(task[:uri]))

    internal use only

  • params_stack (HashStack) (defaults to: Wayfarer::Routing::HashStack.empty)

    internal use only

  • stop_when_found (Boolean) (defaults to: true)

    whether traversal halts on match



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wayfarer/routing/path_finder.rb', line 68

def initialize(
  task,
  path_consumer: initial_path_consumer(task[:uri]),
  params_stack: Wayfarer::Routing::HashStack.empty,
  stop_when_found: true,
  &callback
)
  @task = task
  @uri = task[:uri]
  @current_path = []
  @actions = []
  @path_consumer = path_consumer
  @params_stack = params_stack
  @kv = kv
  @callback = callback
  @stop_when_found = stop_when_found
  @match_history = []
end

Instance Attribute Details

#actionObject?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Object, nil)


56
57
58
# File 'lib/wayfarer/routing/path_finder.rb', line 56

def action
  @action
end

#current_pathArray<Wayfarer::Routing::Route> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



53
54
55
# File 'lib/wayfarer/routing/path_finder.rb', line 53

def current_path
  @current_path
end

#found_pathArray<Wayfarer::Routing::Route>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



53
54
55
# File 'lib/wayfarer/routing/path_finder.rb', line 53

def found_path
  @found_path
end

#paramsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash)


59
60
61
# File 'lib/wayfarer/routing/path_finder.rb', line 59

def params
  @params
end

#params_stackWayfarer::Routing::HashStack (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
# File 'lib/wayfarer/routing/path_finder.rb', line 62

def params_stack
  @params_stack
end

#pathString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


47
48
49
# File 'lib/wayfarer/routing/path_finder.rb', line 47

def path
  @path
end

#path_consumerWayfarer::Routing::PathConsumer (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
# File 'lib/wayfarer/routing/path_finder.rb', line 50

def path_consumer
  @path_consumer
end

#taskWayfarer::Task (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



41
42
43
# File 'lib/wayfarer/routing/path_finder.rb', line 41

def task
  @task
end

#uriAddressable::URI (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Addressable::URI)


44
45
46
# File 'lib/wayfarer/routing/path_finder.rb', line 44

def uri
  @uri
end

Class Method Details

.result(route, task) ⇒ Result::Match, Result::Mismatch

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The result of traversing the route with a new Wayfarer::Routing::PathFinder for url.

Parameters:

Returns:



17
18
19
# File 'lib/wayfarer/routing/path_finder.rb', line 17

def self.result(route, task, &)
  accept_finder(route, new(task, &))
end

.sub_result(route, path_finder) ⇒ Result::Match, Result::Mismatch

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The result of traversing the route with an existing Wayfarer::Routing::PathFinder.

Parameters:

Returns:



26
27
28
# File 'lib/wayfarer/routing/path_finder.rb', line 26

def self.sub_result(route, path_finder)
  accept_finder(route, path_finder)
end

Instance Method Details

#current_routeWayfarer::Routing::Route?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



95
96
97
# File 'lib/wayfarer/routing/path_finder.rb', line 95

def current_route
  current_path.last
end

#enter(route) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Enters a route node, updating state.

Parameters:



103
104
105
106
107
108
109
110
111
# File 'lib/wayfarer/routing/path_finder.rb', line 103

def enter(route)
  return if stopped?

  current_path.push(route)
  path_consumer.push(route)
  params_stack.push(route.params(self))
  actions.prepend(route.action(self))
  match_history.push(match(route))
end

#found?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether a route has matched and consumed the URL.

Returns:

  • (Boolean)


90
91
92
# File 'lib/wayfarer/routing/path_finder.rb', line 90

def found?
  !!found_path
end

#leavevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Leaves the current route, restoring previous state.



116
117
118
119
120
121
122
123
124
# File 'lib/wayfarer/routing/path_finder.rb', line 116

def leave
  return if found? && stop_when_found?

  actions.pop
  params_stack.pop
  path_consumer.pop
  current_path.pop
  match_history.pop
end

#stopped?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether traversal should stop.

Returns:

  • (Boolean)


142
143
144
# File 'lib/wayfarer/routing/path_finder.rb', line 142

def stopped?
  found? && stop_when_found?
end

#visit(route) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visits a Route.

Parameters:

Returns:

  • (Boolean)

    whether to continue traversal



130
131
132
133
134
135
136
137
# File 'lib/wayfarer/routing/path_finder.rb', line 130

def visit(route)
  return false if stopped? || !match_history.last
  return true unless route.leaf? && path_consumer.valid?

  found! if !found? && match_history.all?

  !stop_when_found?
end