Module: Padrino::Routing::ClassMethods
- Defined in:
- lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb
Overview
Class methods responsible for enhanced routing for controllers.
Instance Method Summary collapse
-
#add_filter(type, &block) ⇒ Object
Adds a filter hook to a request.
-
#after(*args, &block) ⇒ Object
Add an after filter hook.
-
#before(*args, &block) ⇒ Object
Add a before filter hook.
-
#compiled_router ⇒ Object
Compiles the routes including deferred routes.
-
#construct_filter(*args, &block) ⇒ Object
Creates a filter to process before/after the matching route.
-
#controller(*args) { ... } ⇒ Object
(also: #controllers)
Method to organize our routes in a better way.
-
#deferred_routes ⇒ Object
Returns all routes that were deferred based on their priority.
- #get(path, *args, &block) ⇒ Object
-
#parent(name, options = {}) ⇒ Object
Provides many parents with shallowing.
-
#recognize_path(path) ⇒ Symbol, Hash
Recognize a given path.
-
#reset_router! ⇒ Object
Resets the http router and all deferred routes.
-
#router ⇒ Object
(also: #urls)
Using HttpRouter, for features and configurations.
-
#url(*args) ⇒ Object
(also: #url_for)
Instance method for url generation.
Instance Method Details
#add_filter(type, &block) ⇒ Object
Adds a filter hook to a request.
363 364 365 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 363 def add_filter(type, &block) filters[type] << block end |
#after(*args, &block) ⇒ Object
Add an after filter hook
356 357 358 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 356 def after(*args, &block) add_filter :after, &(args.empty? ? block : construct_filter(*args, &block)) end |
#before(*args, &block) ⇒ Object
Add a before filter hook
347 348 349 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 347 def before(*args, &block) add_filter :before, &(args.empty? ? block : construct_filter(*args, &block)) end |
#compiled_router ⇒ Object
Compiles the routes including deferred routes.
463 464 465 466 467 468 469 470 471 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 463 def compiled_router if deferred_routes.empty? router else deferred_routes.each { |_, routes| routes.each { |(route, dest)| route.to(dest) } } @deferred_routes = nil router end end |
#construct_filter(*args, &block) ⇒ Object
Creates a filter to process before/after the matching route.
408 409 410 411 412 413 414 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 408 def construct_filter(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} except = .key?(:except) && Array(.delete(:except)) raise("You cannot use except with other options specified") if except && (!args.empty? || !.empty?) = except.last.is_a?(Hash) ? except.pop : {} if except Filter.new(!except, @_controller, , Array(except || args), &block) end |
#controller(*args) { ... } ⇒ Object Also known as: controllers
Method to organize our routes in a better way.
In a controller, before and after filters are scoped and don’t affect other controllers or the main app. In a controller, layouts are scoped and don’t affect other controllers or the main app.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 308 def controller(*args, &block) if block_given? = args. # Controller defaults @_controller, original_controller = args, @_controller @_parents, original_parent = .delete(:parent), @_parents @_provides, original_provides = .delete(:provides), @_provides @_use_format, original_use_format = .delete(:use_format), @_use_format @_cache, original_cache = .delete(:cache), @_cache @_map, original_map = .delete(:map), @_map @_conditions, original_conditions = .delete(:conditions), @_conditions @_defaults, original_defaults = , @_defaults # Application defaults @filters, original_filters = { :before => @filters[:before].dup, :after => @filters[:after].dup }, @filters @layout, original_layout = nil, @layout instance_eval(&block) # Application defaults @filters = original_filters @layout = original_layout # Controller defaults @_controller, @_parents, @_cache = original_controller, original_parent, original_cache @_defaults, @_provides, @_map = original_defaults, original_provides, original_map @_conditions, @_use_format = original_conditions, original_use_format else include(*args) if extensions.any? end end |
#deferred_routes ⇒ Object
Returns all routes that were deferred based on their priority.
474 475 476 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 474 def deferred_routes @deferred_routes ||= Hash[ROUTE_PRIORITY.values.sort.map{|p| [p, []]}] end |
#get(path, *args, &block) ⇒ Object
548 549 550 551 552 553 554 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 548 def get(path, *args, &block) # @private conditions = @conditions.dup route('GET', path, *args, &block) @conditions = conditions route('HEAD', path, *args, &block) end |
#parent(name, options = {}) ⇒ Object
Provides many parents with shallowing.
440 441 442 443 444 445 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 440 def parent(name, ={}) defaults = { :optional => false, :map => name.to_s } = defaults.merge() @_parents = Array(@_parents) unless @_parents.is_a?(Array) @_parents << Parent.new(name, ) end |
#recognize_path(path) ⇒ Symbol, Hash
Recognize a given path
508 509 510 511 512 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 508 def recognize_path(path) responses = @router.recognize(Rack::MockRequest.env_for(path)) responses = responses[0] if responses[0].is_a?(Array) [responses[0].path.route.name, responses[0].params] end |
#reset_router! ⇒ Object
Resets the http router and all deferred routes.
481 482 483 484 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 481 def reset_router! @deferred_routes = nil router.reset! end |
#router ⇒ Object Also known as: urls
Using HttpRouter, for features and configurations.
456 457 458 459 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 456 def router @router ||= HttpRouter.new block_given? ? yield(@router) : @router end |
#url(*args) ⇒ Object Also known as: url_for
Instance method for url generation.
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 524 def url(*args) params = args. # parameters is hash at end names, params_array = args.partition{|a| a.is_a?(Symbol)} name = names.join("_").to_sym # route name is concatenated with underscores if params.is_a?(Hash) params[:format] = params[:format].to_s unless params[:format].nil? params = value_to_param(params) end url = if params_array.empty? compiled_router.path(name, params) else compiled_router.path(name, *(params_array << params)) end url[0,0] = conform_uri(uri_root) if defined?(uri_root) url[0,0] = conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI'] url = "/" if url.blank? url rescue HttpRouter::InvalidRouteException route_error = "route mapping for url(#{name.inspect}) could not be found!" raise Padrino::Routing::UnrecognizedException.new(route_error) end |