Class: Reloj::ControllerBase

Inherits:
Object
  • Object
show all
Includes:
RouteHelper
Defined in:
lib/reloj/core/controller_base.rb

Direct Known Subclasses

CatsController, TodosController

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RouteHelper

included

Constructor Details

#initialize(req, res, route_params = {}, paths = []) ⇒ ControllerBase

Returns a new instance of ControllerBase.



16
17
18
19
20
21
22
23
# File 'lib/reloj/core/controller_base.rb', line 16

def initialize(req, res, route_params = {}, paths = [])
  @req = req
  @res = res
  @already_built_response = false
  @params = Params.new(req, route_params)
  @paths = paths
  self.class.create_helper_methods(paths)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



14
15
16
# File 'lib/reloj/core/controller_base.rb', line 14

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



14
15
16
# File 'lib/reloj/core/controller_base.rb', line 14

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



14
15
16
# File 'lib/reloj/core/controller_base.rb', line 14

def res
  @res
end

Instance Method Details

#already_built_response?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/reloj/core/controller_base.rb', line 25

def already_built_response?
  @already_built_response
end

#flashObject



58
59
60
# File 'lib/reloj/core/controller_base.rb', line 58

def flash
  @flash ||= Flash.new(@req)
end

#invoke_action(name) ⇒ Object



62
63
64
65
# File 'lib/reloj/core/controller_base.rb', line 62

def invoke_action(name)
  self.send(name)
  render(name) unless self.already_built_response?
end

#redirect_to(url) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/reloj/core/controller_base.rb', line 29

def redirect_to(url)
  raise if already_built_response?
  @res.status = 302
  @res["Location"] = url
  @already_built_response = true
  flash.store_flash(@res)
  session.store_session(@res)
end

#render(template_name) ⇒ Object



47
48
49
50
51
52
# File 'lib/reloj/core/controller_base.rb', line 47

def render(template_name)
  controller_name = self.class.to_s.underscore
  f = File.read("app/views/#{controller_name}/#{template_name}.html.erb")
  f = ERB.new(f)
  render_content(f.result(binding), "text/html")
end

#render_content(content, content_type) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/reloj/core/controller_base.rb', line 38

def render_content(content, content_type)
  raise if already_built_response?
  @already_built_response = true
  @res.content_type = content_type
  @res.body = content
  flash.store_flash(@res)
  session.store_session(@res)
end

#sessionObject



54
55
56
# File 'lib/reloj/core/controller_base.rb', line 54

def session
  @session ||= Session.new(@req)
end