class Padrino::PathRouter::Router

Attributes

current_order[R]
engine[R]
routes[R]

Public Class Methods

new() click to toggle source

Constructs an instance of PathRouter::Router.

# File lib/padrino-core/path_router.rb, line 24
def initialize
  reset!
end

Public Instance Methods

add(verb, path, options = {}, &block) click to toggle source

Adds a new route to routes.

# 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) click to toggle source

Returns all routes which are matched with the condition

# File lib/padrino-core/path_router.rb, line 41
def call(request, &block)
  prepare! unless prepared?
  @engine.call_by_request(request, &block)
end
increment_order() click to toggle source

Increments the order.

# File lib/padrino-core/path_router.rb, line 94
def increment_order
  @current_order += 1
end
path(name, *args) click to toggle source

Finds a path which is matched with conditions from arguments

# 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?
  route = candidates.sort_by! { |candidate|
    (params.keys.map(&:to_s) - candidate.matcher.names).length }.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!() click to toggle source

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

# File lib/padrino-core/path_router.rb, line 102
def prepare!
  @engine = Compiler.new(@routes)
  @prepared = true
  return if @current_order.zero?
  @routes.sort_by!(&:order)
end
recognize(request_or_env) click to toggle source

Returns all routes which are matched with the condition without block

# 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) click to toggle source

Recognizes route and expanded params from a path.

# File lib/padrino-core/path_router.rb, line 76
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!() click to toggle source

Resets all routes, current order and preparation.

# File lib/padrino-core/path_router.rb, line 85
def reset!
  @routes = []
  @current_order = 0
  @prepared = nil
end

Private Instance Methods

prepared?() click to toggle source

Returns true if the router has been prepared.

# File lib/padrino-core/path_router.rb, line 114
def prepared?
  !!@prepared
end