class Rack::App::Router

Constants

NOT_FOUND_APP

Attributes

tree[R]

Public Class Methods

new() click to toggle source
# File lib/rack/app/router.rb, line 72
def initialize
  reset
end

Public Instance Methods

call(env) click to toggle source
# 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() click to toggle source
# File lib/rack/app/router.rb, line 19
def endpoints
  @endpoints ||= []
end
merge_router!(router, prop = {}) click to toggle source

rename to merge!

# 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) click to toggle source
# 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) click to toggle source
# File lib/rack/app/router.rb, line 23
def register_endpoint!(endpoint)
  endpoints.push(endpoint)
  compile_endpoint!(endpoint)
  endpoint
end
reset() click to toggle source

add ! to method name

# 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() click to toggle source
# 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

Protected Instance Methods

add_to_lookup_paths(endpoint) click to toggle source
# File lib/rack/app/router.rb, line 87
def add_to_lookup_paths(endpoint)
  return unless endpoint.rack_app?
  def_path = endpoint.config.defined_request_path
  final_path = endpoint.request_path
  dictionary = @lookup_paths[endpoint.config.app_class] ||= {}
  dictionary[def_path] = final_path
end
compile_endpoint!(endpoint) click to toggle source
# File lib/rack/app/router.rb, line 82
def compile_endpoint!(endpoint)
  @tree.add(endpoint)
  add_to_lookup_paths(endpoint)
end
compile_registered_endpoints!() click to toggle source
# File lib/rack/app/router.rb, line 76
def compile_registered_endpoints!
  endpoints.each do |endpoint|
    compile_endpoint!(endpoint)
  end
end