0
0
zero/lib/zero/controller.rb
Gibheer 77a2cbe6d4 a small fix to make the controller useable
This is just to use the controller for a demonstration on where this all
is heading to.
2012-11-26 21:38:49 +01:00

47 lines
1.2 KiB
Ruby

module Zero
# abstract class to make creation of controllers easier
#
# This abstract class creates an interface to make it easier to write
# rack compatible controllers. It catches #call and creates a new instance
# with the environment and calls #render on it.
class Controller
# initialize a new instance of the controller and call response on it
def self.call(env)
new(Zero::Request.new(env)).response
end
# set the renderer to use in the controller
def self.renderer=(renderer)
@@renderer = renderer
end
# get the renderer set in the controller
def self.renderer
@@renderer
end
# a small helper to get the actual renderer
def renderer
self.class.renderer
end
# initialize the controller
# @param request [Request] a request object
def initialize(request)
@request = request
@response = Zero::Response.new
end
# build the response and return it
#
# This method calls #process if it was defined so make it easier to process
# the request before rendering stuff.
# @return Response a rack conform response
def response
process if respond_to?(:process)
render
@response.to_a
end
end
end