Skip to content
This repository
Browse code

route helper just a module, so much simpler

  • Loading branch information...
commit c1dcf7fd38bbd1fc4ac7d40ae7763c4203c366bf 1 parent 28b8d20
Joshua Hull authored
22 lib/http_router.rb
@@ -8,7 +8,7 @@
8 8 require 'http_router/response'
9 9 require 'http_router/route'
10 10 require 'http_router/generator'
11   -require 'http_router/route_proxy'
  11 +require 'http_router/route_helper'
12 12 require 'http_router/regex_route_generation'
13 13 require 'http_router/rack'
14 14 require 'http_router/util'
@@ -28,7 +28,8 @@ class HttpRouter
28 28 RecognizeResponse = Struct.new(:matches, :acceptable_methods)
29 29
30 30 attr_reader :root, :routes, :named_routes, :nodes
31   - attr_accessor :default_app, :url_mount, :route_class, :default_host, :default_port, :default_scheme
  31 + attr_writer :route_class
  32 + attr_accessor :default_app, :url_mount, :default_host, :default_port, :default_scheme
32 33
33 34 # Creates a new HttpRouter.
34 35 # Can be called with either <tt>HttpRouter.new(proc{|env| ... }, { .. options .. })</tt> or with the first argument omitted.
@@ -69,11 +70,10 @@ def add(*args, &app)
69 70 path = args.first
70 71 route = route_class.new
71 72 add_route route
72   - proxy = RouteProxy.new(route)
73   - proxy.path = path if path
74   - proxy.process_opts(opts) if opts
75   - path.to(app) if app
76   - proxy
  73 + route.path = path if path
  74 + route.process_opts(opts) if opts
  75 + route.to(app) if app
  76 + route
77 77 end
78 78
79 79 def add_route(route)
@@ -93,6 +93,14 @@ def add_route(route)
93 93 def extend_route(&blk)
94 94 @route_class = Class.new(Route) if @route_class == Route
95 95 @route_class.class_eval(&blk)
  96 + @extended_route_class = nil
  97 + end
  98 +
  99 + def route_class
  100 + @extended_route_class ||= begin
  101 + @route_class.send(:include, RouteHelper)
  102 + @route_class
  103 + end
96 104 end
97 105
98 106 # Adds a path that only responds to the request method +GET+.
4 lib/http_router/generator.rb
@@ -59,6 +59,10 @@ def initialize(route, paths)
59 59 end
60 60 end
61 61
  62 + def param_names
  63 + @param_names ||= @path_generators.map{|path| path.param_names}.flatten.uniq
  64 + end
  65 +
62 66 def max_param_count
63 67 @max_param_count ||= @path_generators.map{|p| p.param_names.size}.max
64 68 end
4 lib/http_router/route.rb
@@ -84,5 +84,9 @@ def name=(name)
84 84 @name = name
85 85 @router.named_routes[name] << self
86 86 end
  87 +
  88 + def param_names
  89 + @generator.param_names
  90 + end
87 91 end
88 92 end
58 lib/http_router/route_proxy.rb → lib/http_router/route_helper.rb
... ... @@ -1,41 +1,25 @@
1 1 class HttpRouter
2   - class RouteProxy
3   - attr_reader :route
4   -
5   - def initialize(route)
6   - @route = route
7   - @router = route.router
8   - end
9   -
10   - def method_missing(name, *args, &blk)
11   - if @route.respond_to?(name)
12   - @route.send(name, *args, &blk)
13   - self
14   - else
15   - super
16   - end
17   - end
18   -
  2 + module RouteHelper
19 3 def path
20 4 @route.path_for_generation
21 5 end
22 6
23 7 def path=(path)
24   - @route.original_path = path
  8 + @original_path = path
25 9 if path.respond_to?(:[]) and path[/[^\\]\*$/]
26   - @route.match_partially = true
27   - @route.path_for_generation = path[0..path.size - 2]
  10 + @match_partially = true
  11 + @path_for_generation = path[0..path.size - 2]
28 12 else
29   - @route.path_for_generation = path
  13 + @path_for_generation = path
30 14 end
31 15 end
32 16
33 17 def name(name = nil)
34 18 if name
35   - route.name = name
  19 + @name = name
36 20 self
37 21 else
38   - route.name
  22 + @name
39 23 end
40 24 end
41 25
@@ -45,48 +29,48 @@ def process_opts(opts)
45 29 opts.delete(:conditions)
46 30 end
47 31 opts.each do |k, v|
48   - if @route.respond_to?(:"#{k}=")
49   - @route.send(:"#{k}=", v)
50   - elsif @route.respond_to?(:"add_#{k}")
  32 + if respond_to?(:"#{k}=")
  33 + send(:"#{k}=", v)
  34 + elsif respond_to?(:"add_#{k}")
