lib/nitro/dispatcher.rb in nitro-0.10.0 vs lib/nitro/dispatcher.rb in nitro-0.11.0

- old
+ new

@@ -3,10 +3,11 @@ # $Id$ module N require 'nitro/controller' +require 'nitro/simple' # The Dispatcher manages a set of controllers. class Dispatcher @@ -40,11 +41,11 @@ @controllers = { :root => controllers } else @controllers = controllers || { :root => SimpleController } end - @apis = apis + @apis = apis || {} end # Process the given hash and mount the # defined controllers. # @@ -69,10 +70,14 @@ # # [+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 @@ -84,11 +89,11 @@ # The path to dispatch. # # [:context] # The dispatching context. - def dispatch(path, context) + def dispatch(path, context = nil) api = :xhtml if @apis @apis.each { |k, v| api = k if path.slice!(/#{k}\//) } end @@ -99,11 +104,11 @@ when 0 # / -> root.index base = @root klass = controller_class_for(:root, context) action = 'index' - + when 2 if klass = controller_class_for(parts[1], context) # controller/ -> controller.index base = "#{@root}/#{parts[1]}" action = 'index' @@ -119,11 +124,11 @@ base = "#{@root}/#{parts[1]}" klass = controller_class_for(parts[1], context) action = parts[2] end - content_type = @apis ? @apis[:api] : 'text/html' + content_type = @apis[:api] || 'text/html' return klass, "__#{api}__#{action}", base, content_type end alias_method :split_path, :dispatch @@ -131,14 +136,14 @@ # Also handles reloading of controllers. def controller_class_for(key, context) klass = @controllers[key] - if context[:__RELOADED__].nil? and (:full == Rendering.reload) and klass + if (:full == Rendering.reload) and context and context[:__RELOADED__].nil? and klass def_file = klass::DEF_FILE Controller.remove_subclasses load(def_file) - klass = @controllers[key] = Object.const_get(klass.name.intern) + klass = @controllers[key] = Object.const_get(klass.name.intern) context[:__RELOADED__] = true end return klass end