Method: Egalite::Handler#dispatch

Defined in:
lib/egalite.rb

#dispatch(path, params, method, req, first_call = false) ⇒ Object



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/egalite.rb', line 645

def dispatch(path, params, method, req, first_call = false)
  # routing
  (controller_name, action_name, path_params, prmhash) = nil
  (controller, action) = nil
  
  route = @routes.find { |route|
    puts "Routing: matching: #{route.inspect}" if RouteDebug
    route_result = route.match(path)
    (controller_name, action_name, path_params, prmhash) = route_result
    next if route_result == nil
    puts "Routing: pre-matched: #{route_result.inspect}" if RouteDebug
    (controller, action) = get_controller(controller_name, action_name, method)
    true if controller
  }
  return display_notfound unless controller

  puts "Routing: matched: #{controller.class} #{action}" if RouteDebug
  params = prmhash.merge(params)
  
  req.route = route
  req.controller = controller_name
  req.controller_class = controller
  req.action = action_name
  req.action_method = action
  req.inner_path = path
  req.path_params = path_params
  req.path_info = path_params.join('/')

  res = run_controller(controller, action, req)
  
  if first_call
    controller.after_filter(res.to_a)
    
    # access log
    t = Time.now - req.time
    log = [req.time.iso8601, req.ipaddr, t, req.url, req.referrer]
    log += controller.log_values.to_a
    line = log.map {|s| s.to_s.gsub(/\t/,'')}.join("\t").gsub(/\n/,'')
    AccessLogger.write(line)
  end
  res
end