Class: Puffs::ControllerBase
- Inherits:
-
Object
- Object
- Puffs::ControllerBase
- Defined in:
- lib/controller_base.rb
Overview
require_relative ‘puffs’
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
12 13 14 15 |
# File 'lib/controller_base.rb', line 12 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.
9 10 11 |
# File 'lib/controller_base.rb', line 9 def params @params end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
9 10 11 |
# File 'lib/controller_base.rb', line 9 def req @req end |
#res ⇒ Object (readonly)
Returns the value of attribute res.
9 10 11 |
# File 'lib/controller_base.rb', line 9 def res @res end |
Instance Method Details
#already_built_response? ⇒ Boolean
Helper method to alias @already_built_response
18 19 20 |
# File 'lib/controller_base.rb', line 18 def already_built_response? @already_built_response ||= false end |
#controller_name ⇒ Object
69 70 71 |
# File 'lib/controller_base.rb', line 69 def controller_name self.class.name.underscore end |
#invoke_action(name) ⇒ Object
use this with the router to call action_name (:index, :show, :create…)
64 65 66 67 |
# File 'lib/controller_base.rb', line 64 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
23 24 25 26 27 28 29 |
# File 'lib/controller_base.rb', line 23 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
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/controller_base.rb', line 44 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.
34 35 36 37 38 39 40 |
# File 'lib/controller_base.rb', line 34 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 |