5 def dslify_writter(*syms)
9 self.send "#{sym}=", v if v
20 Result = Struct.new(:block, :params)
23 @app ||= Application.new
32 URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
33 PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
35 attr_reader :path, :block, :param_keys, :pattern
37 def initialize(path, &b)
41 regex = @path.to_s.gsub(PARAM) do
42 @param_keys << $1.intern
45 @pattern = /^#{regex}$/
49 return unless pattern =~ env['PATH_INFO'].squeeze('/')
50 params = param_keys.zip($~.captures.map(&:from_param)).to_hash
51 Result.new(block, params)
58 module ResponseHelpers
61 throw :halt, NotFound.new(path)
66 include ResponseHelpers
68 attr_accessor :request, :response
70 dslify_writter :status
72 def initialize(request, response, route_params)
75 @route_params = route_params
80 @params ||= @route_params.merge(@request.params).symbolize_keys
87 def complete(returned)
88 @response.body || returned
91 def render(content, options={}, &b)
92 @content = instance_eval(%Q{"#{content}"})
93 @content = instance_eval(&layout) if layout
100 Sinatra.application.layout
104 @response.body = content
107 def method_missing(name, *args, &b)
108 raise NoMethodError.new('body=') if name == :body=
109 @response.send(name, *args, &b)
119 def to_result(cx, *args)
121 cx.header.merge!('Location' => @path)
128 attr_reader :events, :layout
131 @events = Hash.new { |hash, key| hash[key] = [] }
134 def define_event(method, path, &b)
135 events[method] << event = Event.new(path, &b)
139 def define_layout(&b)
144 events[env['REQUEST_METHOD'].downcase.to_sym].eject(&[:invoke, env])
148 return [404, {}, 'Not Found'] unless result = lookup(env)
149 context = EventContext.new(
150 Rack::Request.new(env),
154 returned = catch(:halt) do
155 [:complete, context.instance_eval(&result.block)]
157 result = returned.to_result(context)
158 context.send :_body=, String === result ? [*result] : result
167 Sinatra.application.define_event(:get, path, &b)
171 Sinatra.application.define_event(:post, path, &b)
175 Sinatra.application.define_event(:put, path, &b)
179 Sinatra.application.define_event(:delete, path, &b)
183 Sinatra::EventContext.class_eval(&b)
187 Sinatra.application.define_layout(&b)
190 ### Misc Core Extensions
195 old_verbose, $VERBOSE = $VERBOSE, nil
198 $VERBOSE = old_verbose
205 # Converts +self+ to an escaped URI parameter value
206 # 'Foo Bar'.to_param # => 'Foo%20Bar'
211 # Converts +self+ from an escaped URI parameter value
212 # 'Foo%20Bar'.from_param # => 'Foo Bar'
222 map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
226 self.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
230 reject { |k,v| !keys.include?(k) }
238 Proc.new { |*args| args.shift.__send__(self, *args) }
246 self.inject({}) { |h, (k, v)| h[k] = v; h }
250 Proc.new { |*args| args.shift.__send__(self[0], *(args + self[1..-1])) }
258 find { |e| result = block[e] and break result }
263 ### Core Extension results for throw :halt
266 def to_result(cx, *args)
267 cx.instance_eval(&self)
272 def to_result(cx, *args)
273 cx.send :_body=, self
278 def to_result(cx, *args)
279 self.shift.to_result(cx, *self)
284 def to_result(cx, *args)
290 def to_result(cx, *args)