New Rails Routing Algorithm Arrived

written by zsombor on June 29th, 2005 @ 08:01 PM

I’m just looking at the code and see the very definition for both refactoring and algorithmic optimization. This is how it was before and this is how it looks after Nicholas Seckar rewrote it. Of course now it’s faster but that is not entirely why I’m rambling here. New parse logic is what leaves my mouth open: using mighty eval from a configuration code like this:

map.connect ':controller/service.wsdl', :action => 'wsdl'
map.connect ':controller/:action/:id'

an optimized parse code is constructed on server startup that looks like:

def self.recognize_path(path)
  index = 0
  controller_result = ::ActionController::Routing::ControllerComponent.traverse_to_controller(path, index)
  if controller_result
    controller_value, segments_to_controller = controller_result
    controller_value = controller_value
    if ((segment1 = path[index += segments_to_controller]) == "service.wsdl")
      if ! (segment2 = path[index += 1])
        return {"controller" => controller_value, "action" => "wsdl"}
      end
    end
  end
  index = 0
  controller_result = ::ActionController::Routing::ControllerComponent.traverse_to_controller(path, index)
  if controller_result
    controller_value, segments_to_controller = controller_result
    controller_value = controller_value
    if (segment1 = path[index += segments_to_controller])
      action_value = CGI.unescape(segment1)
      if (segment2 = path[index += 1])
        id_value = CGI.unescape(segment2)
        if ! (segment3 = path[index += 1])
          return {"id" => id_value, "controller" => controller_value, "action" => action_value}
        end
      else
        return {"controller" => controller_value, "action" => action_value}
      end
    else
      return {"controller" => controller_value, "action" => "index"}
    end
  end
end

And dare to think about Ruby as just another language.

Post a comment

Options:

Size

Colors