Class: Padrino::PathRouter::Compiler

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

Overview

High performance engine for finding all routes which are matched with pattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ Compiler

Constructs an instance of Padrino::PathRouter::Compiler



12
13
14
# File 'padrino-core/lib/padrino-core/path_router/compiler.rb', line 12

def initialize(routes)
  @routes = routes
end

Instance Attribute Details

#routesObject

Returns the value of attribute routes.



7
8
9
# File 'padrino-core/lib/padrino-core/path_router/compiler.rb', line 7

def routes
  @routes
end

Instance Method Details

#call_by_request(request) ⇒ Object

Calls routes by using request.



48
49
50
51
52
53
54
55
56
57
# File 'padrino-core/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!Object

Compiles all routes into regexps.



19
20
21
22
23
24
25
26
# File 'padrino-core/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?Boolean

Returns true if all routes has been compiled.

Returns:

  • (Boolean)


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

def compiled?
  !!@compiled
end

#find_by(request_or_env) ⇒ Object

Finds routes by using request or env.



38
39
40
41
42
43
# File 'padrino-core/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) ⇒ Object

Finds routes by using PATH_INFO.



62
63
64
65
# File 'padrino-core/lib/padrino-core/path_router/compiler.rb', line 62

def find_by_pattern(pattern)
  pattern  = decode_pattern(pattern)
  rotation { |offset| match?(offset, pattern) }
end