Class: Zero::Controller
- Inherits:
-
Object
- Object
- Zero::Controller
- Defined in:
- lib/zero/controller.rb
Overview
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.
Instance Attribute Summary collapse
-
#renderer ⇒ Object
readonly
the renderer which can be used to render templates.
Class Method Summary collapse
-
.call(env) ⇒ Object
initialize a new instance of the controller and call response on it.
-
.renderer ⇒ Object
get the renderer set in the controller.
-
.renderer=(renderer) ⇒ Object
set the renderer to use in the controller.
-
.request ⇒ Object
return the set request class.
-
.request=(request_class) ⇒ Object
set a class to use for requests.
-
.response ⇒ Object
return the set response class.
-
.response=(response_class) ⇒ Object
set the class to use for responses.
Instance Method Summary collapse
-
#initialize(env) ⇒ Controller
constructor
initialize the controller.
-
#response ⇒ Object
build the response and return it.
Constructor Details
#initialize(env) ⇒ Controller
initialize the controller
This creates a new controller instance using the defined classes of renderer, request and response.
51 52 53 54 55 |
# File 'lib/zero/controller.rb', line 51 def initialize(env) @request = self.class.request.new(env) @response = self.class.response.new @renderer = self.class.renderer end |
Instance Attribute Details
#renderer ⇒ Object (readonly)
the renderer which can be used to render templates
44 45 46 |
# File 'lib/zero/controller.rb', line 44 def renderer @renderer end |
Class Method Details
.call(env) ⇒ Object
initialize a new instance of the controller and call response on it
9 10 11 |
# File 'lib/zero/controller.rb', line 9 def self.call(env) new(env).response end |
.renderer ⇒ Object
get the renderer set in the controller
39 40 41 |
# File 'lib/zero/controller.rb', line 39 def self.renderer @@renderer end |
.renderer=(renderer) ⇒ Object
set the renderer to use in the controller
34 35 36 |
# File 'lib/zero/controller.rb', line 34 def self.renderer=(renderer) @@renderer = renderer end |
.request ⇒ Object
return the set request class
29 30 31 |
# File 'lib/zero/controller.rb', line 29 def self.request @@request ||= Zero::Request end |
.request=(request_class) ⇒ Object
set a class to use for requests
24 25 26 |
# File 'lib/zero/controller.rb', line 24 def self.request=(request_class) @@request = request_class end |
.response ⇒ Object
return the set response class
19 20 21 |
# File 'lib/zero/controller.rb', line 19 def self.response @@response ||= Zero::Response end |
.response=(response_class) ⇒ Object
set the class to use for responses
14 15 16 |
# File 'lib/zero/controller.rb', line 14 def self.response=(response_class) @@response = response_class end |
Instance Method Details
#response ⇒ Object
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.
62 63 64 65 66 |
# File 'lib/zero/controller.rb', line 62 def response process if respond_to?(:process) render @response.to_a end |