Class: Padrino::PathRouter::Router
- Inherits:
-
Object
- Object
- Padrino::PathRouter::Router
- Defined in:
- lib/padrino-core/path_router.rb
Instance Attribute Summary collapse
-
#current_order ⇒ Object
readonly
Returns the value of attribute current_order.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#add(verb, path, options = {}, &block) ⇒ Object
Adds a new route to routes.
-
#call(request, &block) ⇒ Object
Returns all routes which are matched with the condition.
-
#increment_order ⇒ Object
Increments the order.
-
#initialize ⇒ Router
constructor
Constructs an instance of PathRouter::Router.
-
#path(name, *args) ⇒ Object
Finds a path which is matched with conditions from arguments.
-
#prepare! ⇒ Object
Constructs an instance of PathRouter::Compiler, and sorts all routes by using the order.
-
#recognize(request_or_env) ⇒ Object
Returns all routes which are matched with the condition without block.
-
#recognize_path(path_info) ⇒ Object
Recognizes route and expanded params from a path.
-
#reset! ⇒ Object
Resets all routes, current order and preparation.
Constructor Details
#initialize ⇒ Router
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_order ⇒ Object (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 |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
19 20 21 |
# File 'lib/padrino-core/path_router.rb', line 19 def engine @engine end |
#routes ⇒ Object (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, = {}, &block) route = Route.new(path, verb, , &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_order ⇒ Object
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.dup if !args.empty? && matcher.mustermann? matcher.names.each_with_index do |matcher_name, index| [matcher_name.to_sym] ||= args[index] end end matcher.mustermann? ? matcher.() : 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 |