forked from owningrails/patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfront_controller.rb
More file actions
32 lines (25 loc) · 858 Bytes
/
front_controller.rb
File metadata and controls
32 lines (25 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require "controller"
class FrontController
def call(env)
request = Rack::Request.new(env)
response = Rack::Response.new
controller_name, action_name = route(request.path_info)
controller_class = load_controller_class(controller_name)
controller = controller_class.new
controller.request = request
controller.response = response
controller.filter do
controller.send(action_name)
controller.render(action_name) unless controller.rendered?
end
response.finish
end
def route(path)
_, controller_name, action_name = path.split("/") # "", "home", "index"
[controller_name || "home", action_name || "index"]
end
def load_controller_class(name)
require "controllers/#{name}_controller"
Object.const_get(name.capitalize + "Controller") # HomeController
end
end