route helper just a module, so much simpler

This commit is contained in:
Josh Hull 2011-10-01 22:17:14 -07:00
parent 28b8d200dc
commit c1dcf7fd38
6 changed files with 51 additions and 53 deletions

View File

@ -8,7 +8,7 @@ require 'http_router/request'
require 'http_router/response'
require 'http_router/route'
require 'http_router/generator'
require 'http_router/route_proxy'
require 'http_router/route_helper'
require 'http_router/regex_route_generation'
require 'http_router/rack'
require 'http_router/util'
@ -28,7 +28,8 @@ class HttpRouter
RecognizeResponse = Struct.new(:matches, :acceptable_methods)
attr_reader :root, :routes, :named_routes, :nodes
attr_accessor :default_app, :url_mount, :route_class, :default_host, :default_port, :default_scheme
attr_writer :route_class
attr_accessor :default_app, :url_mount, :default_host, :default_port, :default_scheme
# Creates a new HttpRouter.
# Can be called with either <tt>HttpRouter.new(proc{|env| ... }, { .. options .. })</tt> or with the first argument omitted.
@ -69,11 +70,10 @@ class HttpRouter
path = args.first
route = route_class.new
add_route route
proxy = RouteProxy.new(route)
proxy.path = path if path
proxy.process_opts(opts) if opts
path.to(app) if app
proxy
route.path = path if path
route.process_opts(opts) if opts
route.to(app) if app
route
end
def add_route(route)
@ -93,6 +93,14 @@ class HttpRouter
def extend_route(&blk)
@route_class = Class.new(Route) if @route_class == Route
@route_class.class_eval(&blk)
@extended_route_class = nil
end
def route_class
@extended_route_class ||= begin
@route_class.send(:include, RouteHelper)
@route_class
end
end
# Adds a path that only responds to the request method +GET+.

View File

@ -59,6 +59,10 @@ class HttpRouter
end
end
def param_names
@param_names ||= @path_generators.map{|path| path.param_names}.flatten.uniq
end
def max_param_count
@max_param_count ||= @path_generators.map{|p| p.param_names.size}.max
end

View File

@ -84,5 +84,9 @@ class HttpRouter
@name = name
@router.named_routes[name] << self
end
def param_names
@generator.param_names
end
end
end

View File

@ -1,41 +1,25 @@
class HttpRouter
class RouteProxy
attr_reader :route
def initialize(route)
@route = route
@router = route.router
end
def method_missing(name, *args, &blk)
if @route.respond_to?(name)
@route.send(name, *args, &blk)
self
else
super
end
end
module RouteHelper
def path
@route.path_for_generation
end
def path=(path)
@route.original_path = path
@original_path = path
if path.respond_to?(:[]) and path[/[^\\]\*$/]
@route.match_partially = true
@route.path_for_generation = path[0..path.size - 2]
@match_partially = true
@path_for_generation = path[0..path.size - 2]
else
@route.path_for_generation = path
@path_for_generation = path
end
end
def name(name = nil)
if name
route.name = name
@name = name
self
else
route.name
@name
end
end
@ -45,48 +29,48 @@ class HttpRouter
opts.delete(:conditions)
end
opts.each do |k, v|
if @route.respond_to?(:"#{k}=")
@route.send(:"#{k}=", v)
elsif @route.respond_to?(:"add_#{k}")
if respond_to?(:"#{k}=")
send(:"#{k}=", v)
elsif respond_to?(:"add_#{k}")
send(:"add_#{k}", v)
else
@route.add_match_with(k => v)
add_match_with(k => v)
end
end
end
def to(dest = nil, &dest_block)
@route.dest = dest || dest_block || raise("you didn't specify a destination")
if @route.dest.respond_to?(:url_mount=)
urlmount = UrlMount.new(@route.path_for_generation, @route.default_values || {}) # TODO url mount should accept nil here.
@dest = dest || dest_block || raise("you didn't specify a destination")
if @dest.respond_to?(:url_mount=)
urlmount = UrlMount.new(@path_for_generation, @default_values || {}) # TODO url mount should accept nil here.
urlmount.url_mount = @router.url_mount if @router.url_mount
@route.dest.url_mount = urlmount
@dest.url_mount = urlmount
end
self
end
def head
@route.add_request_method "HEAD"
add_request_method "HEAD"
self
end
def get
@route.add_request_method "GET"
add_request_method "GET"
self
end
def post
@route.add_request_method "POST"
add_request_method "POST"
self
end
def put
@route.add_request_method "PUT"
add_request_method "PUT"
self
end
def delete
@route.add_request_method "DELETE"
add_request_method "DELETE"
self
end
@ -102,7 +86,7 @@ class HttpRouter
# Sets the destination of this route to serve static files from either a directory or a single file.
def static(root)
@route.match_partially = true if File.directory?(root)
@match_partially = true if File.directory?(root)
to File.directory?(root) ?
::Rack::File.new(root) :
proc {|env|

View File

@ -3,9 +3,9 @@ require 'minitest/autorun'
require 'phocus'
class HttpRouter
class RouteProxy
module RouteHelper
def default_destination
@route.dest = proc {|env| ::Rack::Response.new("Routing to #{@route.object_id}").finish}
to proc {|env| ::Rack::Response.new("Routing to #{object_id}").finish}
self
end
end
@ -50,12 +50,10 @@ class MiniTest::Unit::TestCase
when String
router.reset!
router.add(route)
when HttpRouter::RouteProxy
route.route
else
route
end
HttpRouter::RouteProxy.new(route).default_destination if route && route.dest.nil?
route.default_destination if route && route.dest.nil?
request = Rack::MockRequest.env_for(request) if request.is_a?(String)
response = @router.call(request)
if route

View File

@ -26,7 +26,7 @@ class TestMisc < MiniTest::Unit::TestCase
router = HttpRouter.new
r = router.add('/hi').to(:test)
matches, other_methods = router.recognize(Rack::MockRequest.env_for('/hi'))
assert_equal r.route, matches.first.route
assert_equal r, matches.first.route
router.reset!
assert_equal nil, router.recognize(Rack::MockRequest.env_for('/hi')).first
end
@ -65,9 +65,9 @@ class TestMisc < MiniTest::Unit::TestCase
def test_yielding_from_recognize
r = HttpRouter.new
r1 = r.add('/:name').default_destination.route
r2 = r.add('/:name').default_destination.route
r3 = r.add('/:name').default_destination.route
r1 = r.add('/:name').default_destination
r2 = r.add('/:name').default_destination
r3 = r.add('/:name').default_destination
matches = []
r.recognize(Rack::MockRequest.env_for('/test')) { |r| matches << r.route }
assert_equal [r1, r2, r3], matches