Skip to content

Commit

Permalink
added dupping
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Jun 1, 2010
1 parent 2b3abc1 commit f1e3ec0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
16 changes: 16 additions & 0 deletions lib/http_router.rb
Expand Up @@ -26,11 +26,13 @@ def self.override_rack_mapper!
end

def initialize(options = nil, &block)
@options = options
@default_app = options && options[:default_app] || proc{|env| ::Rack::Response.new("Not Found", 404).finish }
@ignore_trailing_slash = options && options.key?(:ignore_trailing_slash) ? options[:ignore_trailing_slash] : true
@redirect_trailing_slash = options && options.key?(:redirect_trailing_slash) ? options[:redirect_trailing_slash] : false
@routes = []
@named_routes = {}
@init_block = block
reset!
instance_eval(&block) if block
end
Expand Down Expand Up @@ -145,6 +147,20 @@ def glob(*args)
Glob.new(self, *args)
end

def dup
dup_router = HttpRouter.new(@options, &@init_block)
@routes.each do |r|
new_route = r.dup
new_route.router = dup_router
dup_router.add_route new_route
end
dup_router
end

def add_route(route)
route.compile
end

private

def consume_path!(request, response)
Expand Down
2 changes: 1 addition & 1 deletion lib/http_router/route.rb
@@ -1,7 +1,7 @@
class HttpRouter
class Route
attr_reader :dest, :paths
attr_accessor :trailing_slash_ignore, :partially_match, :default_values
attr_accessor :trailing_slash_ignore, :partially_match, :default_values, :router

def initialize(base, path)
@router = base
Expand Down
23 changes: 20 additions & 3 deletions spec/misc_spec.rb
Expand Up @@ -15,9 +15,11 @@
end

context "instance_eval block" do
#HttpRouter.new {
# add('/test').to :test
#}.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).dest.should == :test
it "run the routes" do
HttpRouter.new {
add('/test').to :test
}.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).dest.should == :test
end
end

context "exceptions" do
Expand All @@ -34,4 +36,19 @@
end

end

context "dupping" do
it "run the routes" do
r1 = HttpRouter.new {
add('/test').to :test
}

r2 = r1.dup
r2.add('/test2').to(:test2)
r1.recognize(Rack::MockRequest.env_for('/test2')).should be_nil
r2.recognize(Rack::MockRequest.env_for('/test2')).should_not be_nil
end
end


end

0 comments on commit f1e3ec0

Please sign in to comment.