lib/happy/controller/actions.rb in happy-0.1.0.pre16 vs lib/happy/controller/actions.rb in happy-0.1.0.pre19
- old
+ new
@@ -1,54 +1,50 @@
module Happy
class Controller
module Actions
def serve!(data, options = {})
- # Don't serve if there are still bits of path remaining.
- return unless remaining_path.empty?
+ only_if_path_matches do
+ # Don't serve is data is not a string.
+ return unless data.is_a?(String)
- # Don't serve is data is not a string.
- return unless data.is_a?(String)
+ # Mix in default options
+ options = {
+ :layout => context.layout
+ }.merge(options)
- # Mix in default options
- options = {
- :layout => context.layout
- }.merge(options)
+ # Add status code from options
+ response.status = options.delete(:status) if options.has_key?(:status)
- # Add status code from options
- response.status = options.delete(:status) if options.has_key?(:status)
+ # Extract layout
+ layout = options.delete(:layout)
- # Extract layout
- layout = options.delete(:layout)
+ # Treat remaining options as headers
+ options.each { |k, v| header k, v }
- # Treat remaining options as headers
- options.each { |k, v| header k, v }
+ # Apply layout, if available
+ if layout
+ data = render(layout) { data }
+ end
- # Apply layout, if available
- if layout
- data = render(layout) { data }
+ # Set response body and finish request
+ response.body = [data]
+ halt!
end
-
- # Set response body and finish request
- response.body = [data]
- halt!
end
- def serve_or_404!(*args)
- serve!(*args)
-
- # If we get here, #serve! decided not to serve, so let's raise a 404.
- raise Errors::NotFound
- end
-
def halt!(message = :done)
- throw message
+ only_if_path_matches do
+ throw message
+ end
end
def redirect!(to, status = 302)
- header :location, url_for(to)
- response.status = status
- halt!
+ only_if_path_matches do
+ header :location, url_for(to)
+ response.status = status
+ halt!
+ end
end
def layout(name)
context.layout = name
end
@@ -85,15 +81,26 @@
# Happy controllers!
thing.new(env, options, &blk).perform
elsif thing.respond_to?(:call)
# Rack apps!
context.response = thing.call(request.env)
- halt!
+ throw :done
elsif thing.respond_to?(:to_s)
thing.to_s
else
raise "Don't know how to run #{thing.inspect} :("
end
+ end
+
+
+ private
+
+ # Execute the provided block, unless there are still bits of
+ # unprocessed path left (which indicates that the current path
+ # is not the path the user requested.)
+ #
+ def only_if_path_matches
+ yield if remaining_path.empty?
end
end
end
end