Class: LeanWeb::Controller
- Inherits:
-
Object
- Object
- LeanWeb::Controller
- Defined in:
- lib/leanweb/controller.rb
Overview
Controller is a base controller with @route
, @request
and @response
private attributes that will be shared with your views when you
#render_response.
Even if you don't #render_response, you can use the .finish
method from
Rack::Response
on @response
to return a proper Rack
response.
Instance Method Summary collapse
-
#absolute_view_path(path) ⇒ String
Get absolute path for a file within VIEW_PATH.
-
#base_url(path = '.') ⇒ Object
Relative route to path from public directory considering current route.
-
#create_template(path, options = nil) ⇒ String
Template rendering engine.
-
#default_static_action(view_path) ⇒ Object
Render response for missing static action methods.
-
#initialize(route, request = nil) ⇒ Controller
constructor
A new instance of Controller.
-
#params ⇒ Object
Request params.
-
#render_response(path, locals = {}, content_type = nil) { ... } ⇒ Rack::Request#finish
Render magic.
Constructor Details
#initialize(route, request = nil) ⇒ Controller
Returns a new instance of Controller.
23 24 25 26 27 28 |
# File 'lib/leanweb/controller.rb', line 23 def initialize(route, request = nil) @route = route @request = request @response = Rack::Response.new(nil, 200) @content_for = {} end |
Instance Method Details
#absolute_view_path(path) ⇒ String
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/leanweb/controller.rb', line 89 def absolute_view_path(path) return path if path.start_with?('/') view_path = String.new(LeanWeb::VIEW_PATH) if path.start_with?('./') view_path << @route.path.sub(%r{/[^/]*$}, '') path = path[2..] end path == '' ? view_path : "#{view_path}/#{path}" end |
#base_url(path = '.') ⇒ Object
Relative route to path from public directory considering current route.
75 76 77 78 79 80 81 |
# File 'lib/leanweb/controller.rb', line 75 def base_url(path = '.') @base_url ||= @route.str_path[1..] .sub(%r{[^/]*$}, '') .gsub(%r{[^/]+}, '..') @base_url + path end |
#create_template(path, options = nil) ⇒ String
Template rendering engine. Useful for partials / nested views.
59 60 61 62 63 64 |
# File 'lib/leanweb/controller.rb', line 59 def create_template(path, = nil) path = absolute_view_path(path) ext = File.extname(path)[1..] || '' ext = 'erb' unless Tilt.registered?(ext) Tilt[ext].new(path, 1, || template_defaults[ext] || {}) end |
#default_static_action(view_path) ⇒ Object
Render response for missing static action methods. Called from Route#respond.
68 69 70 |
# File 'lib/leanweb/controller.rb', line 68 def default_static_action(view_path) render_response(view_path) end |
#params ⇒ Object
Request params.
103 104 105 |
# File 'lib/leanweb/controller.rb', line 103 def params @request&.params end |
#render_response(path, locals = {}, content_type = nil) { ... } ⇒ Rack::Request#finish
Render magic. Supports every file extension that Tilt supports. Defaults to ERB when file extension is unknown.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/leanweb/controller.rb', line 40 def render_response(path, locals = {}, content_type = nil, &block) template = create_template(path) @response.set_header( 'Content-Type', content_type || template.class.[:mime_type] || template.class.superclass.[:mime_type] || 'text/plain' ) @response.write( template.render(self, locals){ block.call if block_given? } ) @response.finish end |