Class: Racket::Router
- Inherits:
-
Object
- Object
- Racket::Router
- Defined in:
- lib/racket/router.rb
Overview
Handles routing in Racket applications.
Defined Under Namespace
Classes: Route
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
-
.service(_options = {}) ⇒ Proc
Returns a service proc that can be used by the registry.
Instance Method Summary collapse
- #action_cache ⇒ Racket::Utils::Routing::ActionCache
-
#get_route(controller_class, action, params) ⇒ String
Returns a route to the specified controller/action/parameter combination.
-
#initialize(options) ⇒ Router
constructor
A new instance of Router.
-
#map(path, controller_class) ⇒ nil
Maps a controller to the specified path.
-
#render_error(status, error = nil) ⇒ Object
@todo: Allow the user to set custom handlers for different errors.
-
#route(env) ⇒ Array
Routes a request and renders it.
Constructor Details
#initialize(options) ⇒ Router
Returns a new instance of Router.
59 60 61 62 63 |
# File 'lib/racket/router.rb', line 59 def initialize() @options = OpenStruct.new() @router = HttpRouter.new @routes = {} end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
38 39 40 |
# File 'lib/racket/router.rb', line 38 def routes @routes end |
Class Method Details
.service(_options = {}) ⇒ Proc
Returns a service proc that can be used by the registry.
44 45 46 47 48 49 50 51 52 |
# File 'lib/racket/router.rb', line 44 def self.service( = {}) lambda do |reg| new( action_cache: reg.action_cache, dev_mode: reg.application_settings.mode == :dev, logger: reg.application_logger ) end end |
Instance Method Details
#action_cache ⇒ Racket::Utils::Routing::ActionCache
55 56 57 |
# File 'lib/racket/router.rb', line 55 def action_cache @options.action_cache end |
#get_route(controller_class, action, params) ⇒ String
Returns a route to the specified controller/action/parameter combination.
71 72 73 74 75 |
# File 'lib/racket/router.rb', line 71 def get_route(controller_class, action, params) raise "Cannot find controller #{controller_class}" unless @routes.key?(controller_class) params.flatten! Route.new(@routes[controller_class], action, params).to_s end |
#map(path, controller_class) ⇒ nil
Maps a controller to the specified path.
82 83 84 85 86 |
# File 'lib/racket/router.rb', line 82 def map(path, controller_class) map_controller(path.empty? ? '/' : path, controller_class) @router.add("#{path}(/*params)").to(controller_class) action_cache.add(controller_class) end |
#render_error(status, error = nil) ⇒ Object
@todo: Allow the user to set custom handlers for different errors
89 90 91 92 93 94 95 96 97 |
# File 'lib/racket/router.rb', line 89 def render_error(status, error = nil) if (error) # If running in dev mode, let Rack::ShowExceptions handle the error. raise error if @options.dev_mode # Not running in dev mode, let us handle the error ourselves. $stderr.write("#{error.class}: #{error.}\n#{error.backtrace.map { |l| "\t#{l}" }.join("\n")}\n\n") end Response.generate_error_response(status) end |
#route(env) ⇒ Array
Routes a request and renders it.
103 104 105 106 107 108 109 110 111 |
# File 'lib/racket/router.rb', line 103 def route(env) catch :response do # Catches early exits from Controller.respond. # Ensure that that a controller will respond to the request. If not, send a 404. return render_error(404) unless (target_info = target_info(env)) Racket::Utils::Routing::Dispatcher.new(env, target_info).dispatch end rescue => err render_error(500, err) end |