Class: Rack::Multiplexer::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/multiplexer.rb

Constant Summary collapse

PLACEHOLDER_REGEXP =
/:(\w+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, application) ⇒ Route

Returns a new instance of Route.



110
111
112
113
# File 'lib/rack/multiplexer.rb', line 110

def initialize(pattern, application)
  @application = application
  @regexp, @keys = compile(pattern)
end

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



108
109
110
# File 'lib/rack/multiplexer.rb', line 108

def regexp
  @regexp
end

Instance Method Details

#call(env) ⇒ Object



115
116
117
118
119
120
# File 'lib/rack/multiplexer.rb', line 115

def call(env)
  request = Rack::Request.new(env)
  data = @regexp.match(env["PATH_INFO"])
  (data.size - 1).times {|i| request.update_param(@keys[i], data[i + 1]) }
  @application.call(request.env)
end

#compile(pattern) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rack/multiplexer.rb', line 126

def compile(pattern)
  keys = []
  segments = []
  pattern.split("/").each do |segment|
    segments << Regexp.escape(segment).gsub(PLACEHOLDER_REGEXP, "([^#?/]+)")
    if key = Regexp.last_match(1)
      keys << key
    end
  end
  return Regexp.new(segments.any? ? segments.join(?/) : ?/), keys
end

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rack/multiplexer.rb', line 122

def match?(path)
  @regexp === path
end