From 175e92c9ac674536ad6c54937b54ef2e77217f08 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Sat, 8 Aug 2015 16:12:00 -0700
Subject: eliminate runtime conditional

We know in advance whether the object is a dispatcher or not, so we can
configure the Constraints object with a strategy that will call the
right method.
---
 actionpack/lib/action_dispatch/routing/mapper.rb | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

(limited to 'actionpack')

diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 226f6b56d1..7513c62128 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -16,7 +16,10 @@ module ActionDispatch
       class Constraints < Endpoint #:nodoc:
         attr_reader :app, :constraints
 
-        def initialize(app, constraints, dispatcher_p)
+        SERVE = ->(app, req) { app.serve req }
+        CALL  = ->(app, req) { app.call req.env }
+
+        def initialize(app, constraints, strategy)
           # Unwrap Constraints objects.  I don't actually think it's possible
           # to pass a Constraints object to this constructor, but there were
           # multiple places that kept testing children of this object.  I
@@ -26,12 +29,12 @@ module ActionDispatch
             app = app.app
           end
 
-          @dispatcher = dispatcher_p
+          @strategy = strategy
 
           @app, @constraints, = app, constraints
         end
 
-        def dispatcher?; @dispatcher; end
+        def dispatcher?; @strategy == SERVE; end
 
         def matches?(req)
           @constraints.all? do |constraint|
@@ -43,11 +46,7 @@ module ActionDispatch
         def serve(req)
           return [ 404, {'X-Cascade' => 'pass'}, [] ] unless matches?(req)
 
-          if dispatcher?
-            @app.serve req
-          else
-            @app.call req.env
-          end
+          @strategy.call @app, req
         end
 
         private
@@ -241,9 +240,9 @@ module ActionDispatch
 
           def app(blocks)
             if to.respond_to?(:call)
-              Constraints.new(to, blocks, false)
+              Constraints.new(to, blocks, Constraints::CALL)
             elsif blocks.any?
-              Constraints.new(dispatcher(defaults), blocks, true)
+              Constraints.new(dispatcher(defaults), blocks, Constraints::SERVE)
             else
               dispatcher(defaults)
             end
-- 
cgit v1.2.3