Class: Rack::Multiplexer::Route
- Inherits:
-
Object
- Object
- Rack::Multiplexer::Route
- Defined in:
- lib/rack/multiplexer.rb
Constant Summary collapse
- PLACEHOLDER_REGEXP =
/:(\w+)/
Instance Attribute Summary collapse
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #compile(pattern) ⇒ Object
-
#initialize(pattern, application) ⇒ Route
constructor
A new instance of Route.
- #match?(path) ⇒ Boolean
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
#regexp ⇒ Object (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
122 123 124 |
# File 'lib/rack/multiplexer.rb', line 122 def match?(path) @regexp === path end |