module Nitro # Router mixin. Typically used to generate 'nice' urls. # Nice urls apart from looking more beautiful are considered (?) # more Search Engine friendly. # # However, due to the power of Nitro's intelligent dispatching # mechanism, routing is almost never used! It is only needed # for really special urls. # # === Example # # r.add_route(%r{rewritten/url/(.*)}, :controller => IdController, :action => :register, :param => :name) # r.add_route(%r{another/zelo/(.*)/(.*)}, :controller => AdminController, :action => :kick, :params => [:name, :age]) # r.add_route(%r{cool/(.*)_(.*).html}, :controller => AdminController, :action => :long, :params => [:name, :age]) module Router # Strip the beginning of the path, used by cgi adapter. setting :strip_path, :default => nil, :doc => 'Strip the beginning of the path, used by cgi adapter' # The route table maps 'nice URLs' to real URLs that # can be handled by the Dispatcher. attr_accessor :routes # Decodes a url to a [controller, action, params] tupple. # Returns false if no decoding is possible. def decode_route(url) for rule, options in @routes if md = url.match(rule) params = nil if param_names = options[:params] || options[:param] param_names = [ param_names ] unless param_names.is_a?(Array) params = {} md.captures.each_with_index do |val, idx| params[param_names[idx].to_s] = val end end return options[:controller], options[:action], params end end return false end alias_method :route, :decode_route # Encodes a [controller, action, params] tupple into a url. # Returns false if no encoding is possible. def encode_route(controller, action, *params) if route = @routes.find { |r| (r.last[:controller] == controller) and (r.last[:action] == action) } rule, options = *route url = rule.source (params.size / 2).times do |i| val = params[i + i + 1] url.sub!(/\(.*?\)/, val.to_s) end return url end return false end # Add a route to the routing table. def add_route(rule, options) (@routes ||= []) << [rule, options] end alias_method :<<, :add_route def add_routes end end end # * George Moschovitis