Class: Eksa::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/eksa/controller.rb

Direct Known Subclasses

PagesController, SeoController

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Controller

Returns a new instance of Controller.



8
9
10
11
12
# File 'lib/eksa/controller.rb', line 8

def initialize(request)
  @request = request
  @status = 200
  @flash = {}
end

Instance Attribute Details

#flashObject

Returns the value of attribute flash.



6
7
8
# File 'lib/eksa/controller.rb', line 6

def flash
  @flash
end

#redirect_urlObject

Returns the value of attribute redirect_url.



6
7
8
# File 'lib/eksa/controller.rb', line 6

def redirect_url
  @redirect_url
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/eksa/controller.rb', line 5

def request
  @request
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/eksa/controller.rb', line 6

def status
  @status
end

Instance Method Details

#paramsObject



14
15
16
# File 'lib/eksa/controller.rb', line 14

def params
  @request.params
end

#redirect_to(url, notice: nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/eksa/controller.rb', line 18

def redirect_to(url, notice: nil)
  @status = 302
  @redirect_url = url
  @flash[:notice] = notice if notice
  nil
end

#render(template_name, variables = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eksa/controller.rb', line 25

def render(template_name, variables = {})
  variables.each { |k, v| instance_variable_set("@#{k}", v) }

  content_path = File.expand_path("./app/views/#{template_name}.html.erb")
  layout_path  = File.expand_path("./app/views/layout.html.erb")

  if File.exist?(content_path)
    @content = ERB.new(File.read(content_path)).result(binding)
    if File.exist?(layout_path)
      ERB.new(File.read(layout_path)).result(binding)
    else
      @content
    end
  else
    "Error: View '#{template_name}' tidak ditemukan."
  end
end