Parent

Mongrel::URIClassifier

Attributes

handler_map[R]

Public Class Methods

new() click to toggle source
    # File lib/mongrel/uri_classifier.rb, line 17
17:     def initialize
18:       @handler_map = {}
19:       @matcher = //
20:       @root_handler = nil
21:     end

Public Instance Methods

register(uri, handler) click to toggle source

Register a handler object at a particular URI. The handler can be whatever you want, including an array. It’s up to you what to do with it.

Registering a handler is not necessarily threadsafe, so be careful if you go mucking around once the server is running.

    # File lib/mongrel/uri_classifier.rb, line 28
28:     def register(uri, handler)
29:       raise RegistrationError, "#{uri.inspect} is already registered" if @handler_map[uri]
30:       raise RegistrationError, "URI is empty" if !uri or uri.empty?
31:       raise RegistrationError, "URI must begin with a \"#{Const::SLASH}\"" unless uri[0..0] == Const::SLASH
32:       @handler_map[uri.dup] = handler
33:       rebuild
34:     end
resolve(request_uri) click to toggle source

Resolve a request URI by finding the best partial match in the registered handler URIs.

    # File lib/mongrel/uri_classifier.rb, line 46
46:     def resolve(request_uri)
47:       if @root_handler
48:         # Optimization for the pathological case of only one handler on "/"; e.g. Rails
49:         [Const::SLASH, request_uri, @root_handler]
50:       elsif match = @matcher.match(request_uri)
51:         uri = match.to_s
52:         # A root mounted ("/") handler must resolve such that path info matches the original URI.
53:         [uri, (uri == Const::SLASH ? request_uri : match.post_match), @handler_map[uri]]
54:       else
55:         [nil, nil, nil]
56:       end
57:     end
unregister(uri) click to toggle source

Unregister a particular URI and its handler.

    # File lib/mongrel/uri_classifier.rb, line 37
37:     def unregister(uri)
38:       handler = @handler_map.delete(uri)
39:       raise RegistrationError, "#{uri.inspect} was not registered" unless handler
40:       rebuild
41:       handler
42:     end
uris() click to toggle source

Returns the URIs that have been registered with this classifier so far.

    # File lib/mongrel/uri_classifier.rb, line 13
13:     def uris
14:       @handler_map.keys
15:     end

Private Instance Methods

rebuild() click to toggle source
    # File lib/mongrel/uri_classifier.rb, line 61
61:     def rebuild
62:       if @handler_map.size == 1 and @handler_map[Const::SLASH]
63:         @root_handler = @handler_map.values.first
64:       else
65:         @root_handler = nil
66:         routes = @handler_map.keys.sort.sort_by do |uri|
67:           -uri.length
68:         end
69:         @matcher = Regexp.new(routes.map do |uri|
70:           Regexp.new('^' + Regexp.escape(uri))
71:         end.join('|'))
72:       end
73:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.