51 35 send(:"add_#{k}", v)
52 36 else
53   - @route.add_match_with(k => v)
  37 + add_match_with(k => v)
54 38 end
55 39 end
56 40 end
57 41
58 42 def to(dest = nil, &dest_block)
59   - @route.dest = dest || dest_block || raise("you didn't specify a destination")
60   - if @route.dest.respond_to?(:url_mount=)
61   - urlmount = UrlMount.new(@route.path_for_generation, @route.default_values || {}) # TODO url mount should accept nil here.
  43 + @dest = dest || dest_block || raise("you didn't specify a destination")
  44 + if @dest.respond_to?(:url_mount=)
  45 + urlmount = UrlMount.new(@path_for_generation, @default_values || {}) # TODO url mount should accept nil here.
62 46 urlmount.url_mount = @router.url_mount if @router.url_mount
63   - @route.dest.url_mount = urlmount
  47 + @dest.url_mount = urlmount
64 48 end
65 49 self
66 50 end
67 51
68 52 def head
69   - @route.add_request_method "HEAD"
  53 + add_request_method "HEAD"
70 54 self
71 55 end
72 56
73 57 def get
74   - @route.add_request_method "GET"
  58 + add_request_method "GET"
75 59 self
76 60 end
77 61
78 62 def post
79   - @route.add_request_method "POST"
  63 + add_request_method "POST"
80 64 self
81 65 end
82 66
83 67 def put
84   - @route.add_request_method "PUT"
  68 + add_request_method "PUT"
85 69 self
86 70 end
87 71
88 72 def delete
89   - @route.add_request_method "DELETE"
  73 + add_request_method "DELETE"
90 74 self
91 75 end
92 76
@@ -102,7 +86,7 @@ def redirect(path, status = 302)
102 86
103 87 # Sets the destination of this route to serve static files from either a directory or a single file.
104 88 def static(root)
105   - @route.match_partially = true if File.directory?(root)
  89 + @match_partially = true if File.directory?(root)
106 90 to File.directory?(root) ?
107 91 ::Rack::File.new(root) :
108 92 proc {|env|
8 test/helper.rb
@@ -3,9 +3,9 @@
3 3 require 'phocus'
4 4
5 5 class HttpRouter
6   - class RouteProxy
  6 + module RouteHelper
7 7 def default_destination
8   - @route.dest = proc {|env| ::Rack::Response.new("Routing to #{@route.object_id}").finish}
  8 + to proc {|env| ::Rack::Response.new("Routing to #{object_id}").finish}
9 9 self
10 10 end
11 11 end
@@ -50,12 +50,10 @@ def assert_route(route, request, params = nil, &blk)
50 50 when String
51 51 router.reset!
52 52 router.add(route)
53   - when HttpRouter::RouteProxy
54   - route.route
55 53 else
56 54 route
57 55 end
58   - HttpRouter::RouteProxy.new(route).default_destination if route && route.dest.nil?
  56 + route.default_destination if route && route.dest.nil?
59 57 request = Rack::MockRequest.env_for(request) if request.is_a?(String)
60 58 response = @router.call(request)
61 59 if route
8 test/test_misc.rb
@@ -26,7 +26,7 @@ def test_reseting
26 26 router = HttpRouter.new
27 27 r = router.add('/hi').to(:test)
28 28 matches, other_methods = router.recognize(Rack::MockRequest.env_for('/hi'))
29   - assert_equal r.route, matches.first.route
  29 + assert_equal r, matches.first.route
30 30 router.reset!
31 31 assert_equal nil, router.recognize(Rack::MockRequest.env_for('/hi')).first
32 32 end
@@ -65,9 +65,9 @@ def test_multi_name_gen
65 65
66 66 def test_yielding_from_recognize
67 67 r = HttpRouter.new
68   - r1 = r.add('/:name').default_destination.route
69   - r2 = r.add('/:name').default_destination.route
70   - r3 = r.add('/:name').default_destination.route
  68 + r1 = r.add('/:name').default_destination
  69 + r2 = r.add('/:name').default_destination
  70 + r3 = r.add('/:name').default_destination
71 71 matches = []
72 72 r.recognize(Rack::MockRequest.env_for('/test')) { |r| matches << r.route }
73 73 assert_equal [r1, r2, r3], matches

0 comments on commit c1dcf7f

Please sign in to comment.
Something went wrong with that request. Please try again.