Class: Jetski::BaseController

Inherits:
Object
  • Object
show all
Extended by:
Helpers::RouteHelpers
Includes:
Frontend::JavascriptHelpers
Defined in:
lib/jetski/base_controller.rb

Constant Summary collapse

RESERVED_INSTANCE_VARIABLES =
[
    :@res, :@performed_render, :@action_name, 
    :@controller_name, :@controller_path, :@cookies
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::RouteHelpers

route

Methods included from Frontend::JavascriptHelpers

#reactive_text_area

Constructor Details

#initialize(res) ⇒ BaseController

Returns a new instance of BaseController.



16
17
18
19
# File 'lib/jetski/base_controller.rb', line 16

def initialize(res)
  @res = res
  @performed_render = false
end

Instance Attribute Details

#action_nameObject

Returns the value of attribute action_name.



11
12
13
# File 'lib/jetski/base_controller.rb', line 11

def action_name
  @action_name
end

#controller_nameObject

Returns the value of attribute controller_name.



11
12
13
# File 'lib/jetski/base_controller.rb', line 11

def controller_name
  @controller_name
end

#controller_pathObject

Returns the value of attribute controller_path.



11
12
13
# File 'lib/jetski/base_controller.rb', line 11

def controller_path
  @controller_path
end

#cookiesObject

Returns the value of attribute cookies.



11
12
13
# File 'lib/jetski/base_controller.rb', line 11

def cookies
  @cookies
end

#paramsObject

Returns the value of attribute params.



11
12
13
# File 'lib/jetski/base_controller.rb', line 11

def params
  @params
end

#path=(value) ⇒ Object (writeonly)

Sets the attribute path

Parameters:

  • value

    the value to set the attribute path to.



14
15
16
# File 'lib/jetski/base_controller.rb', line 14

def path=(value)
  @path = value
end

#performed_renderObject (readonly)

Returns the value of attribute performed_render.



13
14
15
# File 'lib/jetski/base_controller.rb', line 13

def performed_render
  @performed_render
end

#request_method=(value) ⇒ Object (writeonly)

Sets the attribute request_method

Parameters:

  • value

    the value to set the attribute request_method to.



14
15
16
# File 'lib/jetski/base_controller.rb', line 14

def request_method=(value)
  @request_method = value
end

#resObject (readonly)

Returns the value of attribute res.



13
14
15
# File 'lib/jetski/base_controller.rb', line 13

def res
  @res
end

#root=(value) ⇒ Object (writeonly)

Sets the attribute root

Parameters:

  • value

    the value to set the attribute root to.



14
15
16
# File 'lib/jetski/base_controller.rb', line 14

def root=(value)
  @root = value
end

Instance Method Details



50
51
52
# File 'lib/jetski/base_controller.rb', line 50

def get_cookie(name)
  cookies&.find { |c| c.name == name.to_s }&.value
end

#redirect_to(url) ⇒ Object



41
42
43
44
# File 'lib/jetski/base_controller.rb', line 41

def redirect_to(url)
  @performed_render = true
  res.set_redirect(WEBrick::HTTPStatus::Found, url)
end

#render(**args) ⇒ Object

Method to render matching view with controller_name/action_name



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jetski/base_controller.rb', line 22

def render(**args)
  @performed_render = true
  request_status = args[:status] || 200
  res.status = request_status
  if args[:text]
    res.content_type = "text/plain"
    res.body = "#{args[:text]}\n"
    return
  end

  if args[:json]
    res.content_type = "application/json"
    res.body = args[:json].to_json
    return
  end

  ViewRenderer.new(self).call
end


46
47
48
# File 'lib/jetski/base_controller.rb', line 46

def set_cookie(name, value)
  res.cookies.push WEBrick::Cookie.new(name.to_s, value || "")
end