Class: ActionController::Routing::RouteSet

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/routing.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouteSet

Returns a new instance of RouteSet.



390
391
392
393
# File 'lib/action_controller/routing.rb', line 390

def initialize
  @routes = []
  @generation_methods = Hash.new(:generate_default_path)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



597
598
599
# File 'lib/action_controller/routing.rb', line 597

def method_missing(name, *args)
  (1..2).include?(args.length) ? named_route(name, *args) : super(name, *args)
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



389
390
391
# File 'lib/action_controller/routing.rb', line 389

def categories
  @categories
end

#controller_to_selectorObject (readonly)

Returns the value of attribute controller_to_selector.



389
390
391
# File 'lib/action_controller/routing.rb', line 389

def controller_to_selector
  @controller_to_selector
end

#routesObject (readonly)

Returns the value of attribute routes.



389
390
391
# File 'lib/action_controller/routing.rb', line 389

def routes
  @routes
end

Instance Method Details

#categorize_routesObject



527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/action_controller/routing.rb', line 527

def categorize_routes
  @categorized_routes = by_controller = Hash.new(self)

  known_controllers.each do |name|
    set = by_controller[name] = []
    each do |route|
      set << route if route.matches_controller? name
    end
  end
    
  @categorized_routes
end

#connect(*args) ⇒ Object



562
563
564
565
566
# File 'lib/action_controller/routing.rb', line 562

def connect(*args)
  new_route = Route.new(*args)
  @routes << new_route
  return new_route
end

#drawObject



568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/action_controller/routing.rb', line 568

def draw
  old_routes = @routes
  @routes = []
  
  begin yield self
  rescue
    @routes = old_routes
    raise
  end
  write_generation
  write_recognition
end

#each(&block) ⇒ Object



583
# File 'lib/action_controller/routing.rb', line 583

def each(&block) @routes.each(&block) end

#empty?Boolean

Returns:

  • (Boolean)


581
# File 'lib/action_controller/routing.rb', line 581

def empty?() @routes.empty? end

#extra_keys(options, recall = {}) ⇒ Object



601
602
603
# File 'lib/action_controller/routing.rb', line 601

def extra_keys(options, recall = {})
  generate(options.dup, recall).last
end

#generate(options, request_or_recall_hash = {}) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/action_controller/routing.rb', line 395

def generate(options, request_or_recall_hash = {})
  recall = request_or_recall_hash.is_a?(Hash) ? request_or_recall_hash : request_or_recall_hash.symbolized_path_parameters
  use_recall = true
  
  controller = options[:controller]
  options[:action] ||= 'index' if controller
  recall_controller = recall[:controller]
  if (recall_controller && recall_controller.include?(?/)) || (controller && controller.include?(?/)) 
    recall = {} if controller && controller[0] == ?/
    options[:controller] = Routing.controller_relative_to(controller, recall_controller)
  end
  options = recall.dup if options.empty? # XXX move to url_rewriter?
  
  keys_to_delete = []
  Routing.treat_hash(options, keys_to_delete)
  
  merged = recall.merge(options)
  keys_to_delete.each {|key| merged.delete key}
  expire_on = Routing.expiry_hash(options, recall)
    
  generate_path(merged, options, expire_on)
end

#generate_default_path(*args) ⇒ Object



421
422
423
424
# File 'lib/action_controller/routing.rb', line 421

def generate_default_path(*args)
  write_generation
  generate_default_path(*args)
end

#generate_path(merged, options, expire_on) ⇒ Object



418
419
420
# File 'lib/action_controller/routing.rb', line 418

def generate_path(merged, options, expire_on)
  send @generation_methods[merged[:controller]], merged, options, expire_on
end

#generation_code_for(ivar = 'routes', method_name = nil) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/action_controller/routing.rb', line 487

