Class: Padrino::PathRouter::Router

Inherits:
Object
  • Object
show all
Defined in:
padrino-core/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 'padrino-core/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 'padrino-core/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 'padrino-core/lib/padrino-core/path_router.rb', line 19

def engine
  @engine
end

#routesObject (readonly)

Returns the value of attribute routes



19
20
21
# File 'padrino-core/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 'padrino-core/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 'padrino-core/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.



94
95
96
# File 'padrino-core/lib/padrino-core/path_router.rb', line 94

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
# File 'padrino-core/lib/padrino-core/path_router.rb', line 57

def path(name, *args)
  params = args.extract_options!
  candidates = @routes.select { |route| route.name == name }
  fail InvalidRouteException if candidates.empty?
  route = candidates.sort_by! { |route|
    (params.keys.map(&:to_s) - route.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!Object

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



102
103
104
105
106
107
# File 'padrino-core/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) ⇒ Object

Returns all routes which are matched with the condition without block



49
50
51
52
# File 'padrino-core/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.



76
77
78
79
80
# File 'padrino-core/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!Object

Resets all routes, current order and preparation.



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

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