Class: Padrino::PathRouter::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/padrino-core/path_router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Constructs an instance of PathRouter::Router.



24
25
26
# File 'lib/padrino-core/path_router.rb', line 24

def initialize
  reset!
end

Instance Attribute Details

#current_orderObject (readonly)

Returns the value of attribute current_order.



19
20
21
# File 'lib/padrino-core/path_router.rb', line 19

def current_order
  @current_order
end

#engineObject (readonly)

Returns the value of attribute engine.



19
20
21
# File 'lib/padrino-core/path_router.rb', line 19

def engine
  @engine
end

#routesObject (readonly)

Returns the value of attribute routes.



19
20
21
# File 'lib/padrino-core/path_router.rb', line 19

def routes
  @routes
end

Instance Method Details

#add(verb, path, options = {}, &block) ⇒ Object

Adds a new route to routes.



31
32
33
34
35
36
# File 'lib/padrino-core/path_router.rb', line 31

def add(verb, path, options = {}, &block)
  route = Route.new(path, verb, options, &block)
  route.router = self
  @routes << route
  route
end

#call(request, &block) ⇒ Object

Returns all routes which are matched with the condition



41
42
43
44
# File 'lib/padrino-core/path_router.rb', line 41

def call(request, &block)
  prepare! unless prepared?
  @engine.call_by_request(request, &block)
end

#increment_orderObject

Increments the order.



96
97
98
# File 'lib/padrino-core/path_router.rb', line 96

def increment_order
  @current_order += 1
end

#path(name, *args) ⇒ Object

Finds a path which is matched with conditions from arguments



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/padrino-core/path_router.rb', line 57

def path(name, *args)
  params = args.last.is_a?(Hash) ? args.pop : {}
  candidates = @routes.select { |route| route.name == name }
  fail InvalidRouteException if candidates.empty?
  i = 0
  route = candidates.sort_by! { |candidate|
    # Tries to find the route that matches more, but with fewer names, in stable order
    [(params.keys.map(&:to_s) - candidate.matcher.names).length, candidate.matcher.names.size, i += 1] }.shift
  matcher = route.matcher
  params_for_expand = params.dup
  if !args.empty? && matcher.mustermann?
    matcher.names.each_with_index do |matcher_name, index|
      params_for_expand[matcher_name.to_sym] ||= args[index]
    end
  end
  matcher.mustermann? ? matcher.expand(params_for_expand) : route.path_for_generation
end

#prepare!Object

Constructs an instance of PathRouter::Compiler, and sorts all routes by using the order.



104
105
106
107
108
109
# File 'lib/padrino-core/path_router.rb', line 104

def prepare!
  @engine = Compiler.new(@routes)
  @prepared = true
  return if @current_order.zero?
  @routes.sort_by!(&:order)
end

#recognize(request_or_env) ⇒ Object

Returns all routes which are matched with the condition without block



49
50
51
52
# File 'lib/padrino-core/path_router.rb', line 49

def recognize(request_or_env)
  prepare! unless prepared?
  @engine.find_by(request_or_env)
end

#recognize_path(path_info) ⇒ Object

Recognizes route and expanded params from a path.



78
79
80
81
82
# File 'lib/padrino-core/path_router.rb', line 78

def recognize_path(path_info)
  prepare! unless prepared?
  route = @engine.find_by_pattern(path_info).first
  [route.name, route.params_for(path_info, {})]
end

#reset!Object

Resets all routes, current order and preparation.



87
88
89
90
91
# File 'lib/padrino-core/path_router.rb', line 87

def reset!
  @routes = []
  @current_order = 0
  @prepared = nil
end