def generation_code_for(ivar = 'routes', method_name = nil)
  routes = instance_variable_get('@' + ivar)
  key_ivar = "@keys_for_#{ivar}"
  instance_variable_set(key_ivar, routes.collect {|route| route.keys})
    
  g = generator = CodeGeneration::GenerationGenerator.new
  g.def "self.#{method_name}(merged, options, expire_on)" do
    g << 'unused_count = options.length + 1'
    g << "unused_keys = keys = options.keys"
    g << 'path = nil'

    routes.each_with_index do |route, index|
      g << "new_unused_keys = keys - #{key_ivar}[#{index}]"
      g << 'new_path = ('
      g.source.indent do
        if index.zero?
          g << "new_unused_count = new_unused_keys.length"
          g << "hash = merged; not_expired = true"
          route.write_generation(g.dup)
        else
          g.if "(new_unused_count = new_unused_keys.length) < unused_count" do |gp|
            gp << "hash = merged; not_expired = true"
            route.write_generation(gp)
          end
        end
      end
      g.source.lines.last << ' )' # Add the closing brace to the end line
      g.if 'new_path' do
        g << 'return new_path, [] if new_unused_count.zero?'
        g << 'path = new_path; unused_keys = new_unused_keys; unused_count = new_unused_count'
      end
    end
  
    g << "raise RoutingError, \"No url can be generated for the hash \#{options.inspect}\" unless path"
    g << "return path, unused_keys"
  end
  
  return g
end

#known_controllersObject



540
541
542
543
544
545
546
547
548
549
550
# File 'lib/action_controller/routing.rb', line 540

def known_controllers
  @routes.inject([]) do |known, route|
    if (controller = route.known[:controller])
      if controller.is_a?(Regexp)
        known << controller.source.scan(%r{[\w\d/]+}).select {|word| controller =~ word} 
      else known << controller
      end
    end
    known
  end.uniq
end

#named_route(name, path, hash = {}) ⇒ Object

Defines a new named route with the provided name and arguments. This method need only be used when you wish to use a name that a RouteSet instance method exists for, such as categories.

For example, map.categories ‘/categories’, :controller => ‘categories’ will not work due to RouteSet#categories.



591
592
593
594
595
# File 'lib/action_controller/routing.rb', line 591

def named_route(name, path, hash = {})
  route = connect(path, hash)
  NamedRoutes.name_route(route, name)
  route
end

#recognition_failed(request) ⇒ Object

Raises:

  • (ActionController::RoutingError)


468
469
470
# File 'lib/action_controller/routing.rb', line 468

def recognition_failed(request)
  raise ActionController::RoutingError, "Recognition failed for #{request.path.inspect}"
end

#recognize(request) ⇒ Object Also known as: recognize!



452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/action_controller/routing.rb', line 452

def recognize(request)
  string_path = request.path  
  string_path.chomp! if string_path[0] == ?/  
  path = string_path.split '/'  
  path.shift  
   
  hash = recognize_path(path)  
  return recognition_failed(request) unless hash && hash['controller']  
   
  controller = hash['controller']  
  hash['controller'] = controller.controller_path  
  request.path_parameters = hash  
  controller.new 
end

#reloadObject



552
553
554
555
556
557
558
559
560
# File 'lib/action_controller/routing.rb', line 552

def reload
  NamedRoutes.clear
  
  if defined?(RAILS_ROOT) then load(File.join(RAILS_ROOT, 'config', 'routes.rb'))
  else connect(':controller/:action/:id', :action => 'index', :id => nil)
  end

  NamedRoutes.install
end

#write_generationObject



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/action_controller/routing.rb', line 426

def write_generation
  method_sources = []
  @generation_methods = Hash.new(:generate_default_path)
  categorize_routes.each do |controller, routes|
    next unless routes.length < @routes.length

    ivar = controller.gsub('/', '__')
    method_name = "generate_path_for_#{ivar}".to_sym
    instance_variable_set "@#{ivar}", routes
    code = generation_code_for(ivar, method_name).to_s
    method_sources << code
    
    filename = "generated_code/routing/generation_for_controller_#{controller}.rb"
    eval(code, nil, filename)

    @generation_methods[controller.to_s]   = method_name
    @generation_methods[controller.to_sym] = method_name
  end
  
  
  code = generation_code_for('routes', 'generate_default_path').to_s
  eval(code, nil, 'generated_code/routing/generation.rb')
  
  return (method_sources << code)
end

#write_recognitionObject



472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/action_controller/routing.rb', line 472

def write_recognition
  g = generator = CodeGeneration::RecognitionGenerator.new
  g.finish_statement = Proc.new {|hash_expr| "return #{hash_expr}"}
    
  g.def "self.recognize_path(path)" do
    each do |route|
      g << 'index = 0'
      route.write_recognition(g)
    end
  end
    
  eval g.to_s, nil, 'generated/routing/recognition.rb'
  return g.to_s
end