[Nitro] "magic" mapping of controllers possible?

Michael Fellinger manveru at weez-int.com
Thu Jun 22 21:32:36 EDT 2006


On Friday 23 June 2006 05:47, Dimitri Aivaliotis wrote:
> 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:

Thanks, that looks much better (saner :) maybe i should have added that i 
didn't actually try the code i suggested...

>
> 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 )

one thing still disturbing me is
if ! Nitro.const_defined?( controller )
could we make it into a pretty
unless Nitro.const_defined? controller
it just seems !good to me to use ! unless it's needed
(task for today: discover the wordplay in the last sentence)

>           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

and also, auto_map= implies that one can retrieve the map later by calling 
Nitro::Server.auto_map, which is not the case, but should be easy to provide 
an alias to Nitro::Server.map - right? :)

>
>   end
> end
>
> Then, your Nitro::Server.auto_map= goodness is ready to go!
>
> - Dimitri

I think we should decide on a (very simple) default nitro-dir-structure and 
integrate that piece of code to make life easier...
Maybe adding some Global variables for the default-locations?

also, how about this snippet, since we are just at improving run.rb...

module Kernel
  def aquire *dirs
    dirs.each do |dir|
      Dir["#{dir}/*.rb"].each do |file|
        require file
      end
    end
  end
end

aquire :helpers, :controllers, :models


More information about the Nitro-general mailing list