Class: LeanWeb::Controller

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(route, request = nil) ⇒ Controller

Returns a new instance of Controller.

Parameters:

  • route (Route)
  • request (Rack::Request) (defaults to: nil)


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

Get absolute path for a file within VIEW_PATH. - A full path, starts with /. - A path relative to VIEW_PATH. - A path relative to current @route.path directory, starts with ./.

Parameters:

  • path (String)

    Can be:

Returns:

  • (String)

    Absolute path.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/leanweb/controller.rb', line 85

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.

Parameters:

  • path (String) (defaults to: '.')

    path from public directory, never begins with /.



71
72
73
74
75
76
77
# File 'lib/leanweb/controller.rb', line 71

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.

Parameters:

  • path (String)

    Same as on #render_response.

  • options (Hash) (defaults to: nil)

    Options for Tilt, defaults to template_defaults[extension].

Returns:

  • (String)

    Rendered path.



55
56
57
58
59
60
# File 'lib/leanweb/controller.rb', line 55

def create_template(path, options = nil)
  path = absolute_view_path(path)
  ext = File.extname(path)[1..] || ''
  ext = 'erb' unless Tilt.registered?(ext)
  Tilt[ext].new(path, 1, options || template_defaults[ext] || {})
end

#default_static_action(view_path) ⇒ Object

Render response for missing static action methods. Called from Route#respond.



64
65
66
# File 'lib/leanweb/controller.rb', line 64

def default_static_action(view_path)
  render_response(view_path)
end

#paramsObject

Request params.



99
100
101
# File 'lib/leanweb/controller.rb', line 99

def params
  @request&.params
end

#render_response(path, content_type = nil) { ... } ⇒ Rack::Request#finish

Render magic. Supports every file extension that Tilt supports. Defaults to ERB when file extension is unknown.

Parameters:

  • path (String)

    Might be an absolute path or a relative path to src/views/.

  • content_type (String) (defaults to: nil)

    Defaults to the proper Content-Type for file extension, text/plain on unknown files.

Yields:

  • Optionally pass a block for nested rendering.

Returns:

  • (Rack::Request#finish)

    A valid Rack response.



39
40
41
42
43
44
45
46
47
# File 'lib/leanweb/controller.rb', line 39

def render_response(path, content_type = nil, &block)
  template = create_template(path)
  @response.set_header(
    'Content-Type',
    content_type || template.class.[:mime_type] || 'text/plain'
  )
  @response.write(template.render(self){ block.call if block_given? })
  @response.finish
end