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.



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

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

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



104
105
106
# File 'lib/rack/multiplexer.rb', line 104

def regexp
  @regexp
end

Instance Method Details

#call(env) ⇒ Object



111
112
113
114
115
116
# File 'lib/rack/multiplexer.rb', line 111

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



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rack/multiplexer.rb', line 122

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)


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

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