Class: Rack::App::Router
- Inherits:
-
Object
- Object
- Rack::App::Router
- Defined in:
- lib/rack/app/router.rb
Constant Summary collapse
- NOT_FOUND_APP =
lambda do |_env| rack_response = Rack::Response.new rack_response.status = 404 rack_response.write('404 Not Found') rack_response.finish end
Instance Attribute Summary collapse
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #endpoints ⇒ Object
-
#merge_router!(router, prop = {}) ⇒ Object
rename to merge!.
- #path_to(klass, defined_path) ⇒ Object
- #register_endpoint!(endpoint) ⇒ Object
-
#reset ⇒ Object
add ! to method name.
- #show_endpoints ⇒ Object
Instance Attribute Details
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
5 6 7 |
# File 'lib/rack/app/router.rb', line 5 def tree @tree end |
Instance Method Details
#call(env) ⇒ Object
14 15 16 17 |
# File 'lib/rack/app/router.rb', line 14 def call(env) env[Rack::App::Constants::ENV::ROUTER] = self @tree.call(env) || NOT_FOUND_APP.call(env) end |
#endpoints ⇒ Object
19 20 21 |
# File 'lib/rack/app/router.rb', line 19 def endpoints @endpoints ||= [] end |
#merge_router!(router, prop = {}) ⇒ Object
rename to merge!
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rack/app/router.rb', line 37 def merge_router!(router, prop = {}) raise(ArgumentError, 'invalid router object, must implement :endpoints interface') unless router.respond_to?(:endpoints) router.endpoints.each do |endpoint| new_request_path = ::Rack::App::Utils.join(prop[:namespaces], endpoint.request_path) new_ancestors = endpoint.config.ancestor_apps + [prop[:new_ancestor]] new_endpoint = endpoint.fork(:request_path => new_request_path, :ancestors => new_ancestors) register_endpoint!(new_endpoint) end nil end |
#path_to(klass, defined_path) ⇒ Object
64 65 66 67 68 |
# File 'lib/rack/app/router.rb', line 64 def path_to(klass, defined_path) @lookup_paths[klass] || raise(self.class::Error::AppIsNotMountedInTheRouter, "#{klass} is not registered in the router") found_path = @lookup_paths[klass][defined_path] || raise(self.class::Error::MountedAppDoesNotHaveThisPath, 'missing path reference') found_path.dup end |
#register_endpoint!(endpoint) ⇒ Object
23 24 25 26 27 |
# File 'lib/rack/app/router.rb', line 23 def register_endpoint!(endpoint) endpoints.push(endpoint) compile_endpoint!(endpoint) endpoint end |
#reset ⇒ Object
add ! to method name
30 31 32 33 34 |
# File 'lib/rack/app/router.rb', line 30 def reset @lookup_paths = {} # (Hash.new) @tree = Rack::App::Router::Tree.new compile_registered_endpoints! end |
#show_endpoints ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rack/app/router.rb', line 48 def show_endpoints endpoints = self.endpoints wd0 = endpoints.map { |endpoint| endpoint.request_method.to_s.length }.max wd1 = endpoints.map { |endpoint| endpoint.request_path.to_s.length }.max wd2 = endpoints.map { |endpoint| endpoint.description.to_s.length }.max endpoints.sort_by { |endpoint| [endpoint.request_method, endpoint.request_path] }.map do |endpoint| [ endpoint.request_method.to_s.ljust(wd0), endpoint.request_path.to_s.ljust(wd1), endpoint.description.to_s.ljust(wd2) ].join(' ') end end |