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.



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

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

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



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

def regexp
  @regexp
end

Instance Method Details

#call(env) ⇒ Object



123
124
125
126
127
128
# File 'lib/rack/multiplexer.rb', line 123

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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rack/multiplexer.rb', line 134

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

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rack/multiplexer.rb', line 130

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