[Nitro] "magic" mapping of controllers possible?

Dimitri Aivaliotis aglarond at gmail.com
Thu Jun 22 16:47:40 EDT 2006


Here's my version, modified (correcting a couple of small errors, including
it within the main Nitro namespace, and fitting it to the way I like to lay
out my sources) from what manveru wrote:

module Nitro
  class Server

    class << self

      def try( &block )
        yield
      rescue Object
        false
      end

      def fetch_mount( point )
        if point == '/'
          return Nitro::Controller # because I like to craft my index page
by hand
        else
          base = point.gsub('/', '')
          # now, this assumes the sources for the controllers are in
          # files named "app/<name>_c.rb
          file, controller = "app/#{base}_c.rb", "#{base.capitalize
}Controller"
        end
        # check if file is in $:
        Logger.debug "missing #{file} for #{controller}" unless try{require
file}

        if ! Nitro.const_defined?( controller )
          Nitro.const_get(controller)
        else
          raise NameError, "missing controller #{controller} for #{point}"
        end
      end

    end

    def self.auto_map=( *arr )
      arr.flatten!
      Nitro::Server.map = Hash[*arr.map{|m| [m, fetch_mount(m)] }.flatten]
    end

  end
end

Then, your Nitro::Server.auto_map= goodness is ready to go!

- Dimitri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/nitro-general/attachments/20060622/587e8e1c/attachment.html 


More information about the Nitro-general mailing list