class Padrino::PathRouter::Compiler
High performance engine for finding all routes which are matched with pattern
Attributes
routes[RW]
Public Class Methods
new(routes)
click to toggle source
Constructs an instance of Padrino::PathRouter::Compiler
# File lib/padrino-core/path_router/compiler.rb, line 12 def initialize(routes) @routes = routes end
Public Instance Methods
call_by_request(request) { |route, params| ... }
click to toggle source
Calls routes by using request.
# File lib/padrino-core/path_router/compiler.rb, line 48 def call_by_request(request) rotation do |offset| pattern = decode_pattern(request.path_info) if route = match?(offset, pattern) params = route.params_for(pattern, request.params) yield(route, params) if route.verb == request.request_method route end end end
compile!()
click to toggle source
Compiles all routes into regexps.
# File lib/padrino-core/path_router/compiler.rb, line 19 def compile! return if compiled? @routes.each_with_index do |route, index| route.index = index route.regexp = /(?<_#{index}>#{route.matcher.to_regexp})/ end @compiled = true end
compiled?()
click to toggle source
Returns true if all routes has been compiled.
# File lib/padrino-core/path_router/compiler.rb, line 31 def compiled? !!@compiled end
find_by(request_or_env)
click to toggle source
Finds routes by using request or env.
# File lib/padrino-core/path_router/compiler.rb, line 38 def find_by(request_or_env) request = request_or_env.is_a?(Hash) ? Sinatra::Request.new(request_or_env) : request_or_env pattern = decode_pattern(request.path_info) verb = request.request_method rotation { |offset| match?(offset, pattern) }.select { |route| route.verb == verb } end
find_by_pattern(pattern)
click to toggle source
Finds routes by using PATH_INFO.
# File lib/padrino-core/path_router/compiler.rb, line 62 def find_by_pattern(pattern) pattern = decode_pattern(pattern) rotation { |offset| match?(offset, pattern) } end
Private Instance Methods
decode_pattern(pattern)
click to toggle source
Decode env
# File lib/padrino-core/path_router/compiler.rb, line 93 def decode_pattern(pattern) decode_uri(encode_default_external(pattern)) end
decode_uri(string)
click to toggle source
Decode uri escape sequences
# File lib/padrino-core/path_router/compiler.rb, line 107 def decode_uri(string) string.split(/%2F|%2f/, -1).map { |part| Rack::Utils.unescape(part) }.join('%2F') end
encode_default_external(string)
click to toggle source
Encode string with Encoding.default_external
# File lib/padrino-core/path_router/compiler.rb, line 100 def encode_default_external(string) string.encode(Encoding.default_external) end
match?(offset, path)
click to toggle source
Returns a instance of PathRouter::Route
if path is matched with current regexp
# File lib/padrino-core/path_router/compiler.rb, line 72 def match?(offset, path) @routes[offset..-1].find do |route| route.regexp === path || (path.end_with?("/") && route.regexp === path[0..-2]) end end
rotation(offset = 0) { |offset| ... }
click to toggle source
Runs through all regexps to find routes.
# File lib/padrino-core/path_router/compiler.rb, line 81 def rotation(offset = 0) compile! unless compiled? loop.with_object([]) do |_, candidacies| return candidacies unless route = yield(offset) candidacies << route offset = route.index.next end end