Module: Mojito::Controllers::Runtime::Path

Defined in:
lib/mojito/controllers/runtime/path.rb

Instance Method Summary collapse

Instance Method Details

#PATH(pattern, delimiter = %r{/|\z}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mojito/controllers/runtime/path.rb', line 8

def PATH(pattern, delimiter = %r{/|\z})
	consume_path = proc do |pattern|
		if match = env['PATH_INFO'].match(%r<\A/*#{pattern}(?=#{delimiter})>)
			env['SCRIPT_NAME'] = match.to_s
			env['PATH_INFO'] = match.post_match
			request.locals.update match.names.inject({}) {|hash, name| hash[name.to_sym] = CGI.unescape(match[name]) ; hash }
			request.captures.push *(match.captures.collect {|c| CGI.unescape c })
			true
		else
			false
		end
	end
	proc do
		if p = case pattern
			when String
				pattern.gsub(%r{/?:\?\w+}) {|name| "(?:/(?<#{name[2..-1]}>[^/]+?))?" }.gsub(%r{:\w+}) {|name| "(?<#{name[1..-1]}>[^/]+?)" }
			when Regexp
				pattern
			end
			instance_exec p, &consume_path
		else
			false
		end
	end
end