From 93e6d0c33a39f9e112b5d1f74de01bcc419a587c Mon Sep 17 00:00:00 2001 From: maraby Date: Sun, 6 Jan 2008 17:46:55 -0500 Subject: [PATCH] Finished initial releasable version of MRouter, the Rack Merb Router. Changed internals around extensively to load appropriately, set up the helper to allow apps to define routes within themselves. Wrote a quick test app and rackup config file to test functionality. --- mrouter/mrouter.rb | 29 ++++++++++++++++++----------- mrouter/rackapp.rb | 4 ++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/mrouter/mrouter.rb b/mrouter/mrouter.rb index 54d139e..9065075 100644 --- a/mrouter/mrouter.rb +++ b/mrouter/mrouter.rb @@ -62,6 +62,7 @@ module Rack def call(env) env['merb.router'] = self + env['merb.route'] = self.class.route(env) @app.call(env) end @@ -95,7 +96,6 @@ module Rack # make sure a route is returned even if no match is found if route[0].nil? #return default route - env['halcyon.logger'].debug "No route found. Using default." @@default_route else # params (including action and module if set) for the matching route @@ -110,20 +110,27 @@ end # to setup routes and to actually route the request. module MRouterHelper - # Yields the router which allows routes to be set up. - def self.route - if block_given? - @env['merb.router'].prepare do |router| - @env['merb.router'].default_to yield(router) + # Matches the request information in +env+ to the appropriate route. + def route + Rack::MRouter.route(@env) + end + + module ClassMethods + # Yields the router which allows routes to be set up. + def route + if block_given? + Rack::MRouter.prepare do |router| + Rack::MRouter.default_to yield(router) + end + else + abort "Halcyon::Server::Base.route expects a block to define routes." end - else - abort "Halcyon::Server::Base.route expects a block to define routes." end end - # Matches the request information in +env+ to the appropriate route. - def route - @env['merb.router'].route(@env) + # Hook to include the class methods into the receiver. + def self.included(receiver) + receiver.extend(ClassMethods) end end diff --git a/mrouter/rackapp.rb b/mrouter/rackapp.rb index 9f906f4..d12a657 100755 --- a/mrouter/rackapp.rb +++ b/mrouter/rackapp.rb @@ -12,8 +12,8 @@ class Rap end def call(env) - [200, {}, env['merb.route'].to_s] + [200, {'Content-Type'=>'text/plain'}, env['merb.route'].inspect] end end -Rack::Handler::Mongrel.run Rap.new, :Port => 4400 +Rack::Handler::Mongrel.run Rack::MRouter.new(Rap.new), :Port => 4400 if $0 == __FILE__ -- 2.11.4.GIT