Class: ControllerBase
- Inherits:
-
Object
- Object
- ControllerBase
- Defined in:
- lib/controller_base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#req ⇒ Object
readonly
Returns the value of attribute req.
-
#res ⇒ Object
readonly
Returns the value of attribute res.
Instance Method Summary collapse
-
#already_built_response? ⇒ Boolean
Helper method to alias @already_built_response.
- #controller_name ⇒ Object
-
#initialize(req, res, route_params = {}) ⇒ ControllerBase
constructor
Setup the controller.
-
#invoke_action(name) ⇒ Object
use this with the router to call action_name (:index, :show, :create…).
-
#redirect_to(url) ⇒ Object
Set the response status code and header.
-
#render(template_name) ⇒ Object
use ERB and binding to evaluate templates pass the rendered html to render_content.
-
#render_content(content, content_type) ⇒ Object
Populate the response with content.
-
#session ⇒ Object
method exposing a ‘Session` object.
Constructor Details
#initialize(req, res, route_params = {}) ⇒ ControllerBase
Setup the controller
11 12 13 14 |
# File 'lib/controller_base.rb', line 11 def initialize(req, res, route_params = {}) @req, @res = req, res @params = req.params.merge(route_params) end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/controller_base.rb', line 8 def params @params end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
8 9 10 |
# File 'lib/controller_base.rb', line 8 def req @req end |
#res ⇒ Object (readonly)
Returns the value of attribute res.
8 9 10 |
# File 'lib/controller_base.rb', line 8 def res @res end |
Instance Method Details
#already_built_response? ⇒ Boolean
Helper method to alias @already_built_response
17 18 19 |
# File 'lib/controller_base.rb', line 17 def already_built_response? @already_built_response ||= false end |
#controller_name ⇒ Object
68 69 70 |
# File 'lib/controller_base.rb', line 68 def controller_name self.class.name.underscore end |
#invoke_action(name) ⇒ Object
use this with the router to call action_name (:index, :show, :create…)
63 64 65 66 |
# File 'lib/controller_base.rb', line 63 def invoke_action(name) self.send(name) render(name) unless already_built_response? end |
#redirect_to(url) ⇒ Object
Set the response status code and header
22 23 24 25 26 27 28 |
# File 'lib/controller_base.rb', line 22 def redirect_to(url) raise DoubleRenderError if already_built_response? res.header['location'] = url res.status = 302 @already_built_response = true session.store_session(res) end |
#render(template_name) ⇒ Object
use ERB and binding to evaluate templates pass the rendered html to render_content
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/controller_base.rb', line 43 def render(template_name) body_string = "" File.open("./app/views/#{controller_name}/#{template_name}.html.erb", "r") do |f| f.each_line do |line| body_string += line end end #File.readlines #File.read content = ERB.new(body_string).result(binding) render_content(content, 'text/html') end |
#render_content(content, content_type) ⇒ Object
Populate the response with content. Set the response’s content type to the given type. Raise an error if the developer tries to double render.
33 34 35 36 37 38 39 |
# File 'lib/controller_base.rb', line 33 def render_content(content, content_type) raise DoubleRenderError if already_built_response? res['Content-Type'] = content_type res.body = [content] @already_built_response = true session.store_session(res) end |
#session ⇒ Object
method exposing a ‘Session` object
58 59 60 |
# File 'lib/controller_base.rb', line 58 def session @session ||= Session.new(req) end |