Class: ActionDispatch::Routing::Mapper::Constraints

Inherits:
Endpoint show all
Defined in:
actionpack/lib/action_dispatch/routing/mapper.rb

Overview

:nodoc:

Constant Summary collapse

SERVE =
->(app, req) { app.serve req }
CALL =
->(app, req) { app.call req.env }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Endpoint

#redirect?

Constructor Details

#initialize(app, constraints, strategy) ⇒ Constraints

Returns a new instance of Constraints.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 19

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
  # *think* they were just being defensive, but I have no idea.
  if app.is_a?(self.class)
    constraints += app.constraints
    app = app.app
  end

  @strategy = strategy

  @app, @constraints, = app, constraints
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


14
15
16
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 14

def app
  @app
end

#constraintsObject (readonly)

Returns the value of attribute constraints


14
15
16
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 14

def constraints
  @constraints
end

Instance Method Details

#dispatcher?Boolean

Returns:

  • (Boolean)

34
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 34

def dispatcher?; @strategy == SERVE; end

#matches?(req) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
39
40
41
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 36

def matches?(req)
  @constraints.all? do |constraint|
    (constraint.respond_to?(:matches?) && constraint.matches?(req)) ||
      (constraint.respond_to?(:call) && constraint.call(*constraint_args(constraint, req)))
  end
end

#serve(req) ⇒ Object


43
44
45
46
47
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 43

def serve(req)
  return [ 404, {'X-Cascade' => 'pass'}, [] ] unless matches?(req)

  @strategy.call @app, req
end