lib/nitro/dispatcher.rb in nitro-0.15.0 vs lib/nitro/dispatcher.rb in nitro-0.16.0

- old
+ new

@@ -1,18 +1,19 @@ # * George Moschovitis <gm@navel.gr> # (c) 2004-2005 Navel, all rights reserved. -# $Id$ +# $Id: dispatcher.rb 23 2005-04-16 18:20:00Z gmosx $ -module N +module Nitro require 'nitro/controller' require 'nitro/routing' require 'nitro/simple' # The Dispatcher manages a set of controllers. class Dispatcher + include Router # The public root directory. The files in this # directory are published by the web server. @@ -28,38 +29,29 @@ # The controllers map. attr_accessor :controllers - # APIs map. - - attr_accessor :apis - # Create a new Dispatcher. # # Input: # # [+controllers+] # Either a hash of controller mappings or a single # controller that gets mapped to :root. - # - # [+apis+] - # A hash of apis supported by the Dispatcher. - def initialize(controllers = nil, apis = nil, routes = nil) + def initialize(controllers = nil) @public_root = 'public' @template_root = @public_root if controllers and controllers.is_a?(Class) and controllers.ancestors.include?(Controller) controllers = { :root => controllers } else controllers ||= { :root => SimpleController } end mount(controllers) - - @apis = apis || {} end # Process the given hash and mount the # defined controllers. # @@ -69,36 +61,21 @@ # A hash representing the mapping of # mount points to controllers. # # === Examples # - # dispatcher.mount { - # :root => MainController # mounts / + # dispatcher.mount( + # :root => MainController, # mounts / # 'users' => UsersController # mounts /users - # } + # ) def add_controller(controllers) (@controllers ||= {}).update(controllers) update_routes end alias_method :mount, :add_controller - # Add a new api to the dispatcher - # - # [+api+] - # API symbol - # [+data+] - # Data for this API [content_type, ..] - # - # === Example - # - # dispatcher.add_api(:xml, 'text/xml') - - def add_api(api, data) - (@apis ||= {})[api] = data - end - # Update the routes. Typically called after a new # Controller is mounted. def update_routes @routes = [] @@ -121,19 +98,12 @@ # # [:context] # The dispatching context. def dispatch(path, context = nil) - api = :xhtml - path = route(path, context) - if @apis - # OPTIMIZE: check only if lookup fails. - @apis.each { |k, v| api = k if path.slice!(/#{k}\//) } - end - parts = path.split('/') parts.shift case parts.size when 0 @@ -165,26 +135,49 @@ base = '/' klass = controller_class_for(:root, context) action = parts.join('__') end end - - content_type = @apis[:api] || 'text/html' - return klass, "__#{api}__#{action}", content_type, base + return klass, "#{action}_action", base end alias_method :split_path, :dispatch # Get the controller for the given key. # Also handles reloading of controllers. + #-- + # gmosx, FIXME: this method is a NASTY hack, anyone can + # help me fix this ? + #++ def controller_class_for(key, context) klass = @controllers[key] if (:full == Rendering.reload) and context and context[:__RELOADED__].nil? and klass +=begin + ancestors = [c = klass] + while (c = c.superclass) != Nitro::Controller + ancestors.unshift(c) + end + + for c in ancestors + actions = c.public_instance_methods.find_all { |m| m.to_s =~ /(_action$)|(_template$)/ } + actions += c.action_methods + actions.each { |m| c.send(:remove_method, m) rescue nil } + c.advices = [] + + Object.send(:remove_const, c) rescue nil + load(c::DEF_FILE) + c = Object.const_get(klass.name.intern) + end +=end +#=begin def_file = klass::DEF_FILE + # Object.send(:remove_const, klass) rescue nil + # FIXME: also reload superclasses! Controller.remove_subclasses load(def_file) +#=end klass = @controllers[key] = Object.const_get(klass.name.intern) context[:__RELOADED__] = true end return klass