class Routing
Public Class Methods
new(request)
click to toggle source
Set request object as an attribute. Declare a routes attribute. Call the draw method to populate it with a hash of routes.
# File lib/jails/routing.rb, line 4 def initialize(request) @request = request @routes = {} draw(Application.routes) end
Public Instance Methods
dispatch()
click to toggle source
Match request path to @routes hash. If match, create controller object and call the action.
# File lib/jails/routing.rb, line 21 def dispatch path = @request.path id = nil segments = path.split("/").reject { |segment| segment.empty? } if segments[1] =~ /^\d+$/ id = segments[1] segments[1] = ":id" path = "/#{segments.join('/')}" end if @routes.key?(path) target = @routes[path] resource_name, action_name = target.split('#') @request.params.merge!(resource: resource_name, action: action_name, id: id) klass = Object.const_get("#{resource_name.capitalize}Controller") puts("Processing by #{klass}##{action_name}") klass.new(@request).send(action_name) else Rack::Response.new(["Nothing found"], 404, {"Content-Type" => "text/html"}) end rescue Exception => error Rack::Response.new(["Internal error"], 500, {"Content-Type" => "text/html"}) end
draw(block)
click to toggle source
Build the @routes hash by evaluating the block passed in as a proc object from the Application.routes method.
# File lib/jails/routing.rb, line 11 def draw(block) instance_eval(&block) end
match(path, target)
click to toggle source
Add route to @routes hash with the url's path as the key and target as the value.
# File lib/jails/routing.rb, line 16 def match(path, target) @routes["#{path}"] = target end