Class: Pancake::Router
- Inherits:
-
Usher::Interface::RackInterface
- Object
- Usher::Interface::RackInterface
- Pancake::Router
- Defined in:
- lib/pancake/router.rb
Overview
The pancake router is a customized version of the Usher router. Usher is a fast tree based router that can generate routes, have nested routers, and even generate from nested routers.
Direct Known Subclasses
Defined Under Namespace
Classes: MountedApplication, RackApplicationExpected
Constant Summary collapse
- CONFIGURATION_KEY =
"pancake.request.configuration".freeze
- ROUTE_KEY =
"pancake.request.matching_route".freeze
Instance Attribute Summary collapse
- #configuration ⇒ Object
- #router ⇒ Object writeonly
Instance Method Summary collapse
-
#add(path, opts = {}, &block) ⇒ Object
Adds a route to the router.
- #base_url(opts = {}) ⇒ Object
- #call(env) ⇒ Object
-
#mount(mounted_app, path, options = {}) ⇒ Object
Mounts an application in the router as a sub application in the url space.
- #mount_applications! ⇒ Object
-
#url(name_or_path, options = {}) ⇒ Object
Generate a url.
Instance Attribute Details
#configuration ⇒ Object
47 48 49 |
# File 'lib/pancake/router.rb', line 47 def configuration @configuration end |
#router=(value) ⇒ Object (writeonly)
41 42 43 |
# File 'lib/pancake/router.rb', line 41 def router=(value) @router = value end |
Instance Method Details
#add(path, opts = {}, &block) ⇒ Object
Adds a route to the router.
117 118 119 120 121 122 123 124 |
# File 'lib/pancake/router.rb', line 117 def add(path, opts = {}, &block) opts = (opts) route = super(path, opts) if block_given? route.to(block) end route end |
#base_url(opts = {}) ⇒ Object
137 138 139 |
# File 'lib/pancake/router.rb', line 137 def base_url(opts = {}) router.generator.generate_base_url(opts) end |
#call(env) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/pancake/router.rb', line 141 def call(env) orig_config = env[CONFIGURATION_KEY] orig_route = env[ROUTE_KEY] env[CONFIGURATION_KEY] = configuration super(env) ensure env[CONFIGURATION_KEY] = orig_config env[ROUTE_KEY] = orig_route end |
#mount(mounted_app, path, options = {}) ⇒ Object
Mounts an application in the router as a sub application in the url space. This will route directly to the sub application and skip any middlewares etc defined on a stack
95 96 97 98 99 |
# File 'lib/pancake/router.rb', line 95 def mount(mounted_app, path, = {}) mounted_app = MountedApplication.new(mounted_app, path, ) self.class.mounted_applications << mounted_app mounted_app end |
#mount_applications! ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pancake/router.rb', line 101 def mount_applications! # need to set them as mounted here before we actually to mount them. # if we just mounted them, the inheritance of the routes would mean that only the first would be mounted on this class # and the rest would be mounted on the child class apps = self.class.mounted_applications.select do |a| a.mounted? ? false : (a.mounted = true) end apps.each do |app| route = add(app.mount_path, app.) app.mount!(route) end end |
#url(name_or_path, options = {}) ⇒ Object
Generate a url
127 128 129 130 131 132 133 134 135 |
# File 'lib/pancake/router.rb', line 127 def url(name_or_path, = {}) if Hash === name_or_path name = nil = name_or_path else name = name_or_path end generate(name, ) end |